Results 1 to 5 of 5
- 06-03-2010, 10:49 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
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 ??
- 06-03-2010, 03:35 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 06-03-2010, 04:28 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
Thank you for the answer and the link.
However it is very strange why this behavior is followed in that code....
- 06-03-2010, 04:36 PM #4
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.
- 06-03-2010, 06:21 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
how to add more than one paint method
By gautham in forum Java 2DReplies: 2Last Post: 04-06-2010, 07:07 AM -
JOptionPane when called from inside the paintComponent() method
By Y. Progammer in forum New To JavaReplies: 10Last Post: 02-28-2010, 01:52 PM -
what made paintComponent() method to be called twice??
By Y. Progammer in forum New To JavaReplies: 5Last Post: 02-21-2010, 10:19 PM -
[SOLVED] how to count how many times you used a method
By Laythe in forum New To JavaReplies: 8Last Post: 06-07-2009, 02:29 AM -
Recursive Method ==> find how many times a value is repeated in an array
By NatNat in forum New To JavaReplies: 2Last Post: 02-16-2008, 08:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks