Class ShadowRenderer


  • public class ShadowRenderer
    extends java.lang.Object

    A shadow renderer 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 renderer.

    Shadow Properties

    A shadow is defined by three properties:

    • size: The size, in pixels, of the shadow. This property also defines the fuzziness.
    • 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 mutators or the appropriate constructor. Here are two ways of creating a green shadow of size 10 and with an opacity of 50%:
     ShadowRenderer renderer = new ShadowRenderer(10, 0.5f, Color.GREEN);
     // ..
     renderer = new ShadowRenderer();
     renderer.setSize(10);
     renderer.setOpacity(0.5f);
     renderer.setColor(Color.GREEN);
     
    The default constructor provides the following default values:
    • size: 5 pixels
    • opacity: 50%
    • color: Black

    Generating a Shadow

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

     ShadowRenderer renderer = new ShadowRenderer();
     // renderer setup
     BufferedImage shadow = renderer.createShadow(bufferedImage);
     

    The generated image dimensions are computed as following:

     width  = imageWidth  + 2 * shadowSize
     height = imageHeight + 2 * shadowSize
     

    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

    ShadowRenderer is not guaranteed to be thread-safe.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String COLOR_CHANGED_PROPERTY
      Identifies a change to the color used to render the shadow.
      static java.lang.String OPACITY_CHANGED_PROPERTY
      Identifies a change to the opacity used to render the shadow.
      static java.lang.String SIZE_CHANGED_PROPERTY
      Identifies a change to the size used to render the shadow.
    • Constructor Summary

      Constructors 
      Constructor Description
      ShadowRenderer()
      Creates a default good looking shadow generator.
      ShadowRenderer​(int size, float opacity, java.awt.Color color)
      A shadow renderer needs three properties to generate shadows.
    • 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 createShadow​(java.awt.image.BufferedImage image)
      Generates the shadow for a given picture and the current properties of the renderer.
      java.awt.Color getColor()
      Gets the color used by the renderer to generate shadows.
      float getOpacity()
      Gets the opacity used by the renderer to generate shadows.
      int getSize()
      Gets the size in pixel used by the renderer to generate shadows.
      void removePropertyChangeListener​(java.beans.PropertyChangeListener listener)
      Remove a PropertyChangeListener from the listener list.
      void setColor​(java.awt.Color shadowColor)
      Sets the color used by the renderer to generate shadows.
      void setOpacity​(float shadowOpacity)
      Sets the opacity used by the renderer to generate shadows.
      void setSize​(int shadowSize)
      Sets the size, in pixels, used by the renderer to generate shadows.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • 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 Integer instances.

        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 Float instances.

        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

      • ShadowRenderer

        public ShadowRenderer()

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

        • size: 5 pixels
        • opacity: 50%
        • color: Black

        These properties provide a regular, good looking shadow.

      • ShadowRenderer

        public ShadowRenderer​(int size,
                              float opacity,
                              java.awt.Color color)

        A shadow renderer needs three properties to generate shadows. These properties are:

        • size: The size, in pixels, of the shadow. This property also defines the fuzziness.
        • 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.
        Parameters:
        size - the size of the shadow in pixels. Defines the fuzziness.
        opacity - the opacity of the shadow.
        color - the color of the shadow.
    • 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
      • getColor

        public java.awt.Color getColor()

        Gets the color used by the renderer to generate shadows.

        Returns:
        this renderer's shadow color
      • setColor

        public void setColor​(java.awt.Color shadowColor)

        Sets the color used by the renderer 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 renderer 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 renderer's shadow opacity
      • setOpacity

        public void setOpacity​(float shadowOpacity)

        Sets the opacity used by the renderer to generate shadows.

        Consecutive calls to createShadow(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:
        shadowOpacity - the generated shadows opacity
      • getSize

        public int getSize()

        Gets the size in pixel used by the renderer to generate shadows.

        Returns:
        this renderer's shadow size
      • setSize

        public void setSize​(int shadowSize)

        Sets the size, in pixels, used by the renderer 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. The size cannot be negative. If you provide a negative value, the size will be 0 instead.

        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 renderer.

        The generated image dimensions are computed as following:

         width  = imageWidth  + 2 * shadowSize
         height = imageHeight + 2 * shadowSize
         
        Parameters:
        image - the picture from which the shadow must be cast
        Returns:
        the picture containing the shadow of image