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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnejeevansai
    New Member
    • Nov 2014
    • 48

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

    Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class ShapeTest extends JFrame{
         public ShapeTest(){
              setSize(400,400);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             // setLocationRelativeTo(null);
              setVisible(true);
         }
    
         public static void main(String a[]){
             new ShapeTest();
         }
    
         public void paint(Graphics g){
    g.setColor(Color.RED);
             // g.drawOval(40, 40, 60, 60); //FOR CIRCLE
              g.drawRect(80, 30, 200, 200); // FOR SQUARE
           //   g.drawRect(200, 100, 100, 200); // FOR RECT
         }
    }
    Last edited by Rabbit; Aug 21 '15, 05:51 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Kara Hewett
    New Member
    • Apr 2014
    • 27

    #2
    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.

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      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.

      Comment

      Working...