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