Class ReflectionRenderer
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:
- opacity: the opacity of the reflection. You will usually change this valued according to the background color.
- length: the length of the reflection. The length is a fraction of the height of the source image.
- blur enabled: perfect reflections are hardly natural. You can blur the reflection to make it look a bit more natural.
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:
- opacity: 35%
- length: 40%
- blur enabled: false
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 embed 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
FieldsModifier and TypeFieldDescriptionstatic final String
Identifies a change to the blurring of the rendered reflection.private boolean
private PropertyChangeSupport
private float
static final String
Identifies a change to the length of the rendered reflection.private float
static final String
Identifies a change to the opacity used to render the reflection.private StackBlurFilter
-
Constructor Summary
ConstructorsConstructorDescriptionCreates 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
Modifier and TypeMethodDescriptionvoid
Add a PropertyChangeListener to the listener list.appendReflection
(BufferedImage image) Returns the source image and its reflection.createReflection
(BufferedImage image) Returns the reflection of the source image.int
Returns the radius, in pixels, of the blur used by this renderer whenisBlurEnabled()
is true.int
Returns the effective radius, in pixels, of the blur used by this renderer whenisBlurEnabled()
is true.float
Returns the length of the reflection.float
Gets the opacity used by the factory to generate reflections.boolean
Returns true if the blurring of the reflection is enabled, false otherwise.void
Remove a PropertyChangeListener from the listener list.void
setBlurEnabled
(boolean blurEnabled) Setting the blur to true will enable the blurring of the reflection whencreateReflection(java.awt.image.BufferedImage)
is invoked.void
setBlurRadius
(int radius) Sets the radius, in pixels, of the blur used by this renderer whenisBlurEnabled()
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.
-
Field Details
-
OPACITY_CHANGED_PROPERTY
Identifies a change to the opacity used to render the reflection.
- See Also:
-
LENGTH_CHANGED_PROPERTY
Identifies a change to the length of the rendered reflection.
- See Also:
-
BLUR_ENABLED_CHANGED_PROPERTY
Identifies a change to the blurring of the rendered reflection.
- See Also:
-
opacity
private float opacity -
length
private float length -
blurEnabled
private boolean blurEnabled -
changeSupport
-
stackBlurFilter
-
-
Constructor Details
-
ReflectionRenderer
public ReflectionRenderer()Creates a default good looking reflections generator. The default reflection renderer provides the following default values:
- opacity: 35%
- length: 40%
- blurring: disabled with a radius of 1 pixel
These properties provide a regular, good looking reflection.
- See Also:
-
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:
- length: 40%
- blurring: disabled with a radius of 1 pixel
- Parameters:
opacity
- the opacity of the reflection, between 0.0 and 1.0- See Also:
-
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 reflectionlength
- the length of the reflectionblurEnabled
- if true, the reflection is blurred- See Also:
-
-
Method Details
-
addPropertyChangeListener
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
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. Iflistener
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:
-
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:
-
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
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:
-
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
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:
-
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:
-
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:
-
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:
-
appendReflection
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 (seegetEffectiveBlurRadius()
). 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()
andgetEffectiveBlurRadius()
. 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
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 (seegetEffectiveBlurRadius()
). 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()
andgetEffectiveBlurRadius()
. 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 environment. 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:
-