    001    /* ========================================================================
    002     * JCommon : a free general purpose class library for the Java(tm) platform
    003     * ========================================================================
    004     *
    005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jcommon/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     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * -------------------------------------
    028     * StandardGradientPaintTransformer.java
    029     * -------------------------------------
    030     * (C) Copyright 2003-2005, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: StandardGradientPaintTransformer.java,v 1.8 2005/10/18 13:18:34 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 28-Oct-2003 : Version 1 (DG);
    040     * 19-Mar-2004 : Added equals() method (DG);
    041     *
    042     */
    043
    044    package org.jfree.ui;
    045
    046    import java.awt.GradientPaint;
    047    import java.awt.Shape;
    048    import java.awt.geom.Rectangle2D;
    049    import java.io.Serializable;
    050
    051    import org.jfree.util.PublicCloneable;
    052
    053    /**
    054     * Transforms a <code>GradientPaint</code> to range over the width of a target
    055     * shape.
    056     * @author David Gilbert
    057     */
    058    public class StandardGradientPaintTransformer
    059        implements GradientPaintTransformer, Cloneable, PublicCloneable,
    060                   Serializable {
    061
    062        /** For serialization. */
    063        private static final long serialVersionUID = -8155025776964678320L;
    064
    065        /** The transform type. */
    066        private GradientPaintTransformType type;
    067
    068        /**
    069         * Creates a new transformer.
    070         */
    071        public StandardGradientPaintTransformer() {
    072            this(GradientPaintTransformType.VERTICAL);
    073        }
    074
    075        /**
    076         * Creates a new transformer.
    077         *
    078         * @param type  the transform type.
    079         */
    080        public StandardGradientPaintTransformer(
    081                final GradientPaintTransformType type) {
    082            this.type = type;
    083        }
    084
    085        /**
    086         * Transforms a <code>GradientPaint</code> instance.
    087         *
    088         * @param paint  the original paint.
    089         * @param target  the target shape.
    090         *
    091         * @return The transformed paint.
    092         */
    093        public GradientPaint transform(final GradientPaint paint,
    094                                       final Shape target) {
    095
    096            GradientPaint result = paint;
    097            final Rectangle2D bounds = target.getBounds2D();
    098
    099            if (this.type.equals(GradientPaintTransformType.VERTICAL)) {
    100                result = new GradientPaint(
    101                    (float) bounds.getCenterX(), (float) bounds.getMinY(),
    102                    paint.getColor1(), (float) bounds.getCenterX(),
    103                    (float) bounds.getMaxY(), paint.getColor2()
    104                );
    105            }
    106            else if (this.type.equals(GradientPaintTransformType.HORIZONTAL)) {
    107                result = new GradientPaint(
    108                    (float) bounds.getMinX(), (float) bounds.getCenterY(),
    109                    paint.getColor1(), (float) bounds.getMaxX(),
    110                    (float) bounds.getCenterY(), paint.getColor2()
    111                );
    112            }
    113            else if (this.type.equals(GradientPaintTransformType.CENTER_HORIZONTAL))
    114            {
    115                result = new GradientPaint(
    116                    (float) bounds.getCenterX(), (float) bounds.getCenterY(),
    117                    paint.getColor1(), (float) bounds.getMaxX(),
    118                    (float) bounds.getCenterY(), paint.getColor2(), true
    119                );
    120            }
    121            else if (this.type.equals(GradientPaintTransformType.CENTER_VERTICAL)) {
    122                result = new GradientPaint(
    123                    (float) bounds.getCenterX(), (float) bounds.getMinY(),
    124                    paint.getColor1(), (float) bounds.getCenterX(),
    125                    (float) bounds.getCenterY(), paint.getColor2(), true
    126                );
    127            }
    128
    129            return result;
    130        }
    131
    132        /**
    133         * Tests this instance for equality with an arbitrary object.
    134         *
    135         * @param obj  the object to test against (<code>null</code> permitted).
    136         *
    137         * @return A boolean.
    138         */
    139        public boolean equals(final Object obj) {
    140            if (obj == this) {
    141                return true;
    142            }
    143            if (!(obj instanceof StandardGradientPaintTransformer)) {
    144                return false;
    145            }
    146            final StandardGradientPaintTransformer that
    147                = (StandardGradientPaintTransformer) obj;
    148            if (this.type != that.type) {
    149                return false;
    150            }
    151            return true;
    152        }
    153
    154        /**
    155         * Returns a clone of the transformer.
    156         *
    157         * @return A clone.
    158         *
    159         * @throws CloneNotSupportedException not thrown by this class, but
    160         *         subclasses (if any) might.
    161         */
    162        public Object clone() throws CloneNotSupportedException {
    163            return super.clone();
    164        }
    165
    166        /**
    167         * Returns a hash code for this object.
    168         *
    169         * @return A hash code.
    170         */
    171        public int hashCode() {
    172            return (this.type != null ? this.type.hashCode() : 0);
    173        }
    174
    175    }