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

Make Your Frame Dissolve #42
Chapter 6, Transparent and Animated Windows
|
221
HACK
// start animation
new Thread(this).start( );
} catch (Exception ex) {
System.out.println(ex);
ex.printStackTrace( );
}
}
dissolveExit( ) saves a reference to the frame and creates a new java.awt.
Robot
to handle the screen captures. Then it captures just the area of the
screen containing the frame by calling
robot.createScreenCatpure( ), using
the rectangle returned by
frame.getBounds( )
. After making this screen cap-
ture, it hides the frame and then captures the entire screen into the second
buffer. Finally, it creates a new
Window
that covers the entire screen, adds the
Dissolver as the window’s only child, and starts the animation on a new
thread.
There are two tricky things to look out for here. First, the code uses a
Window
instead of a JFrame so that the window won’t show up in a task list or the
dock. This also means it won’t have any window decorations that would
ruin the illusion of transparency. Second, the
Window constructor requires
you to pass in another window or frame. This is because every window is
attached to a parent window and will only be visible when the parent is (on
certain platforms). Using the existing application frame won’t work because
it’s just been hidden, but if you create a new, empty
JFrame, the window will
show up fine.
Run the Animation
dissolveExit( ) creates a new thread around the run( ) method (see the fol-
lowing code). This method will request a repaint on the component every
100 milliseconds, looping 20 times and then quitting. This will create a two-
second dissolve animation, but you can certainly tweak these values to your
own tastes:
public void run( ) {
try {
count = 0;
Thread.currentThread( ).sleep(100);
for(int i=0; i<20; i++) {
count = i;
fullscreen.repaint( );
Thread.currentThread( ).sleep(100);
}
} catch (InterruptedException ex) { }
System.exit(0);
}

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