001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 */
028
029package org.jfree.chart.ui;
030
031import java.awt.GradientPaint;
032import java.awt.Shape;
033import java.awt.geom.Rectangle2D;
034import java.io.Serializable;
035import org.jfree.chart.util.PublicCloneable;
036
037/**
038 * Transforms a {@code GradientPaint} to range over the width of a target 
039 * shape.  Instances of this class are immutable.
040 */
041public class StandardGradientPaintTransformer 
042    implements GradientPaintTransformer, Cloneable, PublicCloneable, 
043               Serializable {
044    
045    /** For serialization. */
046    private static final long serialVersionUID = -8155025776964678320L;
047
048    /** The transform type. */
049    private GradientPaintTransformType type;
050    
051    /**
052     * Creates a new transformer with the type 
053     * {@link GradientPaintTransformType#VERTICAL}.
054     */
055    public StandardGradientPaintTransformer() {
056        this(GradientPaintTransformType.VERTICAL);
057    }
058    
059    /**
060     * Creates a new transformer with the specified type.
061     * 
062     * @param type  the transform type ({@code null} not permitted).
063     */
064    public StandardGradientPaintTransformer(
065            final GradientPaintTransformType type) {
066        if (type == null) {
067            throw new IllegalArgumentException("Null 'type' argument.");
068        }
069        this.type = type;
070    }
071    
072    /**
073     * Returns the type of transform.
074     * 
075     * @return The type of transform (never {@code null}).
076     */
077    public GradientPaintTransformType getType() {
078        return this.type;
079    }
080    
081    /**
082     * Transforms a {@code GradientPaint} instance to fit the specified
083     * {@code target} shape.
084     * 
085     * @param paint  the original paint ({@code null} not permitted).
086     * @param target  the target shape ({@code null} not permitted).
087     * 
088     * @return The transformed paint.
089     */
090    @Override
091    public GradientPaint transform(GradientPaint paint, Shape target) {
092        
093        GradientPaint result = paint;
094        Rectangle2D bounds = target.getBounds2D();
095        
096        if (this.type.equals(GradientPaintTransformType.VERTICAL)) {
097            result = new GradientPaint((float) bounds.getCenterX(), 
098                    (float) bounds.getMinY(), paint.getColor1(), 
099                    (float) bounds.getCenterX(), (float) bounds.getMaxY(), 
100                    paint.getColor2());
101        }
102        else if (this.type.equals(GradientPaintTransformType.HORIZONTAL)) {
103            result = new GradientPaint((float) bounds.getMinX(), 
104                    (float) bounds.getCenterY(), paint.getColor1(), 
105                    (float) bounds.getMaxX(), (float) bounds.getCenterY(), 
106                    paint.getColor2());            
107        }
108        else if (this.type.equals(
109                GradientPaintTransformType.CENTER_HORIZONTAL)) {
110            result = new GradientPaint((float) bounds.getCenterX(), 
111                    (float) bounds.getCenterY(), paint.getColor2(), 
112                    (float) bounds.getMaxX(), (float) bounds.getCenterY(), 
113                    paint.getColor1(), true);            
114        }
115        else if (this.type.equals(GradientPaintTransformType.CENTER_VERTICAL)) {
116            result = new GradientPaint((float) bounds.getCenterX(), 
117                    (float) bounds.getMinY(), paint.getColor1(), 
118                    (float) bounds.getCenterX(), (float) bounds.getCenterY(), 
119                    paint.getColor2(), true);            
120        }
121        
122        return result;
123    }
124    
125    /**
126     * Tests this instance for equality with an arbitrary object.
127     * 
128     * @param obj  the object ({@code null} permitted).
129     * 
130     * @return A boolean.
131     */
132    @Override
133    public boolean equals(Object obj) {
134        if (obj == this) {
135            return true;   
136        }
137        if (!(obj instanceof StandardGradientPaintTransformer)) {
138            return false;
139        }
140        StandardGradientPaintTransformer that 
141                = (StandardGradientPaintTransformer) obj;
142        if (this.type != that.type) {
143            return false;
144        }
145        return true;
146    }
147    
148    /**
149     * Returns a clone of the transformer.  Note that instances of this class
150     * are immutable, so cloning an instance isn't really necessary.
151     * 
152     * @return A clone.
153     * 
154     * @throws CloneNotSupportedException not thrown by this class, but 
155     *         subclasses (if any) might.
156     */
157    @Override
158    public Object clone() throws CloneNotSupportedException {
159        return super.clone();
160    }
161
162    /**
163     * Returns a hash code for this object.
164     * 
165     * @return A hash code.
166     */
167    @Override
168    public int hashCode() {
169        return (this.type != null ? this.type.hashCode() : 0);
170    }
171    
172}