File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes paint() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "paint()" Watch "paint()" New topic


Tony kunds

Joined: Apr 25, 2002
Posts: 25
In what situations a paint() is called ,
in what situations update() is called and
in what situations repaint() is called
I know repaint()->update()->paint();
I want to know when is paint() called or when is update() called

Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
paint should never be called directly at any time, not by your applet and it isn't supposed to be called by the JVM integrated with a browser.
update is the magic method that will get called without your code directly causing it. Any kind of windowing event that requires applications to repaint themselves will result in update being called with an appropriate clipping region.

Reid - SCJP2 (April 2002)
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
All the AWT Components know how to draw themselves except Canvas and Containers. The AWT components that draw themselves do it thanks to the code placed in its paint method. Canvas has no code in paint; the programmer has to provide it. I think Containers pass the call to its paint method through the components they contain.
paint is called by a unique thread that is responsible for both painting and event handling. This is so to prevent the drawing of a component from being interrupted by other's, or by an event handling process. These events are "enqued" to the same thread, thus they happens sequentially.
paint shouldn't be called directly by a program, but a call to repaint should be used to enque a petition for the painting thread to draw the component (calling its paint method). The petition takes the form of a scheduling of a call to the component's update method in the painting thread.

The update method clears the component's background and it call its paint method.
Also: AWT calls automatically paint method when a component is first time shown, or whenever is damaged (clipped)
[ May 06, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Kapil Sachdeva

Joined: Apr 10, 2002
Posts: 7
I would like to put Jose's explanations in this way:
Mechanism behind these methods is GUI thread. This Thread can be thought of as a big loop that always run, and call methods based on the value of events that have been set since last time loop was run. When a call to show() method of frame is made a "paint" event for the frame is scheduled. GUI Thread starts running and calls the paint() method. When update() method is called it sets an event for GUI thread to call paint() method the next time through the loop. When GUI thread returns from the paint() method, it runs the loop again, sees that a "paint" event has been set, and calls paint() again.
Above was explanation for update->paint.
Now for repaint->update, its simply a method call in which repaint method call the repaint method of Peer class [Platform dependent implementation]
of GUI objects.
Kapil Sachdeva
Tony kunds

Joined: Apr 25, 2002
Posts: 25
Thanks for the long reply,but many things bounced my head.
can soem one explain in a simple way.
what i want most is when a componet resizezs or shadowed by other components which method is called .
and also if a componet or container is resized and we want to retain the previous drawn thigs which method to call..
and some basic stuff like that..
thanks for the help
I agree. Here's the link:
subject: paint()
jQuery in Action, 3rd edition