• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Repainting within a applet

 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following problem:
I would like to repaint only certain object(s) within a applet based on a certain trigger. How can I accomplish this without refreshing the while applet?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple answer is just to call repaint() on the sub-component of the applet. ( For example, if you have a Canvas called imageCanvas1 that you want to repaint without repainting the entire applet, you could call it's repaint() method directly by calling :

)
However, this solution is not as simple as it seems... if you do anything that would cause the sub-component to change size, or change location on the screen, you will need to call doLayout() on the component that holds your sub-component... and doLayout() forces a repaint() to be called on the component that doLayout() is called on, and on all the sub-components of that component.
So the answer is, for small changes, simply calling repaint() on a sub-component will work, but for most things you cannot get away with not doing a repaint of the top level GUI element. I'm sure that if this is a big problem for you, you can find some way around it, like creating a Panel big enough to hold the maximum size of all your sub-components and then calling doLayout() on that Panel instead of the applet... but I am not sure if that is any better because all your sub-components get painted anyway...
Anyway, here's some code that I used to test this out... all you need to do to see what is going on is to replace the two images I was using ( washu and abbey ) with images of your own, and then comment and uncomment lines to see what goes on...

Enjoy!
HTH,
-Nate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic