📄 reflectionrenderer.java
字号:
/*
* Copyright (c) 2007, Romain Guy
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the TimingFramework project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
/**
* <p>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.</p>
* <h2>Reflection Properties</h2>
* <p>A reflection is defined by three properties:
* <ul>
* <li><i>opacity</i>: the opacity of the reflection. You will usually
* change this valued according to the background color.</li>
* <li><i>length</i>: the length of the reflection. The length is a fraction
* of the height of the source image.</li>
* <li><i>blur enabled</i>: perfect reflections are hardly natural. You can
* blur the reflection to make it look a bit more natural.</li>
* </ul>
* 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:
* <pre>
* ReflectionRenderer renderer = new ReflectionRenderer(0.5f, 0.3f, true);
* // ..
* renderer = new ReflectionRenderer();
* renderer.setOpacity(0.5f);
* renderer.setLength(0.3f);
* renderer.setBlurEnabled(true);
* </pre>
* The default constructor provides the following default values:
* <ul>
* <li><i>opacity</i>: 35%</li>
* <li><i>length</i>: 40%</li>
* <li><i>blur enabled</i>: false</li>
* </ul></p>
* <h2>Generating Reflections</h2>
* <p>A reflection is generated as a <code>BufferedImage</code> from another
* <code>BufferedImage</code>. Once the renderer is set up, you must call
* {@link #createReflection(java.awt.image.BufferedImage)} to actually generate
* the reflection:
* <pre>
* ReflectionRenderer renderer = new ReflectionRenderer();
* // renderer setup
* BufferedImage reflection = renderer.createReflection(bufferedImage);
* </pre></p>
* <p>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:
* <pre>
* ReflectionRenderer renderer = new ReflectionRenderer();
* // renderer setup
* BufferedImage reflection = renderer.appendReflection(bufferedImage);
* </pre></p>
* <h2>Properties Changes</h2>
* <p>This renderer allows to register property change listeners with
* {@link #addPropertyChangeListener}. 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.</p>
* <h2>Threading Issues</h2>
* <p><code>ReflectionRenderer</code> is not guaranteed to be thread-safe.</p>
*
* @author Romain Guy <romain.guy@mac.com>
*/
public class ReflectionRenderer {
/**
* <p>Identifies a change to the opacity used to render the reflection.</p>
*/
public static final String OPACITY_CHANGED_PROPERTY = "reflection_opacity";
/**
* <p>Identifies a change to the length of the rendered reflection.</p>
*/
public static final String LENGTH_CHANGED_PROPERTY = "reflection_length";
/**
* <p>Identifies a change to the blurring of the rendered reflection.</p>
*/
public static final String BLUR_ENABLED_CHANGED_PROPERTY = "reflection_blur";
// opacity of the reflection
private float opacity;
// length of the reflection
private float length;
// should the reflection be blurred?
private boolean blurEnabled;
// notifies listeners of properties changes
private PropertyChangeSupport changeSupport;
private StackBlurFilter stackBlurFilter;
/**
* <p>Creates a default good looking reflections generator.
* The default reflection renderer provides the following default values:
* <ul>
* <li><i>opacity</i>: 35%</li>
* <li><i>length</i>: 40%</li>
* <li><i>blurring</i>: disabled with a radius of 1 pixel</li>
* </ul></p>
* <p>These properties provide a regular, good looking reflection.</p>
*
* @see #getOpacity()
* @see #setOpacity(float)
* @see #getLength()
* @see #setLength(float)
* @see #isBlurEnabled()
* @see #setBlurEnabled(boolean)
* @see #getBlurRadius()
* @see #setBlurRadius(int)
*/
public ReflectionRenderer() {
this(0.35f, 0.4f, false);
}
/**
* <p>Creates a default good looking reflections generator with the
* specified opacity. The default reflection renderer provides the following
* default values:
* <ul>
* <li><i>length</i>: 40%</li>
* <li><i>blurring</i>: disabled with a radius of 1 pixel</li>
* </ul></p>
*
* @param opacity the opacity of the reflection, between 0.0 and 1.0
* @see #getOpacity()
* @see #setOpacity(float)
* @see #getLength()
* @see #setLength(float)
* @see #isBlurEnabled()
* @see #setBlurEnabled(boolean)
* @see #getBlurRadius()
* @see #setBlurRadius(int)
*/
public ReflectionRenderer(float opacity) {
this(opacity, 0.4f, false);
}
/**
* <p>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.</p>
* <p>Enabling the blur generates a different kind of reflections that might
* look more natural. The default blur radius is 1 pixel</p>
*
* @param opacity the opacity of the reflection
* @param length the length of the reflection
* @param blurEnabled if true, the reflection is blurred
* @see #getOpacity(),#setOpacity(float),#getLength(),#setLength(float)
* @see #isBlurEnabled(),#setBlurEnabled(boolean)
* @see #getBlurRadius()
* @see #setBlurRadius(int)
*/
public ReflectionRenderer(float opacity, float length, boolean blurEnabled) {
//noinspection ThisEscapedInObjectConstruction
this.changeSupport = new PropertyChangeSupport(this);
this.stackBlurFilter = new StackBlurFilter(1);
setOpacity(opacity);
setLength(length);
setBlurEnabled(blurEnabled);
}
/**
* <p>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
* <code>listener</code> is null, no exception is thrown and no action
* is taken.</p>
*
* @param listener the PropertyChangeListener to be added
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
/**
* <p>Remove a PropertyChangeListener from the listener list. This removes
* a PropertyChangeListener that was registered for all properties. If
* <code>listener</code> was added more than once to the same event source,
* it will be notified one less time after being removed. If
* <code>listener</code> is null, or was never added, no exception is thrown
* and no action is taken.</p>
*
* @param listener the PropertyChangeListener to be removed
*/
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
/**
* <p>Gets the opacity used by the factory to generate reflections.</p>
* <p>The opacity is comprised between 0.0f and 1.0f; 0.0f being fully
* transparent and 1.0f fully opaque.</p>
*
* @return this factory's shadow opacity
* @see #getOpacity()
* @see #createReflection(java.awt.image.BufferedImage)
* @see #appendReflection(java.awt.image.BufferedImage)
*/
public float getOpacity() {
return opacity;
}
/**
* <p>Sets the opacity used by the factory to generate reflections.</p>
* <p>Consecutive calls to {@link #createReflection} will all use this
* opacity until it is set again.</p>
* <p>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.</p>
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -