
Turn the Spotlight on Swing #64
Chapter 8, Rendering
|
329
HACK
The next step is to draw the shape containing the spotlights. Before doing
that, though, you need to compute the opacity of
mask. When you create a
new
Color
instance, you can define the opacity as a number between
0.0
(fully transparent), and
1.0
(fully opaque). To compute the required opac-
ity, the program first divides the area of the spotlights by the total area.
Thereby, if spotlights occupy 20% of the total area, you obtain the value 0.2.
Subtract this from 1.0 to compute the final opacity. In this example, the
opacity would be 0.8, or 80%.
The final step of the rendering depends on whether blurring is activated.
When blurring is off, the
mask is simply filled with shieldColor. Note that
the rendering hints defined in the constructor are used at this moment to
draw anti-aliased shapes. If blurring is activated, an extra step is required.
Instead of drawing the spotlights directly onto the glass pane’s graphics sur-
face, an offscreen buffer is created, with the same size as the panel itself.
After having drawn
mask on this offscreen buffer, the resulting picture is
drawn onto the panel’s graphics surface. All the magic happens when you
call the
drawImage( ) method with a ConvolveOp as the second parameter. The
method applies our blur filter on the picture while drawing it.
—Romain Guy