author | henryjen |
Tue, 10 Jun 2014 16:18:54 -0700 | |
changeset 24865 | 09b1d992ca72 |
parent 7006 | 05505fff1342 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7006
05505fff1342
4358979: javax.swing.border should have a DashedBorder
malenkov
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.awt; |
|
27 |
||
28 |
import java.awt.geom.Point2D; |
|
29 |
import java.awt.geom.Rectangle2D; |
|
30 |
import java.awt.geom.AffineTransform; |
|
31 |
import java.awt.image.ColorModel; |
|
7006
05505fff1342
4358979: javax.swing.border should have a DashedBorder
malenkov
parents:
5506
diff
changeset
|
32 |
import java.beans.ConstructorProperties; |
2 | 33 |
|
34 |
/** |
|
35 |
* The <code>GradientPaint</code> class provides a way to fill |
|
36 |
* a {@link Shape} with a linear color gradient pattern. |
|
37 |
* If {@link Point} P1 with {@link Color} C1 and <code>Point</code> P2 with |
|
38 |
* <code>Color</code> C2 are specified in user space, the |
|
39 |
* <code>Color</code> on the P1, P2 connecting line is proportionally |
|
40 |
* changed from C1 to C2. Any point P not on the extended P1, P2 |
|
41 |
* connecting line has the color of the point P' that is the perpendicular |
|
42 |
* projection of P on the extended P1, P2 connecting line. |
|
43 |
* Points on the extended line outside of the P1, P2 segment can be colored |
|
44 |
* in one of two ways. |
|
45 |
* <ul> |
|
46 |
* <li> |
|
47 |
* If the gradient is cyclic then the points on the extended P1, P2 |
|
48 |
* connecting line cycle back and forth between the colors C1 and C2. |
|
49 |
* <li> |
|
50 |
* If the gradient is acyclic then points on the P1 side of the segment |
|
51 |
* have the constant <code>Color</code> C1 while points on the P2 side |
|
52 |
* have the constant <code>Color</code> C2. |
|
53 |
* </ul> |
|
54 |
* |
|
55 |
* @see Paint |
|
56 |
* @see Graphics2D#setPaint |
|
436
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
57 |
* @version 10 Feb 1997 |
2 | 58 |
*/ |
59 |
||
60 |
public class GradientPaint implements Paint { |
|
61 |
Point2D.Float p1; |
|
62 |
Point2D.Float p2; |
|
63 |
Color color1; |
|
64 |
Color color2; |
|
65 |
boolean cyclic; |
|
66 |
||
67 |
/** |
|
68 |
* Constructs a simple acyclic <code>GradientPaint</code> object. |
|
69 |
* @param x1 x coordinate of the first specified |
|
70 |
* <code>Point</code> in user space |
|
71 |
* @param y1 y coordinate of the first specified |
|
72 |
* <code>Point</code> in user space |
|
73 |
* @param color1 <code>Color</code> at the first specified |
|
74 |
* <code>Point</code> |
|
75 |
* @param x2 x coordinate of the second specified |
|
76 |
* <code>Point</code> in user space |
|
77 |
* @param y2 y coordinate of the second specified |
|
78 |
* <code>Point</code> in user space |
|
79 |
* @param color2 <code>Color</code> at the second specified |
|
80 |
* <code>Point</code> |
|
81 |
* @throws NullPointerException if either one of colors is null |
|
82 |
*/ |
|
83 |
public GradientPaint(float x1, |
|
84 |
float y1, |
|
85 |
Color color1, |
|
86 |
float x2, |
|
87 |
float y2, |
|
88 |
Color color2) { |
|
89 |
if ((color1 == null) || (color2 == null)) { |
|
90 |
throw new NullPointerException("Colors cannot be null"); |
|
91 |
} |
|
92 |
||
93 |
p1 = new Point2D.Float(x1, y1); |
|
94 |
p2 = new Point2D.Float(x2, y2); |
|
95 |
this.color1 = color1; |
|
96 |
this.color2 = color2; |
|
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* Constructs a simple acyclic <code>GradientPaint</code> object. |
|
101 |
* @param pt1 the first specified <code>Point</code> in user space |
|
102 |
* @param color1 <code>Color</code> at the first specified |
|
103 |
* <code>Point</code> |
|
104 |
* @param pt2 the second specified <code>Point</code> in user space |
|
105 |
* @param color2 <code>Color</code> at the second specified |
|
106 |
* <code>Point</code> |
|
107 |
* @throws NullPointerException if either one of colors or points |
|
108 |
* is null |
|
109 |
*/ |
|
110 |
public GradientPaint(Point2D pt1, |
|
111 |
Color color1, |
|
112 |
Point2D pt2, |
|
113 |
Color color2) { |
|
114 |
if ((color1 == null) || (color2 == null) || |
|
115 |
(pt1 == null) || (pt2 == null)) { |
|
116 |
throw new NullPointerException("Colors and points should be non-null"); |
|
117 |
} |
|
118 |
||
119 |
p1 = new Point2D.Float((float)pt1.getX(), (float)pt1.getY()); |
|
120 |
p2 = new Point2D.Float((float)pt2.getX(), (float)pt2.getY()); |
|
121 |
this.color1 = color1; |
|
122 |
this.color2 = color2; |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Constructs either a cyclic or acyclic <code>GradientPaint</code> |
|
127 |
* object depending on the <code>boolean</code> parameter. |
|
128 |
* @param x1 x coordinate of the first specified |
|
129 |
* <code>Point</code> in user space |
|
130 |
* @param y1 y coordinate of the first specified |
|
131 |
* <code>Point</code> in user space |
|
132 |
* @param color1 <code>Color</code> at the first specified |
|
133 |
* <code>Point</code> |
|
134 |
* @param x2 x coordinate of the second specified |
|
135 |
* <code>Point</code> in user space |
|
136 |
* @param y2 y coordinate of the second specified |
|
137 |
* <code>Point</code> in user space |
|
138 |
* @param color2 <code>Color</code> at the second specified |
|
139 |
* <code>Point</code> |
|
140 |
* @param cyclic <code>true</code> if the gradient pattern should cycle |
|
141 |
* repeatedly between the two colors; <code>false</code> otherwise |
|
142 |
*/ |
|
143 |
public GradientPaint(float x1, |
|
144 |
float y1, |
|
145 |
Color color1, |
|
146 |
float x2, |
|
147 |
float y2, |
|
148 |
Color color2, |
|
149 |
boolean cyclic) { |
|
150 |
this (x1, y1, color1, x2, y2, color2); |
|
151 |
this.cyclic = cyclic; |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Constructs either a cyclic or acyclic <code>GradientPaint</code> |
|
156 |
* object depending on the <code>boolean</code> parameter. |
|
157 |
* @param pt1 the first specified <code>Point</code> |
|
158 |
* in user space |
|
159 |
* @param color1 <code>Color</code> at the first specified |
|
160 |
* <code>Point</code> |
|
161 |
* @param pt2 the second specified <code>Point</code> |
|
162 |
* in user space |
|
163 |
* @param color2 <code>Color</code> at the second specified |
|
164 |
* <code>Point</code> |
|
165 |
* @param cyclic <code>true</code> if the gradient pattern should cycle |
|
166 |
* repeatedly between the two colors; <code>false</code> otherwise |
|
167 |
* @throws NullPointerException if either one of colors or points |
|
168 |
* is null |
|
169 |
*/ |
|
7006
05505fff1342
4358979: javax.swing.border should have a DashedBorder
malenkov
parents:
5506
diff
changeset
|
170 |
@ConstructorProperties({ "point1", "color1", "point2", "color2", "cyclic" }) |
2 | 171 |
public GradientPaint(Point2D pt1, |
172 |
Color color1, |
|
173 |
Point2D pt2, |
|
174 |
Color color2, |
|
175 |
boolean cyclic) { |
|
176 |
this (pt1, color1, pt2, color2); |
|
177 |
this.cyclic = cyclic; |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Returns a copy of the point P1 that anchors the first color. |
|
182 |
* @return a {@link Point2D} object that is a copy of the point |
|
183 |
* that anchors the first color of this |
|
184 |
* <code>GradientPaint</code>. |
|
185 |
*/ |
|
186 |
public Point2D getPoint1() { |
|
187 |
return new Point2D.Float(p1.x, p1.y); |
|
188 |
} |
|
189 |
||
190 |
/** |
|
191 |
* Returns the color C1 anchored by the point P1. |
|
192 |
* @return a <code>Color</code> object that is the color |
|
193 |
* anchored by P1. |
|
194 |
*/ |
|
195 |
public Color getColor1() { |
|
196 |
return color1; |
|
197 |
} |
|
198 |
||
199 |
/** |
|
200 |
* Returns a copy of the point P2 which anchors the second color. |
|
201 |
* @return a {@link Point2D} object that is a copy of the point |
|
202 |
* that anchors the second color of this |
|
203 |
* <code>GradientPaint</code>. |
|
204 |
*/ |
|
205 |
public Point2D getPoint2() { |
|
206 |
return new Point2D.Float(p2.x, p2.y); |
|
207 |
} |
|
208 |
||
209 |
/** |
|
210 |
* Returns the color C2 anchored by the point P2. |
|
211 |
* @return a <code>Color</code> object that is the color |
|
212 |
* anchored by P2. |
|
213 |
*/ |
|
214 |
public Color getColor2() { |
|
215 |
return color2; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Returns <code>true</code> if the gradient cycles repeatedly |
|
220 |
* between the two colors C1 and C2. |
|
221 |
* @return <code>true</code> if the gradient cycles repeatedly |
|
222 |
* between the two colors; <code>false</code> otherwise. |
|
223 |
*/ |
|
224 |
public boolean isCyclic() { |
|
225 |
return cyclic; |
|
226 |
} |
|
227 |
||
228 |
/** |
|
436
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
229 |
* Creates and returns a {@link PaintContext} used to |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
230 |
* generate a linear color gradient pattern. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
231 |
* See the {@link Paint#createContext specification} of the |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
232 |
* method in the {@link Paint} interface for information |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
233 |
* on null parameter handling. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
234 |
* |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
235 |
* @param cm the preferred {@link ColorModel} which represents the most convenient |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
236 |
* format for the caller to receive the pixel data, or {@code null} |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
237 |
* if there is no preference. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
238 |
* @param deviceBounds the device space bounding box |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
239 |
* of the graphics primitive being rendered. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
240 |
* @param userBounds the user space bounding box |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
241 |
* of the graphics primitive being rendered. |
2 | 242 |
* @param xform the {@link AffineTransform} from user |
436
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
243 |
* space into device space. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
244 |
* @param hints the set of hints that the context object can use to |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
245 |
* choose between rendering alternatives. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
246 |
* @return the {@code PaintContext} for |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
247 |
* generating color patterns. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
248 |
* @see Paint |
2 | 249 |
* @see PaintContext |
436
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
250 |
* @see ColorModel |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
251 |
* @see Rectangle |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
252 |
* @see Rectangle2D |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
253 |
* @see AffineTransform |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
254 |
* @see RenderingHints |
2 | 255 |
*/ |
256 |
public PaintContext createContext(ColorModel cm, |
|
257 |
Rectangle deviceBounds, |
|
258 |
Rectangle2D userBounds, |
|
259 |
AffineTransform xform, |
|
260 |
RenderingHints hints) { |
|
261 |
||
262 |
return new GradientPaintContext(cm, p1, p2, xform, |
|
263 |
color1, color2, cyclic); |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
* Returns the transparency mode for this <code>GradientPaint</code>. |
|
268 |
* @return an integer value representing this <code>GradientPaint</code> |
|
269 |
* object's transparency mode. |
|
270 |
* @see Transparency |
|
271 |
*/ |
|
272 |
public int getTransparency() { |
|
273 |
int a1 = color1.getAlpha(); |
|
274 |
int a2 = color2.getAlpha(); |
|
275 |
return (((a1 & a2) == 0xff) ? OPAQUE : TRANSLUCENT); |
|
276 |
} |
|
277 |
||
278 |
} |