Source for org.jfree.ui.StandardGradientPaintTransformer

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * -------------------------------------
  28:  * StandardGradientPaintTransformer.java
  29:  * -------------------------------------
  30:  * (C) Copyright 2003-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: StandardGradientPaintTransformer.java,v 1.11 2007/04/03 13:55:13 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 28-Oct-2003 : Version 1 (DG);
  40:  * 19-Mar-2004 : Added equals() method (DG);
  41:  * 
  42:  */
  43: 
  44: package org.jfree.ui;
  45: 
  46: import java.awt.GradientPaint;
  47: import java.awt.Shape;
  48: import java.awt.geom.Rectangle2D;
  49: import java.io.Serializable;
  50: 
  51: import org.jfree.util.PublicCloneable;
  52: 
  53: /**
  54:  * Transforms a <code>GradientPaint</code> to range over the width of a target 
  55:  * shape.  Instances of this class are immutable.
  56:  * 
  57:  * @author David Gilbert
  58:  */
  59: public class StandardGradientPaintTransformer 
  60:     implements GradientPaintTransformer, Cloneable, PublicCloneable, 
  61:                Serializable {
  62:     
  63:     /** For serialization. */
  64:     private static final long serialVersionUID = -8155025776964678320L;
  65: 
  66:     /** The transform type. */
  67:     private GradientPaintTransformType type;
  68:     
  69:     /**
  70:      * Creates a new transformer with the type 
  71:      * {@link GradientPaintTransformType#VERTICAL}.
  72:      */
  73:     public StandardGradientPaintTransformer() {
  74:         this(GradientPaintTransformType.VERTICAL);
  75:     }
  76:     
  77:     /**
  78:      * Creates a new transformer with the specified type.
  79:      * 
  80:      * @param type  the transform type (<code>null</code> not permitted).
  81:      */
  82:     public StandardGradientPaintTransformer(
  83:             final GradientPaintTransformType type) {
  84:         if (type == null) {
  85:             throw new IllegalArgumentException("Null 'type' argument.");
  86:         }
  87:         this.type = type;
  88:     }
  89:     
  90:     /**
  91:      * Returns the type of transform.
  92:      * 
  93:      * @return The type of transform (never <code>null</code>).
  94:      * 
  95:      * @since 1.0.10
  96:      */
  97:     public GradientPaintTransformType getType() {
  98:         return this.type;
  99:     }
 100:     
 101:     /**
 102:      * Transforms a <code>GradientPaint</code> instance to fit the specified
 103:      * <code>target</code> shape.
 104:      * 
 105:      * @param paint  the original paint (<code>null</code> not permitted).
 106:      * @param target  the target shape (<code>null</code> not permitted).
 107:      * 
 108:      * @return The transformed paint.
 109:      */
 110:     public GradientPaint transform(final GradientPaint paint, 
 111:                                    final Shape target) {
 112:         
 113:         GradientPaint result = paint;
 114:         final Rectangle2D bounds = target.getBounds2D();
 115:         
 116:         if (this.type.equals(GradientPaintTransformType.VERTICAL)) {
 117:             result = new GradientPaint((float) bounds.getCenterX(), 
 118:                     (float) bounds.getMinY(), paint.getColor1(), 
 119:                     (float) bounds.getCenterX(), (float) bounds.getMaxY(), 
 120:                     paint.getColor2());
 121:         }
 122:         else if (this.type.equals(GradientPaintTransformType.HORIZONTAL)) {
 123:             result = new GradientPaint((float) bounds.getMinX(), 
 124:                     (float) bounds.getCenterY(), paint.getColor1(), 
 125:                     (float) bounds.getMaxX(), (float) bounds.getCenterY(), 
 126:                     paint.getColor2());            
 127:         }
 128:         else if (this.type.equals(
 129:                 GradientPaintTransformType.CENTER_HORIZONTAL)) {
 130:             result = new GradientPaint((float) bounds.getCenterX(), 
 131:                     (float) bounds.getCenterY(), paint.getColor2(), 
 132:                     (float) bounds.getMaxX(), (float) bounds.getCenterY(), 
 133:                     paint.getColor1(), true);            
 134:         }
 135:         else if (this.type.equals(GradientPaintTransformType.CENTER_VERTICAL)) {
 136:             result = new GradientPaint((float) bounds.getCenterX(), 
 137:                     (float) bounds.getMinY(), paint.getColor1(), 
 138:                     (float) bounds.getCenterX(), (float) bounds.getCenterY(), 
 139:                     paint.getColor2(), true);            
 140:         }
 141:         
 142:         return result;
 143:     }
 144:     
 145:     /**
 146:      * Tests this instance for equality with an arbitrary object.
 147:      * 
 148:      * @param obj  the object (<code>null</code> permitted).
 149:      * 
 150:      * @return A boolean.
 151:      */
 152:     public boolean equals(final Object obj) {
 153:         if (obj == this) {
 154:             return true;   
 155:         }
 156:         if (!(obj instanceof StandardGradientPaintTransformer)) {
 157:             return false;
 158:         }
 159:         StandardGradientPaintTransformer that 
 160:                 = (StandardGradientPaintTransformer) obj;
 161:         if (this.type != that.type) {
 162:             return false;
 163:         }
 164:         return true;
 165:     }
 166:     
 167:     /**
 168:      * Returns a clone of the transformer.  Note that instances of this class
 169:      * are immutable, so cloning an instance isn't really necessary.
 170:      * 
 171:      * @return A clone.
 172:      * 
 173:      * @throws CloneNotSupportedException not thrown by this class, but 
 174:      *         subclasses (if any) might.
 175:      */
 176:     public Object clone() throws CloneNotSupportedException {
 177:         return super.clone();
 178:     }
 179: 
 180:     /**
 181:      * Returns a hash code for this object.
 182:      * 
 183:      * @return A hash code.
 184:      */
 185:     public int hashCode() {
 186:         return (this.type != null ? this.type.hashCode() : 0);
 187:     }
 188:     
 189: }