KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SpinnerSpinnerPreviousButtonPainter.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 SpinnerSpinnerPreviousButtonPainter 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 SpinnerSpinnerPreviousButtonPainter 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_MOUSEOVER_FOCUSED = 4;
26     static final int BACKGROUND_PRESSED_FOCUSED = 5;
27     static final int BACKGROUND_MOUSEOVER = 6;
28     static final int BACKGROUND_PRESSED = 7;
29     static final int FOREGROUND_DISABLED = 8;
30     static final int FOREGROUND_ENABLED = 9;
31     static final int FOREGROUND_FOCUSED = 10;
32     static final int FOREGROUND_MOUSEOVER_FOCUSED = 11;
33     static final int FOREGROUND_PRESSED_FOCUSED = 12;
34     static final int FOREGROUND_MOUSEOVER = 13;
35     static final int FOREGROUND_PRESSED = 14;
36
37
38     private int state; //refers to one of the static final ints above
39
private PaintContext ctx;
40
41     //the following 4 variables are reused during the painting code of the layers
42
private Path2D path = new Path2D.Float();
43     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
44     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
45     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
46
47     //All Colors used for painting are stored here. Ideally, only those colors being used
48
//by a particular instance of SpinnerSpinnerPreviousButtonPainter would be created. For the moment at least,
49
//however, all are created for each instance.
50
private Color color1 = decodeColor("nimbusBase", 0.015098333f, -0.5557143f, 0.2352941f, 0);
51     private Color color2 = decodeColor("nimbusBase", 0.010237217f, -0.55799407f, 0.20784312f, 0);
52     private Color color3 = decodeColor("nimbusBase", 0.018570602f, -0.5821429f, 0.32941175f, 0);
53     private Color color4 = decodeColor("nimbusBase", 0.021348298f, -0.56722116f, 0.3098039f, 0);
54     private Color color5 = decodeColor("nimbusBase", 0.021348298f, -0.567841f, 0.31764704f, 0);
55     private Color color6 = decodeColor("nimbusBlueGrey", 0.0f, -0.0033834577f, -0.30588236f, -148);
56     private Color color7 = decodeColor("nimbusBase", 5.1498413E-4f, -0.2583558f, -0.13333336f, 0);
57     private Color color8 = decodeColor("nimbusBase", 5.1498413E-4f, -0.095173776f, -0.25882354f, 0);
58     private Color color9 = decodeColor("nimbusBase", 0.004681647f, -0.5383692f, 0.33725488f, 0);
59     private Color color10 = decodeColor("nimbusBase", -0.0017285943f, -0.44453782f, 0.25098038f, 0);
60     private Color color11 = decodeColor("nimbusBase", 5.1498413E-4f, -0.43866998f, 0.24705881f, 0);
61     private Color color12 = decodeColor("nimbusBase", 5.1498413E-4f, -0.4625541f, 0.35686272f, 0);
62     private Color color13 = decodeColor("nimbusFocus", 0.0f, 0.0f, 0.0f, 0);
63     private Color color14 = decodeColor("nimbusBase", 0.0013483167f, 0.088923395f, -0.2784314f, 0);
64     private Color color15 = decodeColor("nimbusBase", 0.059279382f, 0.3642857f, -0.43529415f, 0);
65     private Color color16 = decodeColor("nimbusBase", 0.0010585189f, -0.541452f, 0.4078431f, 0);
66     private Color color17 = decodeColor("nimbusBase", 0.00254488f, -0.4608264f, 0.32549018f, 0);
67     private Color color18 = decodeColor("nimbusBase", 5.1498413E-4f, -0.4555341f, 0.3215686f, 0);
68     private Color color19 = decodeColor("nimbusBase", 5.1498413E-4f, -0.4757143f, 0.43137252f, 0);
69     private Color color20 = decodeColor("nimbusBase", 0.061133325f, 0.3642857f, -0.427451f, 0);
70     private Color color21 = decodeColor("nimbusBase", -3.528595E-5f, 0.018606722f, -0.23137257f, 0);
71     private Color color22 = decodeColor("nimbusBase", 8.354783E-4f, -0.2578073f, 0.12549019f, 0);
72     private Color color23 = decodeColor("nimbusBase", 8.9377165E-4f, -0.01599598f, 0.007843137f, 0);
73     private Color color24 = decodeColor("nimbusBase", 0.0f, -0.00895375f, 0.007843137f, 0);
74     private Color color25 = decodeColor("nimbusBase", 8.9377165E-4f, -0.13853917f, 0.14509803f, 0);
75     private Color color26 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, -0.63529414f, -179);
76     private Color color27 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, -186);
77     private Color color28 = decodeColor("nimbusBase", 0.018570602f, -0.56714284f, 0.1372549f, 0);
78     private Color color29 = decodeColor("nimbusBase", -0.57865167f, -0.6357143f, -0.54901963f, 0);
79     private Color color30 = decodeColor("nimbusBase", 0.0f, -0.6357143f, 0.45098037f, 0);
80
81
82     //Array of current component colors, updated in each paint call
83
private Object JavaDoc[] componentColors;
84
85     public SpinnerSpinnerPreviousButtonPainter(PaintContext ctx, int state) {
86         super();
87         this.state = state;
88         this.ctx = ctx;
89     }
90
91     @Override JavaDoc
92     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
93         //populate componentColors array with colors calculated in getExtendedCacheKeys call
94
componentColors = extendedCacheKeys;
95         //generate this entire method. Each state/bg/fg/border combo that has
96
//been painted gets its own KEY and paint method.
97
switch(state) {
98             case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
99             case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
100             case BACKGROUND_FOCUSED: paintBackgroundFocused(g); break;
101             case BACKGROUND_MOUSEOVER_FOCUSED: paintBackgroundMouseOverAndFocused(g); break;
102             case BACKGROUND_PRESSED_FOCUSED: paintBackgroundPressedAndFocused(g); break;
103             case BACKGROUND_MOUSEOVER: paintBackgroundMouseOver(g); break;
104             case BACKGROUND_PRESSED: paintBackgroundPressed(g); break;
105             case FOREGROUND_DISABLED: paintForegroundDisabled(g); break;
106             case FOREGROUND_ENABLED: paintForegroundEnabled(g); break;
107             case FOREGROUND_FOCUSED: paintForegroundFocused(g); break;
108             case FOREGROUND_MOUSEOVER_FOCUSED: paintForegroundMouseOverAndFocused(g); break;
109             case FOREGROUND_PRESSED_FOCUSED: paintForegroundPressedAndFocused(g); break;
110             case FOREGROUND_MOUSEOVER: paintForegroundMouseOver(g); break;
111             case FOREGROUND_PRESSED: paintForegroundPressed(g); break;
112
113         }
114     }
115         
116
117
118     @Override JavaDoc
119     protected final PaintContext getPaintContext() {
120         return ctx;
121     }
122
123     private void paintBackgroundDisabled(Graphics2D g) {
124         path = decodePath1();
125         g.setPaint(decodeGradient1(path));
126         g.fill(path);
127         path = decodePath2();
128         g.setPaint(decodeGradient2(path));
129         g.fill(path);
130
131     }
132
133     private void paintBackgroundEnabled(Graphics2D g) {
134         path = decodePath3();
135         g.setPaint(color6);
136         g.fill(path);
137         path = decodePath1();
138         g.setPaint(decodeGradient3(path));
139         g.fill(path);
140         path = decodePath2();
141         g.setPaint(decodeGradient4(path));
142         g.fill(path);
143
144     }
145
146     private void paintBackgroundFocused(Graphics2D g) {
147         path = decodePath4();
148         g.setPaint(color13);
149         g.fill(path);
150         path = decodePath1();
151         g.setPaint(decodeGradient3(path));
152         g.fill(path);
153         path = decodePath2();
154         g.setPaint(decodeGradient4(path));
155         g.fill(path);
156
157     }
158
159     private void paintBackgroundMouseOverAndFocused(Graphics2D g) {
160         path = decodePath5();
161         g.setPaint(color13);
162         g.fill(path);
163         path = decodePath6();
164         g.setPaint(decodeGradient5(path));
165         g.fill(path);
166         path = decodePath7();
167         g.setPaint(decodeGradient6(path));
168         g.fill(path);
169
170     }
171
172     private void paintBackgroundPressedAndFocused(Graphics2D g) {
173         path = decodePath4();
174         g.setPaint(color13);
175         g.fill(path);
176         path = decodePath1();
177         g.setPaint(decodeGradient7(path));
178         g.fill(path);
179         path = decodePath2();
180         g.setPaint(decodeGradient8(path));
181         g.fill(path);
182
183     }
184
185     private void paintBackgroundMouseOver(Graphics2D g) {
186         path = decodePath3();
187         g.setPaint(color26);
188         g.fill(path);
189         path = decodePath1();
190         g.setPaint(decodeGradient5(path));
191         g.fill(path);
192         path = decodePath2();
193         g.setPaint(decodeGradient6(path));
194         g.fill(path);
195
196     }
197
198     private void paintBackgroundPressed(Graphics2D g) {
199         path = decodePath8();
200         g.setPaint(color27);
201         g.fill(path);
202         path = decodePath1();
203         g.setPaint(decodeGradient7(path));
204         g.fill(path);
205         path = decodePath2();
206         g.setPaint(decodeGradient8(path));
207         g.fill(path);
208
209     }
210
211     private void paintForegroundDisabled(Graphics2D g) {
212         path = decodePath9();
213         g.setPaint(color28);
214         g.fill(path);
215
216     }
217
218     private void paintForegroundEnabled(Graphics2D g) {
219         path = decodePath9();
220         g.setPaint(color29);
221         g.fill(path);
222
223     }
224
225     private void paintForegroundFocused(Graphics2D g) {
226         path = decodePath9();
227         g.setPaint(color29);
228         g.fill(path);
229
230     }
231
232     private void paintForegroundMouseOverAndFocused(Graphics2D g) {
233         path = decodePath9();
234         g.setPaint(color29);
235         g.fill(path);
236
237     }
238
239     private void paintForegroundPressedAndFocused(Graphics2D g) {
240         path = decodePath9();
241         g.setPaint(color30);
242         g.fill(path);
243
244     }
245
246     private void paintForegroundMouseOver(Graphics2D g) {
247         path = decodePath9();
248         g.setPaint(color29);
249         g.fill(path);
250
251     }
252
253     private void paintForegroundPressed(Graphics2D g) {
254         path = decodePath9();
255         g.setPaint(color30);
256         g.fill(path);
257
258     }
259
260
261
262     private Path2D decodePath1() {
263         path.reset();
264         path.moveTo(decodeX(0.0f), decodeY(1.0f));
265         path.lineTo(decodeX(0.0f), decodeY(2.6666667f));
266         path.lineTo(decodeX(2.142857f), decodeY(2.6666667f));
267         path.curveTo(decodeAnchorX(2.142857074737549f, 3.0f), decodeAnchorY(2.6666667461395264f, 0.0f), decodeAnchorX(2.7142858505249023f, 0.0f), decodeAnchorY(2.0f, 2.0f), decodeX(2.7142859f), decodeY(2.0f));
268         path.lineTo(decodeX(2.7142859f), decodeY(1.0f));
269         path.lineTo(decodeX(0.0f), decodeY(1.0f));
270         path.closePath();
271         return path;
272     }
273
274     private Path2D decodePath2() {
275         path.reset();
276         path.moveTo(decodeX(1.0f), decodeY(1.0f));
277         path.lineTo(decodeX(1.0f), decodeY(2.5f));
278         path.lineTo(decodeX(2.142857f), decodeY(2.5f));
279         path.curveTo(decodeAnchorX(2.142857074737549f, 2.0f), decodeAnchorY(2.5f, 0.0f), decodeAnchorX(2.5714285373687744f, 0.0f), decodeAnchorY(2.0f, 1.0f), decodeX(2.5714285f), decodeY(2.0f));
280         path.lineTo(decodeX(2.5714285f), decodeY(1.0f));
281         path.lineTo(decodeX(1.0f), decodeY(1.0f));
282         path.closePath();
283         return path;
284     }
285
286     private Path2D decodePath3() {
287         path.reset();
288         path.moveTo(decodeX(0.0f), decodeY(2.6666667f));
289         path.lineTo(decodeX(0.0f), decodeY(2.8333333f));
290         path.lineTo(decodeX(2.0324676f), decodeY(2.8333333f));
291         path.curveTo(decodeAnchorX(2.0324676036834717f, 2.1136363636363793f), decodeAnchorY(2.8333332538604736f, 0.0f), decodeAnchorX(2.7142858505249023f, 0.0f), decodeAnchorY(2.0f, 3.0f), decodeX(2.7142859f), decodeY(2.0f));
292         path.lineTo(decodeX(0.0f), decodeY(2.6666667f));
293         path.closePath();
294         return path;
295     }
296
297     private Path2D decodePath4() {
298         path.reset();
299         path.moveTo(decodeX(0.0f), decodeY(1.0f));
300         path.lineTo(decodeX(0.0f), decodeY(2.8999999f));
301         path.lineTo(decodeX(2.2f), decodeY(2.8999999f));
302         path.curveTo(decodeAnchorX(2.200000047683716f, 2.9999999999999982f), decodeAnchorY(2.8999998569488525f, 0.0f), decodeAnchorX(2.914285659790039f, 0.0f), decodeAnchorY(2.2333333492279053f, 3.0f), decodeX(2.9142857f), decodeY(2.2333333f));
303         path.lineTo(decodeX(2.9142857f), decodeY(1.0f));
304         path.lineTo(decodeX(0.0f), decodeY(1.0f));
305         path.closePath();
306         return path;
307     }
308
309     private Path2D decodePath5() {
310         path.reset();
311         path.moveTo(decodeX(0.0f), decodeY(0.0f));
312         path.lineTo(decodeX(0.0f), decodeY(2.8999999f));
313         path.lineTo(decodeX(2.2f), decodeY(2.8999999f));
314         path.curveTo(decodeAnchorX(2.200000047683716f, 2.9999999999999982f), decodeAnchorY(2.8999998569488525f, 0.0f), decodeAnchorX(2.914285659790039f, 0.0f), decodeAnchorY(2.2333333492279053f, 3.0f), decodeX(2.9142857f), decodeY(2.2333333f));
315         path.lineTo(decodeX(2.9142857f), decodeY(0.0f));
316         path.lineTo(decodeX(0.0f), decodeY(0.0f));
317         path.closePath();
318         return path;
319     }
320
321     private Path2D decodePath6() {
322         path.reset();
323         path.moveTo(decodeX(0.0f), decodeY(0.0f));
324         path.lineTo(decodeX(0.0f), decodeY(2.6666667f));
325         path.lineTo(decodeX(2.142857f), decodeY(2.6666667f));
326         path.curveTo(decodeAnchorX(2.142857074737549f, 3.0f), decodeAnchorY(2.6666667461395264f, 0.0f), decodeAnchorX(2.7142858505249023f, 0.0f), decodeAnchorY(2.0f, 2.0f), decodeX(2.7142859f), decodeY(2.0f));
327         path.lineTo(decodeX(2.7142859f), decodeY(0.0f));
328         path.lineTo(decodeX(0.0f), decodeY(0.0f));
329         path.closePath();
330         return path;
331     }
332
333     private Path2D decodePath7() {
334         path.reset();
335         path.moveTo(decodeX(1.0f), decodeY(0.0f));
336         path.lineTo(decodeX(1.0f), decodeY(2.5f));
337         path.lineTo(decodeX(2.142857f), decodeY(2.5f));
338         path.curveTo(decodeAnchorX(2.142857074737549f, 2.0f), decodeAnchorY(2.5f, 0.0f), decodeAnchorX(2.5714285373687744f, 0.0f), decodeAnchorY(2.0f, 1.0f), decodeX(2.5714285f), decodeY(2.0f));
339         path.lineTo(decodeX(2.5714285f), decodeY(0.0f));
340         path.lineTo(decodeX(1.0f), decodeY(0.0f));
341         path.closePath();
342         return path;
343     }
344
345     private Path2D decodePath8() {
346         path.reset();
347         path.moveTo(decodeX(0.0f), decodeY(2.6666667f));
348         path.lineTo(decodeX(0.0f), decodeY(2.8333333f));
349         path.curveTo(decodeAnchorX(0.0f, 0.0f), decodeAnchorY(2.8333332538604736f, 0.0f), decodeAnchorX(2.0324676036834717f, -2.1136363636363793f), decodeAnchorY(2.8333332538604736f, 0.0f), decodeX(2.0324676f), decodeY(2.8333333f));
350         path.curveTo(decodeAnchorX(2.0324676036834717f, 2.1136363636363793f), decodeAnchorY(2.8333332538604736f, 0.0f), decodeAnchorX(2.7142858505249023f, 0.0f), decodeAnchorY(2.0f, 3.0f), decodeX(2.7142859f), decodeY(2.0f));
351         path.lineTo(decodeX(0.0f), decodeY(2.6666667f));
352         path.closePath();
353         return path;
354     }
355
356     private Path2D decodePath9() {
357         path.reset();
358         path.moveTo(decodeX(1.0f), decodeY(1.0f));
359         path.lineTo(decodeX(1.5045455f), decodeY(1.9943181f));
360         path.lineTo(decodeX(2.0f), decodeY(1.0f));
361         path.lineTo(decodeX(1.0f), decodeY(1.0f));
362         path.closePath();
363         return path;
364     }
365
366
367
368     private Paint decodeGradient1(Shape s) {
369         Rectangle2D bounds = s.getBounds2D();
370         float x = (float)bounds.getX();
371         float y = (float)bounds.getY();
372         float w = (float)bounds.getWidth();
373         float h = (float)bounds.getHeight();
374         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
375                 new float[] { 0.0f,0.5f,1.0f },
376                 new Color[] { color1,
377                             decodeColor(color1,color2,0.5f),
378                             color2});
379     }
380
381     private Paint decodeGradient2(Shape s) {
382         Rectangle2D bounds = s.getBounds2D();
383         float x = (float)bounds.getX();
384         float y = (float)bounds.getY();
385         float w = (float)bounds.getWidth();
386         float h = (float)bounds.getHeight();
387         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
388                 new float[] { 0.0f,0.05748663f,0.11497326f,0.55748665f,1.0f },
389                 new Color[] { color3,
390                             decodeColor(color3,color4,0.5f),
391                             color4,
392                             decodeColor(color4,color5,0.5f),
393                             color5});
394     }
395
396     private Paint decodeGradient3(Shape s) {
397         Rectangle2D bounds = s.getBounds2D();
398         float x = (float)bounds.getX();
399         float y = (float)bounds.getY();
400         float w = (float)bounds.getWidth();
401         float h = (float)bounds.getHeight();
402         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
403                 new float[] { 0.0f,0.5f,1.0f },
404                 new Color[] { color7,
405                             decodeColor(color7,color8,0.5f),
406                             color8});
407     }
408
409     private Paint decodeGradient4(Shape s) {
410         Rectangle2D bounds = s.getBounds2D();
411         float x = (float)bounds.getX();
412         float y = (float)bounds.getY();
413         float w = (float)bounds.getWidth();
414         float h = (float)bounds.getHeight();
415         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
416                 new float[] { 0.0f,0.05748663f,0.11497326f,0.2419786f,0.36898395f,0.684492f,1.0f },
417                 new Color[] { color9,
418                             decodeColor(color9,color10,0.5f),
419                             color10,
420                             decodeColor(color10,color11,0.5f),
421                             color11,
422                             decodeColor(color11,color12,0.5f),
423                             color12});
424     }
425
426     private Paint decodeGradient5(Shape s) {
427         Rectangle2D bounds = s.getBounds2D();
428         float x = (float)bounds.getX();
429         float y = (float)bounds.getY();
430         float w = (float)bounds.getWidth();
431         float h = (float)bounds.getHeight();
432         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
433                 new float[] { 0.0f,0.5f,1.0f },
434                 new Color[] { color14,
435                             decodeColor(color14,color15,0.5f),
436                             color15});
437     }
438
439     private Paint decodeGradient6(Shape s) {
440         Rectangle2D bounds = s.getBounds2D();
441         float x = (float)bounds.getX();
442         float y = (float)bounds.getY();
443         float w = (float)bounds.getWidth();
444         float h = (float)bounds.getHeight();
445         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
446                 new float[] { 0.0f,0.05748663f,0.11497326f,0.2419786f,0.36898395f,0.684492f,1.0f },
447                 new Color[] { color16,
448                             decodeColor(color16,color17,0.5f),
449                             color17,
450                             decodeColor(color17,color18,0.5f),
451                             color18,
452                             decodeColor(color18,color19,0.5f),
453                             color19});
454     }
455
456     private Paint decodeGradient7(Shape s) {
457         Rectangle2D bounds = s.getBounds2D();
458         float x = (float)bounds.getX();
459         float y = (float)bounds.getY();
460         float w = (float)bounds.getWidth();
461         float h = (float)bounds.getHeight();
462         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
463                 new float[] { 0.0f,0.5f,1.0f },
464                 new Color[] { color20,
465                             decodeColor(color20,color21,0.5f),
466                             color21});
467     }
468
469     private Paint decodeGradient8(Shape s) {
470         Rectangle2D bounds = s.getBounds2D();
471         float x = (float)bounds.getX();
472         float y = (float)bounds.getY();
473         float w = (float)bounds.getWidth();
474         float h = (float)bounds.getHeight();
475         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
476                 new float[] { 0.0f,0.05748663f,0.11497326f,0.2419786f,0.36898395f,0.684492f,1.0f },
477                 new Color[] { color22,
478                             decodeColor(color22,color23,0.5f),
479                             color23,
480                             decodeColor(color23,color24,0.5f),
481                             color24,
482                             decodeColor(color24,color25,0.5f),
483                             color25});
484     }
485
486
487 }
488
Popular Tags