
Animating a Sheet Dialog #45
Chapter 6, Transparent and Animated Windows
|
237
HACK
startAnimation( ) begins by refreshing the glass pane with a repaint( ). It
then informs the
AnimatingSheet of its new source (i.e., the sheet) and adds
the
AnimatingSheet
to the layout. It then sets up a
javax.swing.Timer
to get
callbacks to an
actionPerformed( )
method that performs each step of the
animation. The
ANIMATION_SLEEP
parameter affects how smooth the anima-
tion will look—shorter sleeps will result in a higher frame rate, but higher
CPU use.
I’ve set this value as low as 2 ms and haven’t had a problem,
but I’m on a pretty fast box (a dual 1.8 GHz G5 Power Mac).
You might want to play with this value to get an ideal
performance-to-smoothness ratio.
stopAnimation( ) just stops the Timer, as you might expect.
The
actionPerformed( ) method is used by the Timer callbacks. It has two
tasks: calculating the height of the sheet to show, and wrapping up the ani-
mation if it has run its course. Notice that the progress calculation doesn’t
assume that it has been called back in accordance with the
ANIMATION_SLEEP.
It calculates the current time offset from when the animation began, and cal-
culates a progress from that, which in turn allows it to figure out how much
of the sheet to show. This is a really good practice because if your computer
can’t keep up with your desired rate, the
Timer will combine multiple call-
backs into ...