Class ShadowFactory
- java.lang.Object
-
- com.jidesoft.swing.ShadowFactory
-
- All Implemented Interfaces:
ShadowRenderer
public class ShadowFactory extends java.lang.Object implements ShadowRenderer
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.
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 algorithm isVALUE_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
BufferedImagefrom anotherBufferedImage. Once the factory is set up, you must callcreateShadow(java.awt.image.BufferedImage)to actually generate the shadow:ShadowFactory factory = new ShadowFactory(); // factory setup BufferedImage shadow = factory.createShadow(bufferedImage);
The resulting image is of typeBufferedImage.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
Properties Changes
This factory allows to register property change listeners with
addPropertyChangeListener(java.beans.PropertyChangeListener). Listening to properties changes is very useful when you embed 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
ShadowFactoryis not guaranteed to be thread-safe.- Author:
- Romain Guy
, Sebastien Petrucci
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCOLOR_CHANGED_PROPERTYIdentifies a change to the color used to render the shadow.static java.lang.StringKEY_BLUR_QUALITYKey for the blur quality rendering hint.static java.lang.StringOPACITY_CHANGED_PROPERTYIdentifies a change to the opacity used to render the shadow.static java.lang.StringSIZE_CHANGED_PROPERTYIdentifies a change to the size used to render the shadow.static java.lang.StringVALUE_BLUR_QUALITY_FASTSelects the fast rendering algorithm.static java.lang.StringVALUE_BLUR_QUALITY_HIGHSelects the high quality rendering algorithm.
-
Constructor Summary
Constructors Constructor Description ShadowFactory()Creates a default good looking shadow generator.ShadowFactory(int size, float opacity, java.awt.Color color)A shadow factory needs three properties to generate shadows.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddPropertyChangeListener(java.beans.PropertyChangeListener listener)Add a PropertyChangeListener to the listener list.java.awt.image.BufferedImagecreateShadow(java.awt.image.BufferedImage image)Generates the shadow for a given picture and the current properties of the factory.java.awt.ColorgetColor()Gets the color used by the factory to generate shadows.floatgetOpacity()Gets the opacity used by the factory to generate shadows.intgetSize()Gets the size in pixel used by the factory to generate shadows.voidremovePropertyChangeListener(java.beans.PropertyChangeListener listener)Remove a PropertyChangeListener from the listener list.voidsetColor(java.awt.Color shadowColor)Sets the color used by the factory to generate shadows.voidsetOpacity(float shadowOpacity)Sets the opacity used by the factory to generate shadows.voidsetRenderingHint(java.lang.Object key, java.lang.Object value)Maps the specified rendering hintkeyto the specifiedvaluein thisSahdowFactoryobject.voidsetSize(int shadowSize)Sets the size, in pixels, used by the factory to generate shadows.
-
-
-
Field Detail
-
KEY_BLUR_QUALITY
public static final java.lang.String KEY_BLUR_QUALITY
Key for the blur quality rendering hint.
- See Also:
- Constant Field Values
-
VALUE_BLUR_QUALITY_FAST
public static final java.lang.String VALUE_BLUR_QUALITY_FAST
Selects the fast rendering algorithm. This is the default rendering hint for
KEY_BLUR_QUALITY.- See Also:
- Constant Field Values
-
VALUE_BLUR_QUALITY_HIGH
public static final java.lang.String VALUE_BLUR_QUALITY_HIGH
Selects the high quality rendering algorithm. With current implementation, This algorithm does not guarantee a better rendering quality and should not be used.
- See Also:
- Constant Field Values
-
SIZE_CHANGED_PROPERTY
public static final java.lang.String SIZE_CHANGED_PROPERTY
Identifies a change to the size used to render the shadow.
When the property change event is fired, the old value and the new value are provided as
Integerinstances.- See Also:
- Constant Field Values
-
OPACITY_CHANGED_PROPERTY
public static final java.lang.String OPACITY_CHANGED_PROPERTY
Identifies a change to the opacity used to render the shadow.
When the property change event is fired, the old value and the new value are provided as
Floatinstances.- See Also:
- Constant Field Values
-
COLOR_CHANGED_PROPERTY
public static final java.lang.String COLOR_CHANGED_PROPERTY
Identifies a change to the color used to render the shadow.
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ShadowFactory
public ShadowFactory()
Creates a default good looking shadow generator. The default shadow factory provides the following default values:
- size: 5 pixels
- opacity: 50%
- color: Black
- rendering quality: VALUE_BLUR_QUALITY_FAST
These properties provide a regular, good looking shadow.
-
ShadowFactory
public ShadowFactory(int size, float opacity, java.awt.Color color)A shadow factory needs three properties to generate shadows. These properties are:
- 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.
Besides these properties you can set rendering hints to control the rendering process. The default rendering hints let the factory use the fastest shadow generation algorithm.
- Parameters:
size- The size of the shadow in pixels. Defines the fuzziness.opacity- The opacity of the shadow.color- The color of the shadow.- See Also:
setRenderingHint(Object, Object)
-
-
Method Detail
-
addPropertyChangeListener
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Add a PropertyChangeListener to the listener list. The listener is registered for all properties. The same listener object may be added more than once, and will be called as many times as it is added. If
listeneris null, no exception is thrown and no action is taken.- Parameters:
listener- the PropertyChangeListener to be added
-
removePropertyChangeListener
public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties. If
listenerwas added more than once to the same event source, it will be notified one less time after being removed. Iflisteneris null, or was never added, no exception is thrown and no action is taken.- Parameters:
listener-
-
setRenderingHint
public void setRenderingHint(java.lang.Object key, java.lang.Object value)Maps the specified rendering hint
keyto the specifiedvaluein thisSahdowFactoryobject.- Parameters:
key- The rendering hint keyvalue- The rendering hint value
-
getColor
public java.awt.Color getColor()
Gets the color used by the factory to generate shadows.
- Returns:
- this factory's shadow color
-
setColor
public void setColor(java.awt.Color shadowColor)
Sets the color used by the factory to generate shadows.
Consecutive calls to
createShadow(java.awt.image.BufferedImage)will all use this color until it is set again.If the color provided is null, the previous color will be retained.
- Parameters:
shadowColor- the generated shadows color
-
getOpacity
public float getOpacity()
Gets the opacity used by the factory to generate shadows.
The opacity is comprised between 0.0f and 1.0f; 0.0f being fully transparent and 1.0f fully opaque.
- Returns:
- this factory's shadow opacity
-
setOpacity
public void setOpacity(float shadowOpacity)
Sets the opacity used by the factory to generate shadows.
Consecutive calls to
createShadow(java.awt.image.BufferedImage)will all use this color until it is set again.The opacity is comprised between 0.0f and 1.0f; 0.0f being fully transparent and 1.0f fully opaque. If you provide a value out of these boundaries, it will be restrained to the closest boundary.
- Parameters:
shadowOpacity- the generated shadows opacity
-
getSize
public int getSize()
Gets the size in pixel used by the factory to generate shadows.
- Returns:
- this factory's shadow size
-
setSize
public void setSize(int shadowSize)
Sets the size, in pixels, used by the factory to generate shadows.
The size defines the blur radius applied to the shadow to create the fuzziness.
There is virtually no limit to the size but it has an impact on shadow generation performances. The greater this value, the longer it will take to generate the shadow. Remember the generated shadow image dimensions are computed as follow:
- new width = original width + 2 * shadow size
- new height = original height + 2 * shadow size
- Parameters:
shadowSize- the generated shadows size in pixels (fuzziness)
-
createShadow
public java.awt.image.BufferedImage createShadow(java.awt.image.BufferedImage image)
Generates the shadow for a given picture and the current properties of the factory.
The generated shadow image dimensions are computed as follow:
- new width = original width + 2 * shadow size
- new height = original height + 2 * shadow size
The time taken by a call to this method depends on the size of the shadow, the larger the longer it takes, and on the selected rendering algorithm.
- Specified by:
createShadowin interfaceShadowRenderer- Parameters:
image- the picture from which the shadow must be cast- Returns:
- the picture containing the shadow of
image
-
-