• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

repaint, update and paint

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to get the relationship between these three calls straight in my head. I know repaint() calls (or rather causes to be called) update(), which (causes to be) calls paint().
My question is:
- which do I call in my code (only ever repaint?),
- which do I override (update() and paint()?), and
- which does AWT call and when?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
These are the answers (as far as I know it)... in your code you will *almost always* only call repaint(), you can override either method (and sometimes both), and technically I think the AWT calls them both.
Basically, as a further explanation of these somewhat obscure sounding answers, I think that the versions of update() and paint() that you inherit when you make a AWT component have a relationship defined between them. update() probably does some stuff that needs to be done before repainting the graphics (like invalidating areas of the graphics or something...), then paint() (which actually defines what needs to be done with the graphics) is called by update(). I think that after some predefined time (not sure how long this is or how it is done) the default AWT component calls repaint(), which calls update(), which calls paint().
However, you can override either of these methods for your own purposes. For simple AWT components you may only need to override the paint() method, which changes the way the AWT component appears. For some more advanced AWT components you may want to override both update() and paint(), mostly for components that do some kind of animation. You must remember to call paint() at the end of your own overridden update(), however, or the changes will not show up. It is also a good idea to have repaint() called frequently with a timed thread so that you have a constant frame rate on the animation.
Hopefully this helps you out and does not sound too confusing,
-Nathan
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Update() clears the existing area prior to repainting. If you choose to override update, you should do something similar, or you will end up with all kinds of garbage on your GUI. One good reason for overriding update in a component is to speed up painting and make an animation more efficient. For example, if you know that you only want to update a 10 x 10 square in your component, you could override update to clear that area (paint it to the background color) and then call paint. This can also be achieved by calling one of the overloaded repaint methods that takes a Rectangle Object as an argument, and that would probably be easier than trying to override update().
 
The moustache of a titan! The ad of a flea:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic