By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,846 Members | 2,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,846 IT Pros & Developers. It's quick & easy.

Can someone tell why is paint method executing eventhough it is not called

P: 48
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class ShapeTest extends JFrame{
  5.      public ShapeTest(){
  6.           setSize(400,400);
  7.           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  8.          // setLocationRelativeTo(null);
  9.           setVisible(true);
  10.      }
  11.  
  12.      public static void main(String a[]){
  13.          new ShapeTest();
  14.      }
  15.  
  16.      public void paint(Graphics g){
  17. g.setColor(Color.RED);
  18.          // g.drawOval(40, 40, 60, 60); //FOR CIRCLE
  19.           g.drawRect(80, 30, 200, 200); // FOR SQUARE
  20.        //   g.drawRect(200, 100, 100, 200); // FOR RECT
  21.      }
  22. }
Aug 21 '15 #1

✓ answered by chaarmann

You are extending your class from JFrame. That means that your class also has all methods that JFrame has but that are not in your code listing.

Some of these methods in JFrame registers a listener. This is a class that calls the paint-method that you have written. It can call your paint method any time, for example when the screen needs to be refreshed.

That means the call is not originated in your class, but will happen from outside by other classes. That's why the paint() method is also called a callback-method.

Share this Question
Share on Google+
2 Replies


Kara Hewett
P: 27
The jvm engine calls the paint method every time the operative system reports that the canvas has to be painted. When the window is created for the first time, paint is called by the JVM. The paint method is also called if we minimize and after we maximize the window and if we change the size of the window with the mouse.
Aug 31 '15 #2

Expert 100+
P: 785
You are extending your class from JFrame. That means that your class also has all methods that JFrame has but that are not in your code listing.

Some of these methods in JFrame registers a listener. This is a class that calls the paint-method that you have written. It can call your paint method any time, for example when the screen needs to be refreshed.

That means the call is not originated in your class, but will happen from outside by other classes. That's why the paint() method is also called a callback-method.
Sep 2 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.