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:
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. Instances of this class are immutable.
056: *
057: * @author David Gilbert
058: */
059: public class StandardGradientPaintTransformer implements
060: 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
120: .getMaxY(), paint.getColor2());
121: } else if (this .type
122: .equals(GradientPaintTransformType.HORIZONTAL)) {
123: result = new GradientPaint((float) bounds.getMinX(),
124: (float) bounds.getCenterY(), paint.getColor1(),
125: (float) bounds.getMaxX(), (float) bounds
126: .getCenterY(), paint.getColor2());
127: } else if (this .type
128: .equals(GradientPaintTransformType.CENTER_HORIZONTAL)) {
129: result = new GradientPaint((float) bounds.getCenterX(),
130: (float) bounds.getCenterY(), paint.getColor2(),
131: (float) bounds.getMaxX(), (float) bounds
132: .getCenterY(), paint.getColor1(), true);
133: } else if (this .type
134: .equals(GradientPaintTransformType.CENTER_VERTICAL)) {
135: result = new GradientPaint((float) bounds.getCenterX(),
136: (float) bounds.getMinY(), paint.getColor1(),
137: (float) bounds.getCenterX(), (float) bounds
138: .getCenterY(), paint.getColor2(), true);
139: }
140:
141: return result;
142: }
143:
144: /**
145: * Tests this instance for equality with an arbitrary object.
146: *
147: * @param obj the object (<code>null</code> permitted).
148: *
149: * @return A boolean.
150: */
151: public boolean equals(final Object obj) {
152: if (obj == this ) {
153: return true;
154: }
155: if (!(obj instanceof StandardGradientPaintTransformer)) {
156: return false;
157: }
158: StandardGradientPaintTransformer that = (StandardGradientPaintTransformer) obj;
159: if (this .type != that.type) {
160: return false;
161: }
162: return true;
163: }
164:
165: /**
166: * Returns a clone of the transformer. Note that instances of this class
167: * are immutable, so cloning an instance isn't really necessary.
168: *
169: * @return A clone.
170: *
171: * @throws CloneNotSupportedException not thrown by this class, but
172: * subclasses (if any) might.
173: */
174: public Object clone() throws CloneNotSupportedException {
175: return super .clone();
176: }
177:
178: /**
179: * Returns a hash code for this object.
180: *
181: * @return A hash code.
182: */
183: public int hashCode() {
184: return (this .type != null ? this .type.hashCode() : 0);
185: }
186:
187: }
|