1 |
14 |
jlechner |
/* MetalUtils.java
|
2 |
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package javax.swing.plaf.metal;
|
39 |
|
|
|
40 |
|
|
import java.awt.Color;
|
41 |
|
|
import java.awt.Component;
|
42 |
|
|
import java.awt.Graphics;
|
43 |
|
|
import java.awt.Graphics2D;
|
44 |
|
|
import java.awt.TexturePaint;
|
45 |
|
|
import java.awt.geom.Rectangle2D;
|
46 |
|
|
import java.awt.image.BufferedImage;
|
47 |
|
|
import java.util.List;
|
48 |
|
|
|
49 |
|
|
import javax.swing.SwingConstants;
|
50 |
|
|
import javax.swing.UIManager;
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* Some utility and helper methods for the Metal Look & Feel.
|
54 |
|
|
*
|
55 |
|
|
* @author Roman Kennke (roman@kennke.org)
|
56 |
|
|
*/
|
57 |
|
|
class MetalUtils
|
58 |
|
|
{
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* The typical metal pattern for use with Graphics2D.
|
62 |
|
|
*/
|
63 |
|
|
static BufferedImage pattern2D;
|
64 |
|
|
|
65 |
|
|
/**
|
66 |
|
|
* The light color to draw the pattern.
|
67 |
|
|
*/
|
68 |
|
|
static Color lightColor;
|
69 |
|
|
|
70 |
|
|
/**
|
71 |
|
|
* The dark color to draw to draw the pattern.
|
72 |
|
|
*/
|
73 |
|
|
static Color darkColor;
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* Fills a rectangle with the typical Metal pattern.
|
77 |
|
|
*
|
78 |
|
|
* @param g the <code>Graphics</code> context to use
|
79 |
|
|
* @param x the X coordinate of the upper left corner of the rectangle to
|
80 |
|
|
* fill
|
81 |
|
|
* @param y the Y coordinate of the upper left corner of the rectangle to
|
82 |
|
|
* fill
|
83 |
|
|
* @param w the width of the rectangle to fill
|
84 |
|
|
* @param h the height of the rectangle to fill
|
85 |
|
|
* @param light the light color to use
|
86 |
|
|
* @param dark the dark color to use
|
87 |
|
|
*/
|
88 |
|
|
static void fillMetalPattern(Component c, Graphics g, int x, int y, int w, int h,
|
89 |
|
|
Color light, Color dark)
|
90 |
|
|
{
|
91 |
|
|
if (g instanceof Graphics2D)
|
92 |
|
|
fillMetalPattern2D((Graphics2D) g, x, y, w, h, light, dark);
|
93 |
|
|
else
|
94 |
|
|
{
|
95 |
|
|
int xOff = 0;
|
96 |
|
|
for (int mY = y; mY < (y + h); mY++)
|
97 |
|
|
{
|
98 |
|
|
// set color alternating with every line
|
99 |
|
|
if (((mY - y) % 2) == 0)
|
100 |
|
|
g.setColor(light);
|
101 |
|
|
else
|
102 |
|
|
g.setColor(dark);
|
103 |
|
|
|
104 |
|
|
for (int mX = x + (xOff); mX < (x + w); mX += 4)
|
105 |
|
|
{
|
106 |
|
|
g.drawLine(mX, mY, mX, mY);
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
// increase x offset
|
110 |
|
|
xOff++;
|
111 |
|
|
if (xOff > 3)
|
112 |
|
|
xOff = 0;
|
113 |
|
|
}
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
/**
|
118 |
|
|
* Fills a rectangle with the typical Metal pattern using Java2D.
|
119 |
|
|
*
|
120 |
|
|
* @param g2d the <code>Graphics2D</code> context to use
|
121 |
|
|
* @param x the X coordinate of the upper left corner of the rectangle to
|
122 |
|
|
* fill
|
123 |
|
|
* @param y the Y coordinate of the upper left corner of the rectangle to
|
124 |
|
|
* fill
|
125 |
|
|
* @param w the width of the rectangle to fill
|
126 |
|
|
* @param h the height of the rectangle to fill
|
127 |
|
|
*/
|
128 |
|
|
static void fillMetalPattern2D(Graphics2D g2d, int x, int y, int w, int h,
|
129 |
|
|
Color light, Color dark)
|
130 |
|
|
{
|
131 |
|
|
if (pattern2D == null || !darkColor.equals(dark) || !lightColor.equals(light))
|
132 |
|
|
initializePattern(light, dark);
|
133 |
|
|
|
134 |
|
|
// Prepare the texture.
|
135 |
|
|
TexturePaint texture =
|
136 |
|
|
new TexturePaint(pattern2D, new Rectangle2D.Double(0., 0., 4., 4.));
|
137 |
|
|
g2d.setPaint(texture);
|
138 |
|
|
g2d.fillRect(x, y, w, h);
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
/**
|
142 |
|
|
* Initializes the pattern image.
|
143 |
|
|
*/
|
144 |
|
|
static void initializePattern(Color light, Color dark)
|
145 |
|
|
{
|
146 |
|
|
pattern2D = new BufferedImage(4, 4, BufferedImage.TYPE_INT_ARGB);
|
147 |
|
|
lightColor = light;
|
148 |
|
|
darkColor = dark;
|
149 |
|
|
Graphics g = pattern2D.getGraphics();
|
150 |
|
|
g.setColor(light);
|
151 |
|
|
g.fillRect(0, 0, 1, 1);
|
152 |
|
|
g.fillRect(2, 2, 1, 1);
|
153 |
|
|
g.setColor(dark);
|
154 |
|
|
g.fillRect(1, 1, 1, 1);
|
155 |
|
|
g.fillRect(3, 3, 1, 1);
|
156 |
|
|
g.dispose();
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
/**
|
160 |
|
|
* Paints the typical Metal gradient. See {@link #paintGradient(Graphics,
|
161 |
|
|
* int, int, int, int, double, double, Color, Color, Color, int)}
|
162 |
|
|
* for more details.
|
163 |
|
|
*
|
164 |
|
|
* The parameters are fetched from the UIManager using the key
|
165 |
|
|
* <code>uiProp</code>. The value is expected to be a {@link List} that
|
166 |
|
|
* contains 4 values: two {@link Double}s and 3 {@link Color} object that
|
167 |
|
|
* together make up the parameters passed to the painting method.
|
168 |
|
|
*
|
169 |
|
|
* @param g the graphics context to use
|
170 |
|
|
* @param x the X coordinate of the upper left corner of the rectangle
|
171 |
|
|
* @param y the Y coordinate of the upper left corner of the rectangle
|
172 |
|
|
* @param w the width of the rectangle
|
173 |
|
|
* @param h the height of the rectangle
|
174 |
|
|
* @param dir the direction of the gradient, either
|
175 |
|
|
* @param uiProp the key of the UIManager property that has the parameters
|
176 |
|
|
*/
|
177 |
|
|
static void paintGradient(Graphics g, int x, int y, int w, int h,
|
178 |
|
|
int dir, String uiProp)
|
179 |
|
|
{
|
180 |
|
|
List params = (List) UIManager.get(uiProp);
|
181 |
|
|
double g1 = ((Double) params.get(0)).doubleValue();
|
182 |
|
|
double g2 = ((Double) params.get(1)).doubleValue();
|
183 |
|
|
Color c1 = (Color) params.get(2);
|
184 |
|
|
Color c2 = (Color) params.get(3);
|
185 |
|
|
Color c3 = (Color) params.get(4);
|
186 |
|
|
paintGradient(g, x, y, w, h, g1, g2, c1, c2, c3, dir);
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
/**
|
190 |
|
|
* Paints the typical Metal gradient. The gradient is painted as follows:
|
191 |
|
|
* <pre>
|
192 |
|
|
*
|
193 |
|
|
* +-------+--------+--------+-----------------------------+
|
194 |
|
|
* | | | | |
|
195 |
|
|
* +-------+--------+--------+-----------------------------+
|
196 |
|
|
* c1 -> c2 -- c2 -> c1 --------> c3
|
197 |
|
|
* < -g1- > < -g2- > < -g1- >
|
198 |
|
|
* </pre>
|
199 |
|
|
*
|
200 |
|
|
* There are 4 distinct areas in this gradient:
|
201 |
|
|
* <ol>
|
202 |
|
|
* <li>A gradient from color 1 to color 2 with the relative width specified
|
203 |
|
|
* by <code>g1</code></li>
|
204 |
|
|
* <li>A solid area with the color 2 and the relative width specified by
|
205 |
|
|
* <code>g2</code></li>
|
206 |
|
|
* <li>A gradient from color 2 to color 1 with the relative width specified
|
207 |
|
|
* by <code>g1</code></li>
|
208 |
|
|
*
|
209 |
|
|
* @param g the graphics context to use
|
210 |
|
|
* @param x the X coordinate of the upper left corner of the rectangle
|
211 |
|
|
* @param y the Y coordinate of the upper left corner of the rectangle
|
212 |
|
|
* @param w the width of the rectangle
|
213 |
|
|
* @param h the height of the rectangle
|
214 |
|
|
* @param g1 the relative width of the c1->c2 gradients
|
215 |
|
|
* @param g2 the relative width of the c2 solid area
|
216 |
|
|
* @param c1 the color 1
|
217 |
|
|
* @param c2 the color 2
|
218 |
|
|
* @param c3 the color 3
|
219 |
|
|
* @param dir the direction of the gradient, either
|
220 |
|
|
* {@link SwingConstants#HORIZONTAL} or {@link SwingConstants#VERTICAL}
|
221 |
|
|
*/
|
222 |
|
|
static void paintGradient(Graphics g, int x, int y, int w, int h, double g1,
|
223 |
|
|
double g2, Color c1, Color c2, Color c3, int dir)
|
224 |
|
|
{
|
225 |
|
|
if (dir == SwingConstants.HORIZONTAL)
|
226 |
|
|
paintHorizontalGradient(g, x, y, w, h, g1, g2, c1, c2, c3);
|
227 |
|
|
else
|
228 |
|
|
paintVerticalGradient(g, x, y, w, h, g1, g2, c1, c2, c3);
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
/**
|
232 |
|
|
* Paints a horizontal gradient. See {@link #paintGradient(Graphics, int,
|
233 |
|
|
* int, int, int, double, double, Color, Color, Color, int)} for details.
|
234 |
|
|
*
|
235 |
|
|
* @param x the X coordinate of the upper left corner of the rectangle
|
236 |
|
|
* @param y the Y coordinate of the upper left corner of the rectangle
|
237 |
|
|
* @param w the width of the rectangle
|
238 |
|
|
* @param h the height of the rectangle
|
239 |
|
|
* @param g1 the relative width of the c1->c2 gradients
|
240 |
|
|
* @param g2 the relative width of the c2 solid area
|
241 |
|
|
* @param c1 the color 1
|
242 |
|
|
* @param c2 the color 2
|
243 |
|
|
* @param c3 the color 3
|
244 |
|
|
*/
|
245 |
|
|
static void paintHorizontalGradient(Graphics g, int x, int y, int w, int h,
|
246 |
|
|
double g1, double g2, Color c1, Color c2,
|
247 |
|
|
Color c3)
|
248 |
|
|
{
|
249 |
|
|
// Calculate the coordinates.
|
250 |
|
|
// The size of the first gradient area (c1->2).
|
251 |
|
|
int w1 = (int) (w * g1);
|
252 |
|
|
// The size of the solid c2 area.
|
253 |
|
|
int w2 = (int) (w * g2);
|
254 |
|
|
int x0 = x;
|
255 |
|
|
int x1 = x0 + w1;
|
256 |
|
|
int x2 = x1 + w2;
|
257 |
|
|
int x3 = x2 + w1;
|
258 |
|
|
int x4 = x + w;
|
259 |
|
|
|
260 |
|
|
// Paint first gradient area (c1->c2).
|
261 |
|
|
int xc; // The current y coordinate.
|
262 |
|
|
for (xc = x0; xc < x1; xc++)
|
263 |
|
|
{
|
264 |
|
|
if (xc > x + w)
|
265 |
|
|
break;
|
266 |
|
|
|
267 |
|
|
// Perform color interpolation;
|
268 |
|
|
double factor = (xc - x0) / (double) w1;
|
269 |
|
|
int rInt = (int) ((c2.getRed() - c1.getRed()) * factor + c1.getRed());
|
270 |
|
|
int gInt = (int) ((c2.getGreen() - c1.getGreen()) * factor
|
271 |
|
|
+ c1.getGreen());
|
272 |
|
|
int bInt = (int) ((c2.getBlue() - c1.getBlue()) * factor
|
273 |
|
|
+ c1.getBlue());
|
274 |
|
|
Color interpolated = new Color(rInt, gInt, bInt);
|
275 |
|
|
g.setColor(interpolated);
|
276 |
|
|
g.drawLine(xc, y, xc, y + h);
|
277 |
|
|
}
|
278 |
|
|
// Paint solid c2 area.
|
279 |
|
|
g.setColor(c2);
|
280 |
|
|
g.fillRect(x1, y, x2 - x1, h);
|
281 |
|
|
|
282 |
|
|
// Paint second gradient area (c2->c1).
|
283 |
|
|
for (xc = x2; xc < x3; xc++)
|
284 |
|
|
{
|
285 |
|
|
if (xc > x + w)
|
286 |
|
|
break;
|
287 |
|
|
|
288 |
|
|
// Perform color interpolation;
|
289 |
|
|
double factor = (xc - x2) / (double) w1;
|
290 |
|
|
int rInt = (int) ((c1.getRed() - c2.getRed()) * factor + c2.getRed());
|
291 |
|
|
int gInt = (int) ((c1.getGreen() - c2.getGreen()) * factor
|
292 |
|
|
+ c2.getGreen());
|
293 |
|
|
int bInt = (int) ((c1.getBlue() - c2.getBlue()) * factor
|
294 |
|
|
+ c2.getBlue());
|
295 |
|
|
Color interpolated = new Color(rInt, gInt, bInt);
|
296 |
|
|
g.setColor(interpolated);
|
297 |
|
|
g.drawLine(xc, y, xc, y + h);
|
298 |
|
|
}
|
299 |
|
|
|
300 |
|
|
// Paint third gradient area (c1->c3).
|
301 |
|
|
for (xc = x3; xc < x4; xc++)
|
302 |
|
|
{
|
303 |
|
|
if (xc > x + w)
|
304 |
|
|
break;
|
305 |
|
|
|
306 |
|
|
// Perform color interpolation;
|
307 |
|
|
double factor = (xc - x3) / (double) (x4 - x3);
|
308 |
|
|
int rInt = (int) ((c3.getRed() - c1.getRed()) * factor + c1.getRed());
|
309 |
|
|
int gInt = (int) ((c3.getGreen() - c1.getGreen()) * factor
|
310 |
|
|
+ c1.getGreen());
|
311 |
|
|
int bInt = (int) ((c3.getBlue() - c1.getBlue()) * factor
|
312 |
|
|
+ c1.getBlue());
|
313 |
|
|
Color interpolated = new Color(rInt, gInt, bInt);
|
314 |
|
|
g.setColor(interpolated);
|
315 |
|
|
g.drawLine(xc, y, xc, y + h);
|
316 |
|
|
}
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
/**
|
320 |
|
|
* Paints a vertical gradient. See {@link #paintGradient(Graphics, int, int,
|
321 |
|
|
* int, int, double, double, Color, Color, Color, int)} for details.
|
322 |
|
|
*
|
323 |
|
|
* @param x the X coordinate of the upper left corner of the rectangle
|
324 |
|
|
* @param y the Y coordinate of the upper left corner of the rectangle
|
325 |
|
|
* @param w the width of the rectangle
|
326 |
|
|
* @param h the height of the rectangle
|
327 |
|
|
* @param g1 the relative width of the c1->c2 gradients
|
328 |
|
|
* @param g2 the relative width of the c2 solid area
|
329 |
|
|
* @param c1 the color 1
|
330 |
|
|
* @param c2 the color 2
|
331 |
|
|
* @param c3 the color 3
|
332 |
|
|
*/
|
333 |
|
|
static void paintVerticalGradient(Graphics g, int x, int y, int w, int h,
|
334 |
|
|
double g1, double g2, Color c1, Color c2,
|
335 |
|
|
Color c3)
|
336 |
|
|
{
|
337 |
|
|
// Calculate the coordinates.
|
338 |
|
|
// The size of the first gradient area (c1->2).
|
339 |
|
|
int w1 = (int) (h * g1);
|
340 |
|
|
// The size of the solid c2 area.
|
341 |
|
|
int w2 = (int) (h * g2);
|
342 |
|
|
int y0 = y;
|
343 |
|
|
int y1 = y0 + w1;
|
344 |
|
|
int y2 = y1 + w2;
|
345 |
|
|
int y3 = y2 + w1;
|
346 |
|
|
int y4 = y + h;
|
347 |
|
|
|
348 |
|
|
// Paint first gradient area (c1->c2).
|
349 |
|
|
int yc; // The current y coordinate.
|
350 |
|
|
for (yc = y0; yc < y1; yc++)
|
351 |
|
|
{
|
352 |
|
|
if (yc > y + h)
|
353 |
|
|
break;
|
354 |
|
|
|
355 |
|
|
// Perform color interpolation;
|
356 |
|
|
double factor = (yc - y0) / (double) w1;
|
357 |
|
|
int rInt = (int) ((c2.getRed() - c1.getRed()) * factor + c1.getRed());
|
358 |
|
|
int gInt = (int) ((c2.getGreen() - c1.getGreen()) * factor
|
359 |
|
|
+ c1.getGreen());
|
360 |
|
|
int bInt = (int) ((c2.getBlue() - c1.getBlue()) * factor
|
361 |
|
|
+ c1.getBlue());
|
362 |
|
|
Color interpolated = new Color(rInt, gInt, bInt);
|
363 |
|
|
g.setColor(interpolated);
|
364 |
|
|
g.drawLine(x, yc, x + w, yc);
|
365 |
|
|
}
|
366 |
|
|
// Paint solid c2 area.
|
367 |
|
|
g.setColor(c2);
|
368 |
|
|
g.fillRect(x, y1, w, y2 - y1);
|
369 |
|
|
|
370 |
|
|
// Paint second gradient area (c2->c1).
|
371 |
|
|
for (yc = y2; yc < y3; yc++)
|
372 |
|
|
{
|
373 |
|
|
if (yc > y + h)
|
374 |
|
|
break;
|
375 |
|
|
|
376 |
|
|
// Perform color interpolation;
|
377 |
|
|
double factor = (yc - y2) / (double) w1;
|
378 |
|
|
int rInt = (int) ((c1.getRed() - c2.getRed()) * factor + c2.getRed());
|
379 |
|
|
int gInt = (int) ((c1.getGreen() - c2.getGreen()) * factor
|
380 |
|
|
+ c2.getGreen());
|
381 |
|
|
int bInt = (int) ((c1.getBlue() - c2.getBlue()) * factor
|
382 |
|
|
+ c2.getBlue());
|
383 |
|
|
Color interpolated = new Color(rInt, gInt, bInt);
|
384 |
|
|
g.setColor(interpolated);
|
385 |
|
|
g.drawLine(x, yc, x + w, yc);
|
386 |
|
|
}
|
387 |
|
|
|
388 |
|
|
// Paint third gradient area (c1->c3).
|
389 |
|
|
for (yc = y3; yc < y4; yc++)
|
390 |
|
|
{
|
391 |
|
|
if (yc > y + h)
|
392 |
|
|
break;
|
393 |
|
|
|
394 |
|
|
// Perform color interpolation;
|
395 |
|
|
double factor = (yc - y3) / (double) (y4 - y3);
|
396 |
|
|
int rInt = (int) ((c3.getRed() - c1.getRed()) * factor + c1.getRed());
|
397 |
|
|
int gInt = (int) ((c3.getGreen() - c1.getGreen()) * factor
|
398 |
|
|
+ c1.getGreen());
|
399 |
|
|
int bInt = (int) ((c3.getBlue() - c1.getBlue()) * factor
|
400 |
|
|
+ c1.getBlue());
|
401 |
|
|
Color interpolated = new Color(rInt, gInt, bInt);
|
402 |
|
|
g.setColor(interpolated);
|
403 |
|
|
g.drawLine(x, yc, x + w, yc);
|
404 |
|
|
}
|
405 |
|
|
}
|
406 |
|
|
}
|