• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Repainting within a applet

 
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?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • 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
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic