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