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

220
|
Chapter 6, Transparent and Animated Windows
#42 Make Your Frame Dissolve
HACK
To keep this simple, I have created a special class that does a simple fade-to-
transparent animation. Once the class is built, you can create more compli-
cated animations by overriding the paint method, leaving the messy details
to the parent class. Here’s the basic skeleton:
class Dissolver extends JComponent implements Runnable {
Frame frame;
Window fullscreen;
int count;
BufferedImage frame_buffer;
BufferedImage screen_buffer;
public Dissolver( ) { }
Dissolver
is a
JComponent
that implements
Runnable
so that it can have an
animation loop. It has member variables for the application frame to dis-
solve (
frame), the window that covers up the screen (fullscreen), an anima-
tion counter (
count), and the two buffers for storing the frame and the
desktop background image (
frame_buffer and screen_buffer).
Prepare the Dissolve
Dissolver has one method to start the dissolve process called dissolveExit( ),
which takes the
JFrame you want to dissolve and generates everything else it
needs internally:
public void dissolveExit(JFrame frame) {
try {
this.frame = frame;
Robot robot = new Robot( );
// cap screen w/ frame to frame buffer
Rectangle frame_rect = frame.getBounds( );
frame_buffer = robot.createScreenCapture(frame_rect);
// hide frame
frame.setVisible(false);
// cap screen w/o frame
Dimension screensize = Toolkit.getDefaultToolkit( )
.getScreenSize( );
Rectangle screen_rect = new Rectangle(0,0,
screensize.width, screensize.height);
screen_buffer = robot.createScreenCapture(screen_rect);
// create big window w/o decorations
fullscreen = new Window(new JFrame( ));
fullscreen.setSize(screensize);
fullscreen.add(this);
this.setSize(screensize);
fullscreen.setVisible(true);

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