
213
Chapter 6
C H A P T E R S I X
Transparent and Animated
Windows
Hacks 41–47
In the previous chapter, our window hacks generally played by the rules—
we simulated the earthquake dialog
[Hack #38] by animating calls to
setLocation( ), and switched to a mini-size window [Hack #40] by calling
setSize( ) and removing some window decorations.
This chapter’s hacks approach from outside the Window API per se, by
hacking the windows with Java 2D, stuffing things into the glass pane of a
JDialog or JFrame, and more. Some of them are practical, some are just
pretty, but all of these hacks offer something unexpected.
H A C K
#41
Transparent Windows Hack #41
Create translucent and shaped windows, while avoiding native code, with
clever use of a screenshot.
One of the most commonly requested Swing features is transparent win-
dows. Also called shaped windows, these are windows that have transparent
portions, allowing the desktop background and other programs to shine
through. Java doesn’t provide any way of creating transparent windows
without using the Java Native Interface (JNI) (and even then the native plat-
form must support transparency as well), but that’s not going to stop us. We
can cheat using one of my favorite techniques, the screenshot.
The process of faking a transparent window is basically:
1. Take a screenshot before the window is shown.
2. Use that screenshot as the background of the window.
3. Adjust ...