1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
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.
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.
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,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* -------------------------------------
28
* StandardGradientPaintTransformer.java
29
* -------------------------------------
30
* (C) Copyright 2003-2005, by Object Refinery Limited.
32
* Original Author: David Gilbert (for Object Refinery Limited);
35
* $Id: StandardGradientPaintTransformer.java,v 1.8 2005/10/18 13:18:34 mungady Exp $
39
* 28-Oct-2003 : Version 1 (DG);
40
* 19-Mar-2004 : Added equals() method (DG);
46
import java.awt.GradientPaint;
47
import java.awt.Shape;
48
import java.awt.geom.Rectangle2D;
49
import java.io.Serializable;
51
import org.jfree.util.PublicCloneable;
54
* Transforms a <code>GradientPaint</code> to range over the width of a target
56
* @author David Gilbert
58
public class StandardGradientPaintTransformer
59
implements GradientPaintTransformer, Cloneable, PublicCloneable,
62
/** For serialization. */
63
private static final long serialVersionUID = -8155025776964678320L;
65
/** The transform type. */
66
private GradientPaintTransformType type;
69
* Creates a new transformer.
71
public StandardGradientPaintTransformer() {
72
this(GradientPaintTransformType.VERTICAL);
76
* Creates a new transformer.
78
* @param type the transform type.
80
public StandardGradientPaintTransformer(
81
final GradientPaintTransformType type) {
86
* Transforms a <code>GradientPaint</code> instance.
88
* @param paint the original paint.
89
* @param target the target shape.
91
* @return The transformed paint.
93
public GradientPaint transform(final GradientPaint paint,
96
GradientPaint result = paint;
97
final Rectangle2D bounds = target.getBounds2D();
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()
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()
113
else if (this.type.equals(GradientPaintTransformType.CENTER_HORIZONTAL))
115
result = new GradientPaint(
116
(float) bounds.getCenterX(), (float) bounds.getCenterY(),
117
paint.getColor1(), (float) bounds.getMaxX(),
118
(float) bounds.getCenterY(), paint.getColor2(), true
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
133
* Tests this instance for equality with an arbitrary object.
135
* @param obj the object to test against (<code>null</code> permitted).
139
public boolean equals(final Object obj) {
143
if (!(obj instanceof StandardGradientPaintTransformer)) {
146
final StandardGradientPaintTransformer that
147
= (StandardGradientPaintTransformer) obj;
148
if (this.type != that.type) {
155
* Returns a clone of the transformer.
159
* @throws CloneNotSupportedException not thrown by this class, but
160
* subclasses (if any) might.
162
public Object clone() throws CloneNotSupportedException {
163
return super.clone();
167
* Returns a hash code for this object.
169
* @return A hash code.
171
public int hashCode() {
172
return (this.type != null ? this.type.hashCode() : 0);