38 |
package javax.swing.plaf.metal; |
package javax.swing.plaf.metal; |
39 |
|
|
40 |
import java.awt.Color; |
import java.awt.Color; |
41 |
|
import java.awt.Component; |
42 |
import java.awt.Graphics; |
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 |
|
|
48 |
/** |
/** |
49 |
* Some utility and helper methods for the Metal Look & Feel. |
* Some utility and helper methods for the Metal Look & Feel. |
54 |
{ |
{ |
55 |
|
|
56 |
/** |
/** |
57 |
|
* The typical metal pattern for use with Graphics2D. |
58 |
|
*/ |
59 |
|
static BufferedImage pattern2D; |
60 |
|
|
61 |
|
/** |
62 |
|
* The light color to draw the pattern. |
63 |
|
*/ |
64 |
|
static Color lightColor; |
65 |
|
|
66 |
|
/** |
67 |
|
* The dark color to draw to draw the pattern. |
68 |
|
*/ |
69 |
|
static Color darkColor; |
70 |
|
|
71 |
|
/** |
72 |
* Fills a rectangle with the typical Metal pattern. |
* Fills a rectangle with the typical Metal pattern. |
73 |
* |
* |
74 |
* @param g the <code>Graphics</code> context to use |
* @param g the <code>Graphics</code> context to use |
81 |
* @param light the light color to use |
* @param light the light color to use |
82 |
* @param dark the dark color to use |
* @param dark the dark color to use |
83 |
*/ |
*/ |
84 |
static void fillMetalPattern(Graphics g, int x, int y, int w, int h, |
static void fillMetalPattern(Component c, Graphics g, int x, int y, int w, int h, |
85 |
Color light, Color dark) |
Color light, Color dark) |
86 |
{ |
{ |
87 |
int xOff = 0; |
if (g instanceof Graphics2D) |
88 |
for (int mY = y; mY < (y + h); mY++) |
fillMetalPattern2D((Graphics2D) g, x, y, w, h, light, dark); |
89 |
|
else |
90 |
{ |
{ |
91 |
// set color alternating with every line |
int xOff = 0; |
92 |
if (((mY - y) % 2) == 0) |
for (int mY = y; mY < (y + h); mY++) |
|
g.setColor(light); |
|
|
else |
|
|
g.setColor(dark); |
|
|
|
|
|
for (int mX = x + (xOff); mX < (x + w); mX += 4) |
|
93 |
{ |
{ |
94 |
g.drawLine(mX, mY, mX, mY); |
// set color alternating with every line |
95 |
|
if (((mY - y) % 2) == 0) |
96 |
|
g.setColor(light); |
97 |
|
else |
98 |
|
g.setColor(dark); |
99 |
|
|
100 |
|
for (int mX = x + (xOff); mX < (x + w); mX += 4) |
101 |
|
{ |
102 |
|
g.drawLine(mX, mY, mX, mY); |
103 |
|
} |
104 |
|
|
105 |
|
// increase x offset |
106 |
|
xOff++; |
107 |
|
if (xOff > 3) |
108 |
|
xOff = 0; |
109 |
} |
} |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
// increase x offset |
/** |
114 |
xOff++; |
* Fills a rectangle with the typical Metal pattern using Java2D. |
115 |
if (xOff > 3) |
* |
116 |
xOff = 0; |
* @param g2d the <code>Graphics2D</code> context to use |
117 |
} |
* @param x the X coordinate of the upper left corner of the rectangle to |
118 |
|
* fill |
119 |
|
* @param y the Y coordinate of the upper left corner of the rectangle to |
120 |
|
* fill |
121 |
|
* @param w the width of the rectangle to fill |
122 |
|
* @param h the height of the rectangle to fill |
123 |
|
*/ |
124 |
|
static void fillMetalPattern2D(Graphics2D g2d, int x, int y, int w, int h, |
125 |
|
Color light, Color dark) |
126 |
|
{ |
127 |
|
if (pattern2D == null || !darkColor.equals(dark) || !lightColor.equals(light)) |
128 |
|
initializePattern(light, dark); |
129 |
|
|
130 |
|
// Prepare the texture. |
131 |
|
TexturePaint texture = |
132 |
|
new TexturePaint(pattern2D, new Rectangle2D.Double(0., 0., 4., 2.)); |
133 |
|
g2d.setPaint(texture); |
134 |
|
g2d.fillRect(x, y, w, h); |
135 |
|
} |
136 |
|
|
137 |
|
/** |
138 |
|
* Initializes the pattern image. |
139 |
|
*/ |
140 |
|
static void initializePattern(Color light, Color dark) |
141 |
|
{ |
142 |
|
pattern2D = new BufferedImage(4, 4, BufferedImage.TYPE_INT_RGB); |
143 |
|
lightColor = light; |
144 |
|
darkColor = dark; |
145 |
|
Graphics g = pattern2D.getGraphics(); |
146 |
|
g.setColor(light); |
147 |
|
g.fillRect(0, 0, 1, 1); |
148 |
|
g.fillRect(2, 2, 1, 1); |
149 |
|
g.setColor(dark); |
150 |
|
g.fillRect(1, 1, 1, 1); |
151 |
|
g.fillRect(3, 3, 1, 1); |
152 |
|
g.dispose(); |
153 |
} |
} |
154 |
} |
} |