org.jdesktop.swingx.graphics
Class ReflectionRenderer

java.lang.Object
  extended by org.jdesktop.swingx.graphics.ReflectionRenderer

public class ReflectionRenderer
extends Object

A reflection renderer generates the reflection of a given picture. The result can be either the reflection itself, or an image containing both the source image and its reflection.

Reflection Properties

A reflection is defined by three properties:

You can set these properties using the provided mutaters or the appropriate constructor. Here are two ways of creating a blurred reflection, with an opacity of 50% and a length of 30% the height of the original image:
 ReflectionRenderer renderer = new ReflectionRenderer(0.5f, 0.3f, true);
 // ..
 renderer = new ReflectionRenderer();
 renderer.setOpacity(0.5f);
 renderer.setLength(0.3f);
 renderer.setBlurEnabled(true);
 
The default constructor provides the following default values:

Generating Reflections

A reflection is generated as a BufferedImage from another BufferedImage. Once the renderer is set up, you must call createReflection(java.awt.image.BufferedImage) to actually generate the reflection:

 ReflectionRenderer renderer = new ReflectionRenderer();
 // renderer setup
 BufferedImage reflection = renderer.createReflection(bufferedImage);
 

The returned image contains only the reflection. You will have to append it to the source image at painting time to get a realistic results. You can also asks the rendered to return a picture composed of both the source image and its reflection:

 ReflectionRenderer renderer = new ReflectionRenderer();
 // renderer setup
 BufferedImage reflection = renderer.appendReflection(bufferedImage);
 

Properties Changes

This renderer allows to register property change listeners with addPropertyChangeListener(java.beans.PropertyChangeListener). Listening to properties changes is very useful when you emebed the renderer in a graphical component and give the API user the ability to access the renderer. By listening to properties changes, you can easily repaint the component when needed.

Threading Issues

ReflectionRenderer is not guaranteed to be thread-safe.


Field Summary
static String BLUR_ENABLED_CHANGED_PROPERTY
          Identifies a change to the blurring of the rendered reflection.
static String LENGTH_CHANGED_PROPERTY
          Identifies a change to the length of the rendered reflection.
static String OPACITY_CHANGED_PROPERTY
          Identifies a change to the opacity used to render the reflection.
 
Constructor Summary
ReflectionRenderer()
          Creates a default good looking reflections generator.
ReflectionRenderer(float opacity)
          Creates a default good looking reflections generator with the specified opacity.
ReflectionRenderer(float opacity, float length, boolean blurEnabled)
          Creates a reflections generator with the specified properties.
 
Method Summary
 void addPropertyChangeListener(PropertyChangeListener listener)
          Add a PropertyChangeListener to the listener list.
 BufferedImage appendReflection(BufferedImage image)
          Returns the source image and its reflection.
 BufferedImage createReflection(BufferedImage image)
          Returns the reflection of the source image.
 int getBlurRadius()
          Returns the radius, in pixels, of the blur used by this renderer when isBlurEnabled() is true.
 int getEffectiveBlurRadius()
          Returns the effective radius, in pixels, of the blur used by this renderer when isBlurEnabled() is true.
 float getLength()
          Returns the length of the reflection.
 float getOpacity()
          Gets the opacity used by the factory to generate reflections.
 boolean isBlurEnabled()
          Returns true if the blurring of the reflection is enabled, false otherwise.
 void removePropertyChangeListener(PropertyChangeListener listener)
          Remove a PropertyChangeListener from the listener list.
 void setBlurEnabled(boolean blurEnabled)
          Setting the blur to true will enable the blurring of the reflection when createReflection(java.awt.image.BufferedImage) is invoked.
 void setBlurRadius(int radius)
          Sets the radius, in pixels, of the blur used by this renderer when isBlurEnabled() is true.
 void setLength(float length)
          Sets the length of the reflection, as a fraction of the height of the source image.
 void setOpacity(float opacity)
          Sets the opacity used by the factory to generate reflections.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OPACITY_CHANGED_PROPERTY

public static final String OPACITY_CHANGED_PROPERTY

Identifies a change to the opacity used to render the reflection.

See Also:
Constant Field Values

LENGTH_CHANGED_PROPERTY

public static final String LENGTH_CHANGED_PROPERTY

Identifies a change to the length of the rendered reflection.

See Also:
Constant Field Values

BLUR_ENABLED_CHANGED_PROPERTY

public static final String BLUR_ENABLED_CHANGED_PROPERTY

Identifies a change to the blurring of the rendered reflection.

See Also:
Constant Field Values
Constructor Detail

ReflectionRenderer

public ReflectionRenderer()

Creates a default good looking reflections generator. The default reflection renderer provides the following default values:

These properties provide a regular, good looking reflection.

See Also:
getOpacity(), setOpacity(float), getLength(), setLength(float), isBlurEnabled(), setBlurEnabled(boolean), getBlurRadius(), setBlurRadius(int)

ReflectionRenderer

public ReflectionRenderer(float opacity)

Creates a default good looking reflections generator with the specified opacity. The default reflection renderer provides the following default values:

Parameters:
opacity - the opacity of the reflection, between 0.0 and 1.0
See Also:
getOpacity(), setOpacity(float), getLength(), setLength(float), isBlurEnabled(), setBlurEnabled(boolean), getBlurRadius(), setBlurRadius(int)

ReflectionRenderer

public ReflectionRenderer(float opacity,
                          float length,
                          boolean blurEnabled)

Creates a reflections generator with the specified properties. Both opacity and length are numbers between 0.0 (0%) and 1.0 (100%). If the provided numbers are outside this range, they are clamped.

Enabling the blur generates a different kind of reflections that might look more natural. The default blur radius is 1 pixel

Parameters:
opacity - the opacity of the reflection
length - the length of the reflection
blurEnabled - if true, the reflection is blurred
See Also:
getOpacity(), setOpacity(float), getLength(), setLength(float), isBlurEnabled(), setBlurEnabled(boolean), getBlurRadius(), setBlurRadius(int)
Method Detail

addPropertyChangeListener

public void addPropertyChangeListener(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 listener is null, no exception is thrown and no action is taken.

Parameters:
listener - the PropertyChangeListener to be added

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)

Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties. If listener was added more than once to the same event source, it will be notified one less time after being removed. If listener is null, or was never added, no exception is thrown and no action is taken.

Parameters:
listener - the PropertyChangeListener to be removed

getOpacity

public float getOpacity()

Gets the opacity used by the factory to generate reflections.

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
See Also:
getOpacity(), createReflection(java.awt.image.BufferedImage), appendReflection(java.awt.image.BufferedImage)

setOpacity

public void setOpacity(float opacity)

Sets the opacity used by the factory to generate reflections.

Consecutive calls to createReflection(java.awt.image.BufferedImage) will all use this opacity 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:
opacity - the generated reflection opacity
See Also:
setOpacity(float), createReflection(java.awt.image.BufferedImage), appendReflection(java.awt.image.BufferedImage)

getLength

public float getLength()

Returns the length of the reflection. The result is a number between 0.0 and 1.0. This number is the fraction of the height of the source image that is used to compute the size of the reflection.

Returns:
the length of the reflection, as a fraction of the source image height
See Also:
setLength(float), createReflection(java.awt.image.BufferedImage), appendReflection(java.awt.image.BufferedImage)

setLength

public void setLength(float length)

Sets the length of the reflection, as a fraction of the height of the source image.

Consecutive calls to createReflection(java.awt.image.BufferedImage) will all use this opacity 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:
length - the length of the reflection, as a fraction of the source image height
See Also:
getLength(), createReflection(java.awt.image.BufferedImage), appendReflection(java.awt.image.BufferedImage)

isBlurEnabled

public boolean isBlurEnabled()

Returns true if the blurring of the reflection is enabled, false otherwise. When blurring is enabled, the reflection is blurred to look more natural.

Returns:
true if blur is enabled, false otherwise
See Also:
setBlurEnabled(boolean), createReflection(java.awt.image.BufferedImage), appendReflection(java.awt.image.BufferedImage)

setBlurEnabled

public void setBlurEnabled(boolean blurEnabled)

Setting the blur to true will enable the blurring of the reflection when createReflection(java.awt.image.BufferedImage) is invoked.

Enabling the blurring of the reflection can yield to more natural results which may or may not be better looking, depending on the source picture.

Consecutive calls to createReflection(java.awt.image.BufferedImage) will all use this opacity until it is set again.

Parameters:
blurEnabled - true to enable the blur, false otherwise
See Also:
isBlurEnabled(), createReflection(java.awt.image.BufferedImage), appendReflection(java.awt.image.BufferedImage)

getEffectiveBlurRadius

public int getEffectiveBlurRadius()

Returns the effective radius, in pixels, of the blur used by this renderer when isBlurEnabled() is true.

Returns:
the effective radius of the blur used when isBlurEnabled is true
See Also:
isBlurEnabled(), setBlurEnabled(boolean), setBlurRadius(int), getBlurRadius()

getBlurRadius

public int getBlurRadius()

Returns the radius, in pixels, of the blur used by this renderer when isBlurEnabled() is true.

Returns:
the radius of the blur used when isBlurEnabled is true
See Also:
isBlurEnabled(), setBlurEnabled(boolean), setBlurRadius(int), getEffectiveBlurRadius()

setBlurRadius

public void setBlurRadius(int radius)

Sets the radius, in pixels, of the blur used by this renderer when isBlurEnabled() is true. This radius changes the size of the generated image when blurring is applied.

Parameters:
radius - the radius, in pixels, of the blur
See Also:
isBlurEnabled(), setBlurEnabled(boolean), getBlurRadius()

appendReflection

public BufferedImage appendReflection(BufferedImage image)

Returns the source image and its reflection. The appearance of the reflection is defined by the opacity, the length and the blur properties.

*

The width of the generated image will be augmented when isBlurEnabled() is true. The generated image will have the width of the source image plus twice the effective blur radius (see getEffectiveBlurRadius()). The default blur radius is 1 so the width will be augmented by 6. You might need to take this into account at drawing time.

The returned image height depends on the value returned by getLength() and getEffectiveBlurRadius(). For instance, if the length is 0.5 (or 50%) and the source image is 480 pixels high, then the reflection will be 246 (480 * 0.5 + 3 * 2) pixels high.

You can create only the reflection by calling createReflection(java.awt.image.BufferedImage).

Parameters:
image - the source image
Returns:
the source image with its reflection below
See Also:
createReflection(java.awt.image.BufferedImage)

createReflection

public BufferedImage createReflection(BufferedImage image)

Returns the reflection of the source image. The appearance of the reflection is defined by the opacity, the length and the blur properties.

*

The width of the generated image will be augmented when isBlurEnabled() is true. The generated image will have the width of the source image plus twice the effective blur radius (see getEffectiveBlurRadius()). The default blur radius is 1 so the width will be augmented by 6. You might need to take this into account at drawing time.

The returned image height depends on the value returned by getLength() and getEffectiveBlurRadius(). For instance, if the length is 0.5 (or 50%) and the source image is 480 pixels high, then the reflection will be 246 (480 * 0.5 + 3 * 2) pixels high.

The returned image contains only the reflection. You will have to append it to the source image to produce the illusion of a reflective environement. The method appendReflection(java.awt.image.BufferedImage) provides an easy way to create an image containing both the source and the reflection.

Parameters:
image - the source image
Returns:
the reflection of the source image
See Also:
appendReflection(java.awt.image.BufferedImage)