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
|
249
HACK
Picture as Indicator
The first solution relies on cycling the brightness of a picture in a panel.
Instead of moving a rectangle back and forth, you can increase and decrease
the brightness of a picture. The result is much more appealing and lets you
choose an adequate picture regarding the current running task. Figure 6-14
shows the
AnimatedPanel
in action.
When you click the Start button, the contents of the Animated tab are
replaced by a panel containing a picture and a message. With the picture’s
brightness cycles, as shown in Figure 6-15, you can see the most bright state
on the left and the less bright state on the right.
Figure 6-14. The first tab demonstrates the use of a picture as an indefinite progress
indicator
Figure 6-15. AnimatedPanel displays a picture and cycles its brightness
250
|
Chapter 6, Transparent and Animated Windows
#47 Indefinite Progress Indicator
HACK
To change the brightness of the picture dynamically, AnimatedPanel starts an
animation thread responsible for computing the brightness over the time:
public void start( ) {
this.animator = new Thread(new HighlightCycler( ), "Highlighter");
this.animator.start( );
}
class HighlightCycler implements Runnable {
private int way = 1;
private final int LOWER_BOUND = 10;
private final int UPPER_BOUND = 35;
private int value = LOWER_BOUND;
public void run( ) {
while (true) {
try {
Thread.sleep(1000 / (UPPER_BOUND - LOWER_BOUND));
} catch (InterruptedException e) {
return;
}
value += this.way;
if (value > UPPER_BOUND) {
value = UPPER_BOUND;
this.way = -1;
} else if (value < LOWER_BOUND) {
value = LOWER_BOUND;
this.way = 1;
}
synchronized (convolvedImage) {
setBrightness((float) value / 10);
setGradientFactor((float) value / UPPER_BOUND);
}
}
}
}
When the start( ) method of the panel is called, a new thread is spawned to
run
HighlightCycler. The animation loop is very simple, and it increases or
decreases the variable
value until its value reaches LOWER_BOUND or UPPER_
BOUND
. Then, setBrightness( ) is invoked to change the brightness of the pic-
ture. Given the values of the bounds, the brightness swings between 1.0
(unchanged) and 3.5 (the picture is 3.5 times brighter). You can notice this
call is synchronized with the object
convolvedImage, which is one of the two
BufferedImages contained in AnimatedPanel. This image is the result of a fil-
ter applied on
originalImage, the picture that contains the unchanged origi-
nal picture. As applying the filter can take some time, the code synchronizes
on
convolvedImage to ensure the previous filtering is done before running a
new one. This code shows how to change the brightness of the picture:

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