KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > nimbus > SpinnerPanelSpinnerFormattedTextFieldPainter


1 /*
2  * SpinnerPanelSpinnerFormattedTextFieldPainter.java %E%
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.nimbus;
8
9 import java.awt.*;
10 import java.awt.geom.*;
11 import java.awt.image.*;
12 import javax.swing.*;
13 import com.sun.java.swing.Painter;
14
15 /**
16  */

17 public final class SpinnerPanelSpinnerFormattedTextFieldPainter extends AbstractRegionPainter {
18     //package private integers representing the available states that
19
//this painter will paint. These are used when creating a new instance
20
//of SpinnerPanelSpinnerFormattedTextFieldPainter to determine which region/state is being painted
21
//by that instance.
22
static final int BACKGROUND_DISABLED = 1;
23     static final int BACKGROUND_ENABLED = 2;
24     static final int BACKGROUND_FOCUSED = 3;
25     static final int BACKGROUND_SELECTED = 4;
26     static final int BACKGROUND_SELECTED_FOCUSED = 5;
27
28
29     private int state; //refers to one of the static final ints above
30
private PaintContext ctx;
31
32     //the following 4 variables are reused during the painting code of the layers
33
private Path2D path = new Path2D.Float();
34     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
35     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
36     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
37
38     //All Colors used for painting are stored here. Ideally, only those colors being used
39
//by a particular instance of SpinnerPanelSpinnerFormattedTextFieldPainter would be created. For the moment at least,
40
//however, all are created for each instance.
41
private Color color1 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, -0.74509805f, -237);
42     private Color color2 = decodeColor("nimbusBlueGrey", -0.006944418f, -0.07187897f, 0.06666666f, 0);
43     private Color color3 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07703349f, 0.0745098f, 0);
44     private Color color4 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07968931f, 0.14509803f, 0);
45     private Color color5 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07856284f, 0.11372548f, 0);
46     private Color color6 = decodeColor("nimbusBase", 0.040395975f, -0.60315615f, 0.29411763f, 0);
47     private Color color7 = decodeColor("nimbusBase", 0.016586483f, -0.6051466f, 0.3490196f, 0);
48     private Color color8 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.0965403f, -0.18431371f, 0);
49     private Color color9 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.1048766f, -0.08f, 0);
50     private Color color10 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.105624355f, 0.054901958f, 0);
51     private Color color11 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, 0);
52     private Color color12 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.105344966f, 0.011764705f, 0);
53     private Color color13 = decodeColor("nimbusLightBackground", 0.0f, 0.0f, 0.0f, 0);
54     private Color color14 = decodeColor("nimbusFocus", 0.0f, 0.0f, 0.0f, 0);
55     private Color color15 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.1048766f, -0.05098039f, 0);
56
57
58     //Array of current component colors, updated in each paint call
59
private Object JavaDoc[] componentColors;
60
61     public SpinnerPanelSpinnerFormattedTextFieldPainter(PaintContext ctx, int state) {
62         super();
63         this.state = state;
64         this.ctx = ctx;
65     }
66
67     @Override JavaDoc
68     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
69         //populate componentColors array with colors calculated in getExtendedCacheKeys call
70
componentColors = extendedCacheKeys;
71         //generate this entire method. Each state/bg/fg/border combo that has
72
//been painted gets its own KEY and paint method.
73
switch(state) {
74             case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
75             case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
76             case BACKGROUND_FOCUSED: paintBackgroundFocused(g); break;
77             case BACKGROUND_SELECTED: paintBackgroundSelected(g); break;
78             case BACKGROUND_SELECTED_FOCUSED: paintBackgroundSelectedAndFocused(g); break;
79
80         }
81     }
82         
83
84
85     @Override JavaDoc
86     protected final PaintContext getPaintContext() {
87         return ctx;
88     }
89
90     private void paintBackgroundDisabled(Graphics2D g) {
91         rect = decodeRect1();
92         g.setPaint(color1);
93         g.fill(rect);
94         rect = decodeRect2();
95         g.setPaint(decodeGradient1(rect));
96         g.fill(rect);
97         rect = decodeRect3();
98         g.setPaint(decodeGradient2(rect));
99         g.fill(rect);
100         rect = decodeRect4();
101         g.setPaint(color6);
102         g.fill(rect);
103         rect = decodeRect5();
104         g.setPaint(color7);
105         g.fill(rect);
106
107     }
108
109     private void paintBackgroundEnabled(Graphics2D g) {
110         rect = decodeRect1();
111         g.setPaint(color1);
112         g.fill(rect);
113         rect = decodeRect2();
114         g.setPaint(decodeGradient3(rect));
115         g.fill(rect);
116         rect = decodeRect3();
117         g.setPaint(decodeGradient4(rect));
118         g.fill(rect);
119         rect = decodeRect4();
120         g.setPaint(color12);
121         g.fill(rect);
122         rect = decodeRect5();
123         g.setPaint(color13);
124         g.fill(rect);
125
126     }
127
128     private void paintBackgroundFocused(Graphics2D g) {
129         rect = decodeRect6();
130         g.setPaint(color14);
131         g.fill(rect);
132         rect = decodeRect2();
133         g.setPaint(decodeGradient5(rect));
134         g.fill(rect);
135         rect = decodeRect3();
136         g.setPaint(decodeGradient4(rect));
137         g.fill(rect);
138         rect = decodeRect4();
139         g.setPaint(color12);
140         g.fill(rect);
141         rect = decodeRect5();
142         g.setPaint(color13);
143         g.fill(rect);
144
145     }
146
147     private void paintBackgroundSelected(Graphics2D g) {
148         rect = decodeRect1();
149         g.setPaint(color1);
150         g.fill(rect);
151         rect = decodeRect2();
152         g.setPaint(decodeGradient3(rect));
153         g.fill(rect);
154         rect = decodeRect3();
155         g.setPaint(decodeGradient4(rect));
156         g.fill(rect);
157         rect = decodeRect4();
158         g.setPaint(color12);
159         g.fill(rect);
160         rect = decodeRect5();
161         g.setPaint(color13);
162         g.fill(rect);
163
164     }
165
166     private void paintBackgroundSelectedAndFocused(Graphics2D g) {
167         rect = decodeRect6();
168         g.setPaint(color14);
169         g.fill(rect);
170         rect = decodeRect2();
171         g.setPaint(decodeGradient5(rect));
172         g.fill(rect);
173         rect = decodeRect3();
174         g.setPaint(decodeGradient4(rect));
175         g.fill(rect);
176         rect = decodeRect4();
177         g.setPaint(color12);
178         g.fill(rect);
179         rect = decodeRect5();
180         g.setPaint(color13);
181         g.fill(rect);
182
183     }
184
185
186
187     private Rectangle2D decodeRect1() {
188             rect.setRect(decodeX(0.6666667f), //x
189
decodeY(2.3333333f), //y
190
decodeX(3.0f) - decodeX(0.6666667f), //width
191
decodeY(2.6666667f) - decodeY(2.3333333f)); //height
192
return rect;
193     }
194
195     private Rectangle2D decodeRect2() {
196             rect.setRect(decodeX(0.6666667f), //x
197
decodeY(0.4f), //y
198
decodeX(3.0f) - decodeX(0.6666667f), //width
199
decodeY(1.0f) - decodeY(0.4f)); //height
200
return rect;
201     }
202
203     private Rectangle2D decodeRect3() {
204             rect.setRect(decodeX(1.0f), //x
205
decodeY(0.6f), //y
206
decodeX(3.0f) - decodeX(1.0f), //width
207
decodeY(1.0f) - decodeY(0.6f)); //height
208
return rect;
209     }
210
211     private Rectangle2D decodeRect4() {
212             rect.setRect(decodeX(0.6666667f), //x
213
decodeY(1.0f), //y
214
decodeX(3.0f) - decodeX(0.6666667f), //width
215
decodeY(2.3333333f) - decodeY(1.0f)); //height
216
return rect;
217     }
218
219     private Rectangle2D decodeRect5() {
220             rect.setRect(decodeX(1.0f), //x
221
decodeY(1.0f), //y
222
decodeX(3.0f) - decodeX(1.0f), //width
223
decodeY(2.0f) - decodeY(1.0f)); //height
224
return rect;
225     }
226
227     private Rectangle2D decodeRect6() {
228             rect.setRect(decodeX(0.22222222f), //x
229
decodeY(0.13333334f), //y
230
decodeX(2.916668f) - decodeX(0.22222222f), //width
231
decodeY(2.75f) - decodeY(0.13333334f)); //height
232
return rect;
233     }
234
235
236
237     private Paint decodeGradient1(Shape s) {
238         Rectangle2D bounds = s.getBounds2D();
239         float x = (float)bounds.getX();
240         float y = (float)bounds.getY();
241         float w = (float)bounds.getWidth();
242         float h = (float)bounds.getHeight();
243         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
244                 new float[] { 0.0f,0.5f,1.0f },
245                 new Color[] { color2,
246                             decodeColor(color2,color3,0.5f),
247                             color3});
248     }
249
250     private Paint decodeGradient2(Shape s) {
251         Rectangle2D bounds = s.getBounds2D();
252         float x = (float)bounds.getX();
253         float y = (float)bounds.getY();
254         float w = (float)bounds.getWidth();
255         float h = (float)bounds.getHeight();
256         return decodeGradient((0.5f * w) + x, (1.0f * h) + y, (0.5f * w) + x, (0.0f * h) + y,
257                 new float[] { 0.0f,0.5f,1.0f },
258                 new Color[] { color4,
259                             decodeColor(color4,color5,0.5f),
260                             color5});
261     }
262
263     private Paint decodeGradient3(Shape s) {
264         Rectangle2D bounds = s.getBounds2D();
265         float x = (float)bounds.getX();
266         float y = (float)bounds.getY();
267         float w = (float)bounds.getWidth();
268         float h = (float)bounds.getHeight();
269         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
270                 new float[] { 0.0f,0.49573863f,0.99147725f },
271                 new Color[] { color8,
272                             decodeColor(color8,color9,0.5f),
273                             color9});
274     }
275
276     private Paint decodeGradient4(Shape s) {
277         Rectangle2D bounds = s.getBounds2D();
278         float x = (float)bounds.getX();
279         float y = (float)bounds.getY();
280         float w = (float)bounds.getWidth();
281         float h = (float)bounds.getHeight();
282         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
283                 new float[] { 0.0f,0.1684492f,1.0f },
284                 new Color[] { color10,
285                             decodeColor(color10,color11,0.5f),
286                             color11});
287     }
288
289     private Paint decodeGradient5(Shape s) {
290         Rectangle2D bounds = s.getBounds2D();
291         float x = (float)bounds.getX();
292         float y = (float)bounds.getY();
293         float w = (float)bounds.getWidth();
294         float h = (float)bounds.getHeight();
295         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
296                 new float[] { 0.0f,0.49573863f,0.99147725f },
297                 new Color[] { color8,
298                             decodeColor(color8,color15,0.5f),
299                             color15});
300     }
301
302
303 }
304
Popular Tags