KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > GradientPaint


1 /*
2  * @(#)GradientPaint.java 1.39 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 import java.awt.geom.Point2D JavaDoc;
11 import java.awt.geom.Rectangle2D JavaDoc;
12 import java.awt.geom.AffineTransform JavaDoc;
13 import java.awt.image.ColorModel JavaDoc;
14
15 /**
16  * The <code>GradientPaint</code> class provides a way to fill
17  * a {@link Shape} with a linear color gradient pattern.
18  * If {@link Point} P1 with {@link Color} C1 and <code>Point</code> P2 with
19  * <code>Color</code> C2 are specified in user space, the
20  * <code>Color</code> on the P1, P2 connecting line is proportionally
21  * changed from C1 to C2. Any point P not on the extended P1, P2
22  * connecting line has the color of the point P' that is the perpendicular
23  * projection of P on the extended P1, P2 connecting line.
24  * Points on the extended line outside of the P1, P2 segment can be colored
25  * in one of two ways.
26  * <ul>
27  * <li>
28  * If the gradient is cyclic then the points on the extended P1, P2
29  * connecting line cycle back and forth between the colors C1 and C2.
30  * <li>
31  * If the gradient is acyclic then points on the P1 side of the segment
32  * have the constant <code>Color</code> C1 while points on the P2 side
33  * have the constant <code>Color</code> C2.
34  * </ul>
35  *
36  * @see Paint
37  * @see Graphics2D#setPaint
38  * @version 10 Feb 1997
39  */

40
41 public class GradientPaint implements Paint JavaDoc {
42     Point2D.Float JavaDoc p1;
43     Point2D.Float JavaDoc p2;
44     Color JavaDoc color1;
45     Color JavaDoc color2;
46     boolean cyclic;
47
48     /**
49      * Constructs a simple acyclic <code>GradientPaint</code> object.
50      * @param x1 x coordinate of the first specified
51      * <code>Point</code> in user space
52      * @param y1 y coordinate of the first specified
53      * <code>Point</code> in user space
54      * @param color1 <code>Color</code> at the first specified
55      * <code>Point</code>
56      * @param x2 x coordinate of the second specified
57      * <code>Point</code> in user space
58      * @param y2 y coordinate of the second specified
59      * <code>Point</code> in user space
60      * @param color2 <code>Color</code> at the second specified
61      * <code>Point</code>
62      * @throws NullPointerException if either one of colors is null
63      */

64     public GradientPaint(float x1,
65              float y1,
66              Color JavaDoc color1,
67              float x2,
68              float y2,
69              Color JavaDoc color2) {
70         if ((color1 == null) || (color2 == null)) {
71             throw new NullPointerException JavaDoc("Colors cannot be null");
72         }
73
74         p1 = new Point2D.Float JavaDoc(x1, y1);
75         p2 = new Point2D.Float JavaDoc(x2, y2);
76         this.color1 = color1;
77         this.color2 = color2;
78     }
79
80     /**
81      * Constructs a simple acyclic <code>GradientPaint</code> object.
82      * @param pt1 the first specified <code>Point</code> in user space
83      * @param color1 <code>Color</code> at the first specified
84      * <code>Point</code>
85      * @param pt2 the second specified <code>Point</code> in user space
86      * @param color2 <code>Color</code> at the second specified
87      * <code>Point</code>
88      * @throws NullPointerException if either one of colors or points
89      * is null
90      */

91     public GradientPaint(Point2D JavaDoc pt1,
92              Color JavaDoc color1,
93              Point2D JavaDoc pt2,
94              Color JavaDoc color2) {
95         if ((color1 == null) || (color2 == null) ||
96             (pt1 == null) || (pt2 == null)) {
97             throw new NullPointerException JavaDoc("Colors and points should be non-null");
98         }
99
100         p1 = new Point2D.Float JavaDoc((float)pt1.getX(), (float)pt1.getY());
101         p2 = new Point2D.Float JavaDoc((float)pt2.getX(), (float)pt2.getY());
102         this.color1 = color1;
103         this.color2 = color2;
104     }
105
106     /**
107      * Constructs either a cyclic or acyclic <code>GradientPaint</code>
108      * object depending on the <code>boolean</code> parameter.
109      * @param x1 x coordinate of the first specified
110      * <code>Point</code> in user space
111      * @param y1 y coordinate of the first specified
112      * <code>Point</code> in user space
113      * @param color1 <code>Color</code> at the first specified
114      * <code>Point</code>
115      * @param x2 x coordinate of the second specified
116      * <code>Point</code> in user space
117      * @param y2 y coordinate of the second specified
118      * <code>Point</code> in user space
119      * @param color2 <code>Color</code> at the second specified
120      * <code>Point</code>
121      * @param cyclic <code>true</code> if the gradient pattern should cycle
122      * repeatedly between the two colors; <code>false</code> otherwise
123      */

124     public GradientPaint(float x1,
125              float y1,
126              Color JavaDoc color1,
127              float x2,
128              float y2,
129              Color JavaDoc color2,
130              boolean cyclic) {
131     this (x1, y1, color1, x2, y2, color2);
132     this.cyclic = cyclic;
133     }
134
135     /**
136      * Constructs either a cyclic or acyclic <code>GradientPaint</code>
137      * object depending on the <code>boolean</code> parameter.
138      * @param pt1 the first specified <code>Point</code>
139      * in user space
140      * @param color1 <code>Color</code> at the first specified
141      * <code>Point</code>
142      * @param pt2 the second specified <code>Point</code>
143      * in user space
144      * @param color2 <code>Color</code> at the second specified
145      * <code>Point</code>
146      * @param cyclic <code>true</code> if the gradient pattern should cycle
147      * repeatedly between the two colors; <code>false</code> otherwise
148      * @throws NullPointerException if either one of colors or points
149      * is null
150      */

151     public GradientPaint(Point2D JavaDoc pt1,
152              Color JavaDoc color1,
153              Point2D JavaDoc pt2,
154              Color JavaDoc color2,
155              boolean cyclic) {
156     this (pt1, color1, pt2, color2);
157     this.cyclic = cyclic;
158     }
159
160     /**
161      * Returns a copy of the point P1 that anchors the first color.
162      * @return a {@link Point2D} object that is a copy of the point
163      * that anchors the first color of this
164      * <code>GradientPaint</code>.
165      */

166     public Point2D JavaDoc getPoint1() {
167     return new Point2D.Float JavaDoc(p1.x, p1.y);
168     }
169
170     /**
171      * Returns the color C1 anchored by the point P1.
172      * @return a <code>Color</code> object that is the color
173      * anchored by P1.
174      */

175     public Color JavaDoc getColor1() {
176     return color1;
177     }
178
179     /**
180      * Returns a copy of the point P2 which anchors the second color.
181      * @return a {@link Point2D} object that is a copy of the point
182      * that anchors the second color of this
183      * <code>GradientPaint</code>.
184      */

185     public Point2D JavaDoc getPoint2() {
186     return new Point2D.Float JavaDoc(p2.x, p2.y);
187     }
188
189     /**
190      * Returns the color C2 anchored by the point P2.
191      * @return a <code>Color</code> object that is the color
192      * anchored by P2.
193      */

194     public Color JavaDoc getColor2() {
195     return color2;
196     }
197
198     /**
199      * Returns <code>true</code> if the gradient cycles repeatedly
200      * between the two colors C1 and C2.
201      * @return <code>true</code> if the gradient cycles repeatedly
202      * between the two colors; <code>false</code> otherwise.
203      */

204     public boolean isCyclic() {
205     return cyclic;
206     }
207
208     /**
209      * Creates and returns a context used to generate the color pattern.
210      * @param cm {@link ColorModel} that receives
211      * the <code>Paint</code> data. This is used only as a hint.
212      * @param deviceBounds the device space bounding box of the
213      * graphics primitive being rendered
214      * @param userBounds the user space bounding box of the
215      * graphics primitive being rendered
216      * @param xform the {@link AffineTransform} from user
217      * space into device space
218      * @param hints the hints that the context object uses to choose
219      * between rendering alternatives
220      * @return the {@link PaintContext} that generates color patterns.
221      * @see PaintContext
222      */

223     public PaintContext JavaDoc createContext(ColorModel JavaDoc cm,
224                       Rectangle JavaDoc deviceBounds,
225                       Rectangle2D JavaDoc userBounds,
226                       AffineTransform JavaDoc xform,
227                                       RenderingHints JavaDoc hints) {
228
229         return new GradientPaintContext JavaDoc(cm, p1, p2, xform,
230                     color1, color2, cyclic);
231     }
232
233     /**
234      * Returns the transparency mode for this <code>GradientPaint</code>.
235      * @return an integer value representing this <code>GradientPaint</code>
236      * object's transparency mode.
237      * @see Transparency
238      */

239     public int getTransparency() {
240     int a1 = color1.getAlpha();
241     int a2 = color2.getAlpha();
242     return (((a1 & a2) == 0xff) ? OPAQUE : TRANSLUCENT);
243     }
244
245 }
246
Popular Tags