Results 1 to 5 of 5
  1. #1
    supremo is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Unhappy Why the paint() method is called two times ?

    This code calls two times the paint() method when I run this:

    package KLL;
    import javax.swing.*;
    import java.awt.*;


    public class KLL extends JFrame {

    static int a;
    static int b;
    public KLL(){
    super();
    a=12;
    b=20;

    setSize(300,300);

    setVisible(true);

    }

    public void paint(Graphics g){

    System.out.println(a);
    Graphics2D g2;
    g2=(Graphics2D) g;
    g2.draw3DRect(100, 100, a, b, false);
    }

    public static void main (String[] args) throws InterruptedException{
    KLL K=new KLL();
    Thread.currentThread();
    Thread.sleep(3000);
    a=80;
    b=120;
    K.repaint();
    Thread.sleep(3000);
    a=200;
    b=120;
    K.repaint();
    }

    }


    When using repaint() it is called only 1 time which is understandable. But why when I run this code it is called two times at the beginning ??

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I have no idea but that is NOT the way to do custom painting.

    Read the section from the Swing tutorial on Custom Painting for the proper way to do this.

  3. #3
    supremo is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank you for the answer and the link.

    However it is very strange why this behavior is followed in that code....

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    The JVM AWT engine determines how and when to call paint(); It can merge repaint() requests into a single call to paint().

    Bottom-line Not always easy to predict when paint() will be called.

  5. #5
    supremo is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 08:07 AM
  2. Replies: 10
    Last Post: 02-28-2010, 02:52 PM
  3. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 11:19 PM
  4. Replies: 8
    Last Post: 06-07-2009, 03:29 AM
  5. Replies: 2
    Last Post: 02-16-2008, 09:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •