Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing applet design paintComponent or repaint ?

 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I made these changes based on your observations.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic