Indefinite Progress Indicator #47
Chapter 6, Transparent and Animated Windows
|
251
HACK
private void setBrightness(float multiple) {
float[] brightKernel = { multiple };
RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
BufferedImageOp bright = new ConvolveOp(new Kernel(1, 1, brightKernel),
ConvolveOp.EDGE_NO_OP, hints);
bright.filter(originalImage, convolvedImage);
repaint( );
}
The parameter multiple tells the method how many times brighter than the
original the resulting image must be. To achieve this effect, you can simply
perform a convolve operation on the original image. The kernel contains a
single value,
multiple. When the filter( ) method is invoked, every pixel
value of the original image is multiplied by the value of the kernel, making it
brighter. The result is stored in
convolveImage. It is now easy to use the
AnimatedPanel in an application:
Icon icon = UIHelper.readImageIcon("network.png");
AnimatedPanel animated = new AnimatedPanel("Waiting in vain...", icon);
getContentPane( ).add(animated);
animated.start( );
Besides the start( ) method, you can call stop( ) to interrupt the animation
thread when the task is done. This panel can be very efficient when used
with a
CardLayout. As this layout lets you stack components, you can put a
form on top of the animated panel. When the user performs an action, you
can display the animated panel and start the animation. At the end of the
task, you just need to stop the animation and hide the animated panel.
The example file Demo.java contains a complete example
using
CardLayout.
The Glass Pane as an Indicator
Using the AnimatedPanel proves to be efficient in form-oriented applica-
tions. Unfortunately, it does not help much when the running task must not
be interrupted by any action performed by the user. In this case, you need a
way to disable the entire GUI while displaying an indefinite progress indica-
tor. This can be done with a glass pane, as shown in Figure 6-16.
InfiniteProgressPanel is an animated glass pane you can set up on any
Swing frame:
InfiniteProgressPanel glassPane = new InfiniteProgressPanel( );
setGlassPane(glassPane);
glassPane.start( );
252
|
Chapter 6, Transparent and Animated Windows
#47 Indefinite Progress Indicator
HACK
When you start( ) the panel, a fade-in animation is played during the ramp-
up phase. When
stop( ) is called, a fade-out animation is played. Between
the two, a circular shape is rotated at the center of the glass pane. All the
while, a white translucent veil is drawn over the underlying UI to disable it
visually.
The implementation of
InfiniteProgressPanel relies on many parameters,
which you can specify with the various constructors:
text
The optional message to be displayed below the circular shape
barsCount
The number of bars composing the circular shape
shield
The opacity of the white veil, also known as the shield
fps
The requested amount of frames per second during the animation
rampDelay
The time, in milliseconds, that fade-in and fade-out animations should
last
Figure 6-16. InfiniteProgressPanel draws a white veil to disable the UI visually

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.