O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Indefinite Progress Indicator #47
Chapter 6, Transparent and Animated Windows
|
255
HACK
To make things clearer, this code does not contain the painting of the text.
To allow the glass pane to be visible even when the animation is not started,
the application first checks the value of
started
. When false,
start( )
has
not been called yet and nothing must be drawn. In the other case, the white
veil is first painted all over the glass pane. The fourth parameter of the
Color
object used by
fillRect( )
defines the opacity. Opacity depends on two
parameters here:
shield and alphaLevel. The latter is computed by the ani-
mation thread to fade in and fade out the glass pane. As
alphaLevel is
between 0 and 255, and you multiply it by
shield, which is in the range 0.0
to 1.0, to obtain the opacity of the veil. Finally, the
for
loop computes a new
color for each bar and paints it. The colors are computed so as to create a
circular gradient and give an impression of movement when the circular
shape is rotated.
Run the Animation Thread
The most complicated part of InfiniteProgressPanel is the Animator thread
since its
run( ) method handles the fade-in and -out animations, as well as
the main animation. The only constructor of
Animator requires a boolean
parameter called
rampUp. When true, the thread will play both main and
fade-in animations. Otherwise, the fade-out animation is played. This is
what happens when you call
InfiniteProgressPanel.stop( ). Here is the
code of
Animator.run( ) to handle the main animation:
public void run( )
{
Point2D.Double center;
center = new Point2D.Double((double) getWidth( ) / 2,
(double) getHeight( ) / 2);
double fixedIncrement = 2.0 * Math.PI / ((double) barsCount);
AffineTransform toCircle;
toCircle = AffineTransform.getRotateInstance(fixedIncrement,
center.getX( ),
center.getY( ));
long start = System.currentTimeMillis( );
if (rampDelay == 0)
alphaLevel = rampUp ? 255 : 0;
started = true;
boolean inRamp = rampUp;
while (!Thread.interrupted( ))
{
if (!inRamp)
{
256
|
Chapter 6, Transparent and Animated Windows
#47 Indefinite Progress Indicator
HACK
for (int i = 0; i < ticker.length; i++)
ticker[i].transform(toCircle);
}
repaint( );
// fade-in/out animation
try
{
Thread.sleep(inRamp ? 10 : (int) (1000 / fps));
} catch (InterruptedException ie) {
break;
}
Thread.yield( );
}
}
The first step of this method is to create an instance of AffineTransform that
will be used to rotate the bar by a fixed increment. By applying this transfor-
mation to the bars, each bar will move clockwise to the location of the next
bar. The rotation cannot happen during the fade-in animation, when
rampUp,
and therefore
inRamp, are true. After having rotated the ticker and repainted
the glass pane, the thread goes to sleep. The length of the sleep is defined by
the request amount of frames per second. The more frames you specify, the
more CPU resources the animation will need. Finally, the fade-in and fade-
out animations, not shown here, are performed by computing
alphaLevel
according to the elapsed time since the beginning of the animation, stored in
the variable
start. The alpha level is computed so that it goes, during the
fade in, from 0 to 255 in
rampDelay milliseconds. The same rule applies to
the fade-out animation.
Displaying nice-looking, indefinite progress indicators is not very difficult in
Swing, even though it requires some work. Using a circular shape is defi-
nitely the best way to show an infinite progress indicator because you can
walk around it indefinitely, even if you always go in the same direction.
—Romain Guy

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required