//java architect /
Java SE 7: Shaped and Translucent
Windows Deep Dive
COMMUNITY
Enhance your applications in new and interesting ways.
JOSH MARINACCI
JAVA IN ACTION
PHOTOGRAPH BY
CHRIS PIETSCH/GETTY IMAGES
One of the best new features of Java SE 7 is official support for shaped and transparent
windows. While shaped windows
have long been possible using
various tricks and hacks, operating systems have started to
support this feature natively in
recent years. Updates to Java SE 6
exposed this native functionality
through private APIs, but starting
with Java SE 7, it is now an official
part of Java SE.
With shaped and transparent windows, you can enhance
your applications in ways that
simply weren’t possible before.
This article takes you on a deep
dive into the technology and
shows you some advanced ways
to enhance your applications with
it. If you would like to code along
with me, download the full project
source code.
I will assume that you are
already familiar with the shaped
and translucent window concept and API. If not, Oracle has
a great tutorial on the topic. The
Java SE 7 API exposes three kinds
of window effects: shaped windows, transparent windows, and
windows with a full alpha channel. I won’t cover the first two
types because I have found their
usefulness to be limited, and
the most common use cases are
already covered in the standard
tutorials. Instead, I will focus on
the third kind: windows with a
full alpha channel.
ing effects, such as fuzzy edges,
translucent overlays, windows
with holes in them, and cool pop-down menus.
ABOUT US
Background
Working with a per-pixel alpha
channel is more powerful than
shaped and transparent windows because you can control
the transparency of each pixel
independently. A shaped window
is simply a nonrectangular window. All visible pixels are still
100 percent opaque, but the window might be circular or some
other funky shape. Translucent
windows set a single transparency value for the entire window,
which can be useful if you want to
fade the window all at once, but
it’s not useful for much beyond
that. The per-pixel alpha channel
is where the real magic is.
About Screen
Let’s start off with something
simple: an “about” screen with
fuzzy edges. This
could be the screen
that a user sees when
selecting the About
menu item from the
system menu of your
application. First, I
will create a Swing
component that
draws some text on
top of a translucent
gradient. The code is
pretty straightforward
Java 2-D code, which
fills a rectangle with a gradient
and then draws text on top, as
shown in Listing 1.
Notice that the gradient is
made of four separate colors and
each color has an alpha component (the last argument to the
While shaped
windows have
long been possible
using various tricks
and hacks, they are
now an official part of
Java SE 7.
blog
35
ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////////// MAY/JUNE 2012