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);

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.