close the pop-down menu. The code
in Listing 7 adds listeners to the main
frame to move or hide the pop-down
menu as needed.

Remember, when creating this sort of
custom window, we must always consider how the user will actually use it.

COMMUNITY

@Override
public void windowClosed(WindowEvent e) {
}

JAVA IN ACTION

Figure 4

class. This lets you merge multiple
shapes together. I created the pointy
tab shape by merging a triangle with a
rounded rectangle. This was far easier
than trying to write the Bezier curve
code for the shape directly. Then I filled
the shape with another nice translucent
gradient and added a stroked border to
make it stand out.

The pop-down menu looks great
and it doesn’t show up in the taskbar,
because I used a JWindow—but there is
still one problem. If the user chooses an
action, the pop-down menu will close,
but what happens if the user switches
to another application or minimizes the
main window? What if the user moves
the window? The pop-down menu will
still be there, just sort of hanging out in
space unattached to anything.

To fix this, I need to add an extra listener for window events. If the window
moves, we should move the pop-down
menu. If the window is hidden or is no
longer the active window, we should

Magnifying Glass

For our final example, let’s build something crazy that simply wasn’t possible
before: an onscreen magnifying glass.
This is a window that actually has a hole
in it. Anything inside the hole will be
magnified. To make it look cool, we will
use a fully custom window without any
window decorations, which means we
need another way for the user to move
it around.

First, let’s create the cool-looking
window. I drew the window using Adobe
Photoshop and saved it as a transparent
PNG file. See Figure 4.

Again, I’ll create a custom component
inside of an alpha channel window. I
also set the window to always be on top
so that I can use it when interacting with
other windows. Listing 8 shows what the
code looks like.

Now let’s make the magnification
actually work. To capture what’s inside
the smaller circle, we will use the java
. awt.Robot API. This API was created
mainly for doing testing and automation, but it has a very handy screen capture method that lets you specify a small
area of the screen to capture. Once we
have that small slice of screen, we will
draw it blown up by a factor of 8 in the
other circular area. Listing 9 shows an

excerpt of the full code available in the
architect.trans-windows-project.zip.

Now we have a magnifying glass, but
there are no window decorations, so
the user can’t move it. The most natural thing is to let the user click and drag
anywhere on the window to move the
magnifying glass. We can do this by
attaching a mouse listener to the main
component. Every time the user drags,
we will update the position of the window and then update the screen cap-