001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, by Object Refinery Limited 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     * @since 1.0.10
078     */
079    public GradientPaintTransformType getType() {
080        return this.type;
081    }
082    
083    /**
084     * Transforms a {@code GradientPaint} instance to fit the specified
085     * {@code target} shape.
086     * 
087     * @param paint  the original paint ({@code null} not permitted).
088     * @param target  the target shape ({@code null} not permitted).
089     * 
090     * @return The transformed paint.
091     */
092    @Override
093    public GradientPaint transform(GradientPaint paint, Shape target) {
094        
095        GradientPaint result = paint;
096        Rectangle2D bounds = target.getBounds2D();
097        
098        if (this.type.equals(GradientPaintTransformType.VERTICAL)) {
099            result = new GradientPaint((float) bounds.getCenterX(), 
100                    (float) bounds.getMinY(), paint.getColor1(), 
101                    (float) bounds.getCenterX(), (float) bounds.getMaxY(), 
102                    paint.getColor2());
103        }
104        else if (this.type.equals(GradientPaintTransformType.HORIZONTAL)) {
105            result = new GradientPaint((float) bounds.getMinX(), 
106                    (float) bounds.getCenterY(), paint.getColor1(), 
107                    (float) bounds.getMaxX(), (float) bounds.getCenterY(), 
108                    paint.getColor2());            
109        }
110        else if (this.type.equals(
111                GradientPaintTransformType.CENTER_HORIZONTAL)) {
112            result = new GradientPaint((float) bounds.getCenterX(), 
113                    (float) bounds.getCenterY(), paint.getColor2(), 
114                    (float) bounds.getMaxX(), (float) bounds.getCenterY(), 
115                    paint.getColor1(), true);            
116        }
117        else if (this.type.equals(GradientPaintTransformType.CENTER_VERTICAL)) {
118            result = new GradientPaint((float) bounds.getCenterX(), 
119                    (float) bounds.getMinY(), paint.getColor1(), 
120                    (float) bounds.getCenterX(), (float) bounds.getCenterY(), 
121                    paint.getColor2(), true);            
122        }
123        
124        return result;
125    }
126    
127    /**
128     * Tests this instance for equality with an arbitrary object.
129     * 
130     * @param obj  the object ({@code null} permitted).
131     * 
132     * @return A boolean.
133     */
134    @Override
135    public boolean equals(Object obj) {
136        if (obj == this) {
137            return true;   
138        }
139        if (!(obj instanceof StandardGradientPaintTransformer)) {
140            return false;
141        }
142        StandardGradientPaintTransformer that 
143                = (StandardGradientPaintTransformer) obj;
144        if (this.type != that.type) {
145            return false;
146        }
147        return true;
148    }
149    
150    /**
151     * Returns a clone of the transformer.  Note that instances of this class
152     * are immutable, so cloning an instance isn't really necessary.
153     * 
154     * @return A clone.
155     * 
156     * @throws CloneNotSupportedException not thrown by this class, but 
157     *         subclasses (if any) might.
158     */
159    @Override
160    public Object clone() throws CloneNotSupportedException {
161        return super.clone();
162    }
163
164    /**
165     * Returns a hash code for this object.
166     * 
167     * @return A hash code.
168     */
169    @Override
170    public int hashCode() {
171        return (this.type != null ? this.type.hashCode() : 0);
172    }
173    
174}