Eliminating Flashing
The flashing that you might have noticed
in the example on the previous page
is a common problem with animation
(and occasionally with static graphics).
The flashing effect is the result of two facts:
- By default, the background of the animation is cleared
(its whole area redrawn in the background color)
before the
paint() method is called.
- The computation in the previous example's
paint() method
is complex enough that it takes longer to compute and draw
each frame of animation
than the video screen's refresh rate.
As a result, the first part of the frame
is drawn on one video refresh pass,
and the rest of the frame is drawn
on the next (or even the pass after that).
The result is that
although the first part of the frame is animated smoothly (usually),
you can see a break between the first and second parts,
since the second part is blank until the second pass.
You can use two techniques to eliminate flashing:
overriding the update() method
and implementing double buffering.
Overriding the update() Method
To eliminate flashing, whether or not you use double buffering,
you must override the update() method.
This is necessary because it's the only way
to prevent the entire background of the Component
from being cleared every time the Component is drawn.
Implementing Double Buffering
Double buffering involves performing multiple graphics operations
on an undisplayed graphics buffer,
and then displaying the resulting image onscreen.
Double buffering prevents incomplete images
from being drawn to the screen.