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-2005, 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-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: StandardGradientPaintTransformer.java,v 1.8 2005/10/18 13:18:34 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.
  56:  * @author David Gilbert
  57:  */
  58: public class StandardGradientPaintTransformer 
  59:     implements GradientPaintTransformer, Cloneable, PublicCloneable, 
  60:                Serializable {
  61:     
  62:     /** For serialization. */
  63:     private static final long serialVersionUID = -8155025776964678320L;
  64: 
  65:     /** The transform type. */
  66:     private GradientPaintTransformType type;
  67:     
  68:     /**
  69:      * Creates a new transformer.
  70:      */
  71:     public StandardGradientPaintTransformer() {
  72:         this(GradientPaintTransformType.VERTICAL);
  73:     }
  74:     
  75:     /**
  76:      * Creates a new transformer.
  77:      * 
  78:      * @param type  the transform type.
  79:      */
  80:     public StandardGradientPaintTransformer(
  81:             final GradientPaintTransformType type) {
  82:         this.type = type;
  83:     }
  84:     
  85:     /**
  86:      * Transforms a <code>GradientPaint</code> instance.
  87:      * 
  88:      * @param paint  the original paint.
  89:      * @param target  the target shape.
  90:      * 
  91:      * @return The transformed paint.
  92:      */
  93:     public GradientPaint transform(final GradientPaint paint, 
  94:                                    final Shape target) {
  95:         
  96:         GradientPaint result = paint;
  97:         final Rectangle2D bounds = target.getBounds2D();
  98:         
  99:         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: }