001 /*
002 * Copyright 1997-2002 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt;
027
028 import java.awt.geom.Point2D;
029 import java.awt.geom.Rectangle2D;
030 import java.awt.geom.AffineTransform;
031 import java.awt.image.ColorModel;
032
033 /**
034 * The <code>GradientPaint</code> class provides a way to fill
035 * a {@link Shape} with a linear color gradient pattern.
036 * If {@link Point} P1 with {@link Color} C1 and <code>Point</code> P2 with
037 * <code>Color</code> C2 are specified in user space, the
038 * <code>Color</code> on the P1, P2 connecting line is proportionally
039 * changed from C1 to C2. Any point P not on the extended P1, P2
040 * connecting line has the color of the point P' that is the perpendicular
041 * projection of P on the extended P1, P2 connecting line.
042 * Points on the extended line outside of the P1, P2 segment can be colored
043 * in one of two ways.
044 * <ul>
045 * <li>
046 * If the gradient is cyclic then the points on the extended P1, P2
047 * connecting line cycle back and forth between the colors C1 and C2.
048 * <li>
049 * If the gradient is acyclic then points on the P1 side of the segment
050 * have the constant <code>Color</code> C1 while points on the P2 side
051 * have the constant <code>Color</code> C2.
052 * </ul>
053 *
054 * @see Paint
055 * @see Graphics2D#setPaint
056 * @version 10 Feb 1997
057 */
058
059 public class GradientPaint implements Paint {
060 Point2D.Float p1;
061 Point2D.Float p2;
062 Color color1;
063 Color color2;
064 boolean cyclic;
065
066 /**
067 * Constructs a simple acyclic <code>GradientPaint</code> object.
068 * @param x1 x coordinate of the first specified
069 * <code>Point</code> in user space
070 * @param y1 y coordinate of the first specified
071 * <code>Point</code> in user space
072 * @param color1 <code>Color</code> at the first specified
073 * <code>Point</code>
074 * @param x2 x coordinate of the second specified
075 * <code>Point</code> in user space
076 * @param y2 y coordinate of the second specified
077 * <code>Point</code> in user space
078 * @param color2 <code>Color</code> at the second specified
079 * <code>Point</code>
080 * @throws NullPointerException if either one of colors is null
081 */
082 public GradientPaint(float x1, float y1, Color color1, float x2,
083 float y2, Color color2) {
084 if ((color1 == null) || (color2 == null)) {
085 throw new NullPointerException("Colors cannot be null");
086 }
087
088 p1 = new Point2D.Float(x1, y1);
089 p2 = new Point2D.Float(x2, y2);
090 this .color1 = color1;
091 this .color2 = color2;
092 }
093
094 /**
095 * Constructs a simple acyclic <code>GradientPaint</code> object.
096 * @param pt1 the first specified <code>Point</code> in user space
097 * @param color1 <code>Color</code> at the first specified
098 * <code>Point</code>
099 * @param pt2 the second specified <code>Point</code> in user space
100 * @param color2 <code>Color</code> at the second specified
101 * <code>Point</code>
102 * @throws NullPointerException if either one of colors or points
103 * is null
104 */
105 public GradientPaint(Point2D pt1, Color color1, Point2D pt2,
106 Color color2) {
107 if ((color1 == null) || (color2 == null) || (pt1 == null)
108 || (pt2 == null)) {
109 throw new NullPointerException(
110 "Colors and points should be non-null");
111 }
112
113 p1 = new Point2D.Float((float) pt1.getX(), (float) pt1.getY());
114 p2 = new Point2D.Float((float) pt2.getX(), (float) pt2.getY());
115 this .color1 = color1;
116 this .color2 = color2;
117 }
118
119 /**
120 * Constructs either a cyclic or acyclic <code>GradientPaint</code>
121 * object depending on the <code>boolean</code> parameter.
122 * @param x1 x coordinate of the first specified
123 * <code>Point</code> in user space
124 * @param y1 y coordinate of the first specified
125 * <code>Point</code> in user space
126 * @param color1 <code>Color</code> at the first specified
127 * <code>Point</code>
128 * @param x2 x coordinate of the second specified
129 * <code>Point</code> in user space
130 * @param y2 y coordinate of the second specified
131 * <code>Point</code> in user space
132 * @param color2 <code>Color</code> at the second specified
133 * <code>Point</code>
134 * @param cyclic <code>true</code> if the gradient pattern should cycle
135 * repeatedly between the two colors; <code>false</code> otherwise
136 */
137 public GradientPaint(float x1, float y1, Color color1, float x2,
138 float y2, Color color2, boolean cyclic) {
139 this (x1, y1, color1, x2, y2, color2);
140 this .cyclic = cyclic;
141 }
142
143 /**
144 * Constructs either a cyclic or acyclic <code>GradientPaint</code>
145 * object depending on the <code>boolean</code> parameter.
146 * @param pt1 the first specified <code>Point</code>
147 * in user space
148 * @param color1 <code>Color</code> at the first specified
149 * <code>Point</code>
150 * @param pt2 the second specified <code>Point</code>
151 * in user space
152 * @param color2 <code>Color</code> at the second specified
153 * <code>Point</code>
154 * @param cyclic <code>true</code> if the gradient pattern should cycle
155 * repeatedly between the two colors; <code>false</code> otherwise
156 * @throws NullPointerException if either one of colors or points
157 * is null
158 */
159 public GradientPaint(Point2D pt1, Color color1, Point2D pt2,
160 Color color2, boolean cyclic) {
161 this (pt1, color1, pt2, color2);
162 this .cyclic = cyclic;
163 }
164
165 /**
166 * Returns a copy of the point P1 that anchors the first color.
167 * @return a {@link Point2D} object that is a copy of the point
168 * that anchors the first color of this
169 * <code>GradientPaint</code>.
170 */
171 public Point2D getPoint1() {
172 return new Point2D.Float(p1.x, p1.y);
173 }
174
175 /**
176 * Returns the color C1 anchored by the point P1.
177 * @return a <code>Color</code> object that is the color
178 * anchored by P1.
179 */
180 public Color getColor1() {
181 return color1;
182 }
183
184 /**
185 * Returns a copy of the point P2 which anchors the second color.
186 * @return a {@link Point2D} object that is a copy of the point
187 * that anchors the second color of this
188 * <code>GradientPaint</code>.
189 */
190 public Point2D getPoint2() {
191 return new Point2D.Float(p2.x, p2.y);
192 }
193
194 /**
195 * Returns the color C2 anchored by the point P2.
196 * @return a <code>Color</code> object that is the color
197 * anchored by P2.
198 */
199 public Color getColor2() {
200 return color2;
201 }
202
203 /**
204 * Returns <code>true</code> if the gradient cycles repeatedly
205 * between the two colors C1 and C2.
206 * @return <code>true</code> if the gradient cycles repeatedly
207 * between the two colors; <code>false</code> otherwise.
208 */
209 public boolean isCyclic() {
210 return cyclic;
211 }
212
213 /**
214 * Creates and returns a context used to generate the color pattern.
215 * @param cm {@link ColorModel} that receives
216 * the <code>Paint</code> data. This is used only as a hint.
217 * @param deviceBounds the device space bounding box of the
218 * graphics primitive being rendered
219 * @param userBounds the user space bounding box of the
220 * graphics primitive being rendered
221 * @param xform the {@link AffineTransform} from user
222 * space into device space
223 * @param hints the hints that the context object uses to choose
224 * between rendering alternatives
225 * @return the {@link PaintContext} that generates color patterns.
226 * @see PaintContext
227 */
228 public PaintContext createContext(ColorModel cm,
229 Rectangle deviceBounds, Rectangle2D userBounds,
230 AffineTransform xform, RenderingHints hints) {
231
232 return new GradientPaintContext(cm, p1, p2, xform, color1,
233 color2, cyclic);
234 }
235
236 /**
237 * Returns the transparency mode for this <code>GradientPaint</code>.
238 * @return an integer value representing this <code>GradientPaint</code>
239 * object's transparency mode.
240 * @see Transparency
241 */
242 public int getTransparency() {
243 int a1 = color1.getAlpha();
244 int a2 = color2.getAlpha();
245 return (((a1 & a2) == 0xff) ? OPAQUE : TRANSLUCENT);
246 }
247
248 }
|