| Author |
Swing applet design paintComponent or repaint ?
|
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
Here is an example of a swing applet which uses paintComponent. The paintComponent method is called almost non stop. I can prevent its executing by setting a flag which prevents the thread from continuing once it enters the paintComponent method - unless there is actually a change in the data.
However I believe there must be a design solution to this issue or perhaps just a better design which does not use paintComponet but instead calls repaint?? All the objects in paintComponet are native - no custom graphics.
Any advice please.
Thanks!
link to code snippet:
http://pastebin.com/f9BEPBc4
|
 |
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1788
|
|
Code should be posted in this forum.
I don't understand the question.
super.paintComponent() should only ever be executed in the paintComponent() method if you are overriding the method.
repaint() is used to tell the component to repaint() itself when a property that controls the painting of the component is changed.
|
 |
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
|
Thank you for the response. I am trying to get a pointer on how to restructure this code so it does not use paint component.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
A few mistakes:
- paintComponent should remain protected. There's no need to make it public.
- paintComponent misses the call to super.paintComponent(g).
- you're creating user interfaces and have complete business logic in the paintComponent method. That method's going to be called A LOT.
The paint methods should be used for just that - painting the component.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
|
Thank you. I made these changes based on your observations.
|
 |
 |
|
|
subject: Swing applet design paintComponent or repaint ?
|
|
|