001/* ======================================================================== 002 * JCommon : a free general purpose class library for the Java(tm) platform 003 * ======================================================================== 004 * 005 * (C) Copyright 2000-2007, 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-2007, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * $Id: StandardGradientPaintTransformer.java,v 1.11 2007/04/03 13:55:13 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 28-Oct-2003 : Version 1 (DG); 040 * 19-Mar-2004 : Added equals() method (DG); 041 * 042 */ 043 044package org.jfree.ui; 045 046import java.awt.GradientPaint; 047import java.awt.Shape; 048import java.awt.geom.Rectangle2D; 049import java.io.Serializable; 050 051import org.jfree.util.PublicCloneable; 052 053/** 054 * Transforms a <code>GradientPaint</code> to range over the width of a target 055 * shape. Instances of this class are immutable. 056 * 057 * @author David Gilbert 058 */ 059public class StandardGradientPaintTransformer 060 implements GradientPaintTransformer, Cloneable, PublicCloneable, 061 Serializable { 062 063 /** For serialization. */ 064 private static final long serialVersionUID = -8155025776964678320L; 065 066 /** The transform type. */ 067 private GradientPaintTransformType type; 068 069 /** 070 * Creates a new transformer with the type 071 * {@link GradientPaintTransformType#VERTICAL}. 072 */ 073 public StandardGradientPaintTransformer() { 074 this(GradientPaintTransformType.VERTICAL); 075 } 076 077 /** 078 * Creates a new transformer with the specified type. 079 * 080 * @param type the transform type (<code>null</code> not permitted). 081 */ 082 public StandardGradientPaintTransformer( 083 final GradientPaintTransformType type) { 084 if (type == null) { 085 throw new IllegalArgumentException("Null 'type' argument."); 086 } 087 this.type = type; 088 } 089 090 /** 091 * Returns the type of transform. 092 * 093 * @return The type of transform (never <code>null</code>). 094 * 095 * @since 1.0.10 096 */ 097 public GradientPaintTransformType getType() { 098 return this.type; 099 } 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}