• Post Reply Bookmark Topic Watch Topic
  • New Topic

public void paint(Graphics page) - method seems to run twice  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code for painting a number of random circles on a JApplet Graphics page.



I works, but seems to perform the 'public void paint (Graphics page)' method twice.

This happens for any value of 'numCircles'. I always get two sets of circles.  It seems that one complete set (with a "largest red" circle) is painted, and then a second complete set is painted over the first - because sometimes there is a black circle outline painted over a red filled circle.

I can't get over thinking that the loop structure (line 20) is a potential cause of the problem even though the program outputs two complete sets of circles, including two "largest red" circles, and the "largest red" circle is painted outside of the loop.  But to be safe, I have tried several variations of 'for' and 'while' loops, to no avail.

I've gotta be missing something simple.  Pleeeeeease help....

Thanks in advance.
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seems to perform the 'public void paint (Graphics page)' method twice.

Add a print statement to the paint() method to see how often it is called.
The paint() method is called every time the JVM thinks the GUI needs to be refreshed and in response to a call to repaint().

Note: the statements on lines 17 and 18 should not be inside of the paint() method. Normally they should only be executed one time in the init or start method.
 
Dustin White
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a print statement to the paint() method to see how often it is called.

It's definitely called twice.

Note: the statements on lines 17 and 18 should not be inside of the paint() method. Normally they should only be executed one time in the init or start method.


Thanks! But how do I execute them outside (before?) the paint() method, when the Graphics page does not yet exist?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do I execute them outside (before?) the paint() method

I assume the "them" you are talking about are statements on lines 17 & 18.  They don't use the Graphics object passed to the paint method.
Note: the statements on lines 17 and 18 should not be inside of the paint() method. Normally they should only be executed one time in the init or start method.


It's definitely called twice.

How are you executing it?  The paint() method should be called every time the JVM needs to have the GUI repainted, like when the frame is minimized and restored.
 
Dustin White
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running the code as an applet via Eclipse.

Yes, by "them" I am referring to lines 17 and 18.  The paint() methods is only called once when I remove them - but then I don't get the appearance I want.  And when I manually resize the Applet window (Graphics page), it causes paint() to be called again.
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I remove them 

They should MOVED (not REMOVED) to the init() or start() method.
 
Dustin White
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much!!
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you writing applets? Nobody uses them any more.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!