Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Swing applet design   paintComponent or repaint ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing applet design   paintComponent or repaint ?" Watch "Swing applet design   paintComponent or repaint ?" New topic
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: 2092
    
    7
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: 19544
    
  16

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 - OCEEJBD 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing applet design paintComponent or repaint ?
 
Similar Threads
fluctuation
Does the repaint() method run the paintComponent() method automatically?
Having two problem on paint
Applet initialization problem
How to properly create an applet with an Input field and some shapes.