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

254
|
Chapter 6, Transparent and Animated Windows
#47 Indefinite Progress Indicator
HACK
AffineTransform toWheel = new AffineTransform( );
toWheel.concatenate(toCenter);
toWheel.concatenate(toBorder);
primitive.transform(toWheel);
primitive.transform(toCircle);
ticker[(int) i] = primitive;
}
return ticker;
}
The buildTicker( ) method returns an array of Areas, where each Area is one
of the bars composing the resulting shape. A bar is built in
buildPrimitive( )
by merging two circles and a rectangle. The role of buildTicker( ) is simply
to create the requested amount of bars and to move them to their final
location. To do that, the code applies several
AffineTransform operations:
toCenter first translates the bar to the center of the glass pane, toBorder then
moves the bar to the perimeter of the circle you want to create, and
toCircle
finally rotates the bar around the glass pane’s center.
Paint the Indicator
Once ticker has been built, paintComponent( ) only needs to get each bar
and fill it on the graphics surface of the glass pane:
public void paintComponent(Graphics g)
{
if (started)
{
int width = getWidth( );
int height = getHeight( );
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHints(hints);
g2.setColor(new Color(255, 255, 255, (int) (alphaLevel * shield)));
g2.fillRect(0, 0, getWidth(), getHeight( ));
for (int i = 0; i < ticker.length; i++)
{
int channel = 224 - 128 / (i + 1);
g2.setColor(new Color(channel, channel, channel, alphaLevel));
g2.fill(ticker[i]);
}
}
}

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