Class ReflectionRenderer


  • public class ReflectionRenderer
    extends java.lang.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:

    • 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.
    You can set these properties using the provided mutators 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:
    • 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.

    • Constructor Summary

      Constructors 
      Constructor Description
      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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addPropertyChangeListener​(java.beans.PropertyChangeListener listener)
      Add a PropertyChangeListener to the listener list.
      java.awt.image.BufferedImage appendReflection​(java.awt.image.BufferedImage image)
      Returns the source image and its reflection.
      java.awt.image.BufferedImage createReflection​(java.awt.image.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​(java.beans.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 java.lang.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 java.lang.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 java.lang.String BLUR_ENABLED_CHANGED_PROPERTY

        Identifies a change to the blurring of the rendered reflection.

        See Also:
        Constant Field Values
      • opacity

        private float opacity
      • length

        private float length
      • blurEnabled

        private boolean blurEnabled
      • changeSupport

        private java.beans.PropertyChangeSupport changeSupport
    • 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 listener is 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 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
      • 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 java.awt.image.BufferedImage appendReflection​(java.awt.image.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 java.awt.image.BufferedImage createReflection​(java.awt.image.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 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:
        appendReflection(java.awt.image.BufferedImage)