A shadow factory generates a drop shadow for any given picture, respecting
the transparency channel if present. The resulting picture contains the
shadow only and to create a drop shadow effect you will need to stack the
original picture and the shadow generated by the factory.
Shadow Properties
A shadow is defined by three properties:
- size: The size, in pixels, of the shadow. This property also
defines the fuzzyness.
- opacity: The opacity, between 0.0 and 1.0, of the shadow.
- color: The color of the shadow. Shadows are not meant to be
black only.
You can set these properties using the provided mutaters or the appropriate
constructor. Here are two ways of creating a green shadow of size 10 and
with an opacity of 50%:
ShadowFactory factory = new ShadowFactory(10, 0.5f, Color.GREEN);
// ..
factory = new ShadowFactory();
factory.setSize(10);
factory.setOpacity(0.5f);
factory.setColor(Color.GREEN);
The default constructor provides the following default values:
- size: 5 pixels
- opacity: 50%
- color: Black
Shadow Quality
The factory provides two shadow generation algorithms: fast quality blur
and high quality blur. You can select your preferred algorithm by
setting the appropriate rendering hint:
ShadowFactory factory = new ShadowFactory();
factory.setRenderingHint(ShadowFactory.KEY_BLUR_QUALITY,
ShadowFactory.VALUE_BLUR_QUALITY_HIGH);
The default rendering algorihtm is VALUE_BLUR_QUALITY_FAST .
The current implementation should provide the same quality with both
algorithms but performances are guaranteed to be better (about 30 times
faster) with the fast quality blur.
Generating a Shadow
A shadow is generated as a BufferedImage from another
BufferedImage . Once the factory is set up, you must call
ShadowFactory.createShadow to actually generate the shadow:
ShadowFactory factory = new ShadowFactory();
// factory setup
BufferedImage shadow = factory.createShadow(bufferedImage);
The resulting image is of type BufferedImage.TYPE_INT_ARGB .
Both dimensions of this image are larger than original image's:
- new width = original width + 2 * shadow size
- new height = original height + 2 * shadow size
This must be taken into account when you need to create a drop shadow effect.
Properties Changes
This factory allows to register property change listeners with
ShadowFactory.addPropertyChangeListener . Listening to properties changes is very
useful when you emebed the factory in a graphical component and give the API
user the ability to access the factory. By listening to properties changes,
you can easily repaint the component when needed.
Threading Issues
ShadowFactory is not guaranteed to be thread-safe.
author: Romain Guy author: Sebastien Petrucci |