KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ComboBoxComboBoxArrowButtonPainter.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 ComboBoxComboBoxArrowButtonPainter 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 ComboBoxComboBoxArrowButtonPainter 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_ENABLED_MOUSEOVER = 3;
25     static final int BACKGROUND_ENABLED_PRESSED = 4;
26     static final int BACKGROUND_DISABLED_EDITABLE = 5;
27     static final int BACKGROUND_ENABLED_EDITABLE = 6;
28     static final int BACKGROUND_MOUSEOVER_EDITABLE = 7;
29     static final int BACKGROUND_PRESSED_EDITABLE = 8;
30     static final int BACKGROUND_SELECTED_EDITABLE = 9;
31     static final int FOREGROUND_ENABLED = 10;
32     static final int FOREGROUND_MOUSEOVER = 11;
33     static final int FOREGROUND_DISABLED = 12;
34     static final int FOREGROUND_PRESSED = 13;
35     static final int FOREGROUND_SELECTED = 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 ComboBoxComboBoxArrowButtonPainter would be created. For the moment at least,
49
//however, all are created for each instance.
50
private Color color1 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, -0.74509805f, -247);
51     private Color color2 = decodeColor("nimbusBase", 0.021348298f, -0.56289876f, 0.2588235f, 0);
52     private Color color3 = decodeColor("nimbusBase", 0.010237217f, -0.55799407f, 0.20784312f, 0);
53     private Color color4 = new Color(255, 200, 0, 255);
54     private Color color5 = decodeColor("nimbusBase", 0.021348298f, -0.59223604f, 0.35294116f, 0);
55     private Color color6 = decodeColor("nimbusBase", 0.02391243f, -0.5774183f, 0.32549018f, 0);
56     private Color color7 = decodeColor("nimbusBase", 0.021348298f, -0.56722116f, 0.3098039f, 0);
57     private Color color8 = decodeColor("nimbusBase", 0.021348298f, -0.567841f, 0.31764704f, 0);
58     private Color color9 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, -0.74509805f, -191);
59     private Color color10 = decodeColor("nimbusBase", 5.1498413E-4f, -0.34585923f, -0.007843137f, 0);
60     private Color color11 = decodeColor("nimbusBase", 5.1498413E-4f, -0.095173776f, -0.25882354f, 0);
61     private Color color12 = decodeColor("nimbusBase", 0.004681647f, -0.6197143f, 0.43137252f, 0);
62     private Color color13 = decodeColor("nimbusBase", 0.0023007393f, -0.46825016f, 0.27058822f, 0);
63     private Color color14 = decodeColor("nimbusBase", 5.1498413E-4f, -0.43866998f, 0.24705881f, 0);
64     private Color color15 = decodeColor("nimbusBase", 5.1498413E-4f, -0.4625541f, 0.35686272f, 0);
65     private Color color16 = decodeColor("nimbusBase", 0.0013483167f, -0.1769987f, -0.12156865f, 0);
66     private Color color17 = decodeColor("nimbusBase", 0.059279382f, 0.3642857f, -0.43529415f, 0);
67     private Color color18 = decodeColor("nimbusBase", 0.004681647f, -0.6198413f, 0.43921566f, 0);
68     private Color color19 = decodeColor("nimbusBase", 0.0023007393f, -0.48084703f, 0.33725488f, 0);
69     private Color color20 = decodeColor("nimbusBase", 5.1498413E-4f, -0.4555341f, 0.3215686f, 0);
70     private Color color21 = decodeColor("nimbusBase", 5.1498413E-4f, -0.4757143f, 0.43137252f, 0);
71     private Color color22 = decodeColor("nimbusBase", -0.57865167f, -0.6357143f, -0.54901963f, 0);
72     private Color color23 = decodeColor("nimbusBase", -3.528595E-5f, 0.018606722f, -0.23137257f, 0);
73     private Color color24 = decodeColor("nimbusBase", -4.2033195E-4f, -0.38050595f, 0.20392156f, 0);
74     private Color color25 = decodeColor("nimbusBase", 7.13408E-4f, -0.064285696f, 0.027450979f, 0);
75     private Color color26 = decodeColor("nimbusBase", 0.0f, -0.00895375f, 0.007843137f, 0);
76     private Color color27 = decodeColor("nimbusBase", 8.9377165E-4f, -0.13853917f, 0.14509803f, 0);
77     private Color color28 = decodeColor("nimbusBase", -0.57865167f, -0.6357143f, -0.37254906f, 0);
78     private Color color29 = decodeColor("nimbusBase", -0.57865167f, -0.6357143f, -0.5254902f, 0);
79     private Color color30 = decodeColor("nimbusBase", 0.027408898f, -0.57391655f, 0.1490196f, 0);
80     private Color color31 = decodeColor("nimbusBase", 0.0f, -0.6357143f, 0.45098037f, 0);
81
82
83     //Array of current component colors, updated in each paint call
84
private Object JavaDoc[] componentColors;
85
86     public ComboBoxComboBoxArrowButtonPainter(PaintContext ctx, int state) {
87         super();
88         this.state = state;
89         this.ctx = ctx;
90     }
91
92     @Override JavaDoc
93     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
94         //populate componentColors array with colors calculated in getExtendedCacheKeys call
95
componentColors = extendedCacheKeys;
96         //generate this entire method. Each state/bg/fg/border combo that has
97
//been painted gets its own KEY and paint method.
98
switch(state) {
99             case BACKGROUND_DISABLED_EDITABLE: paintBackgroundDisabledAndEditable(g); break;
100             case BACKGROUND_ENABLED_EDITABLE: paintBackgroundEnabledAndEditable(g); break;
101             case BACKGROUND_MOUSEOVER_EDITABLE: paintBackgroundMouseOverAndEditable(g); break;
102             case BACKGROUND_PRESSED_EDITABLE: paintBackgroundPressedAndEditable(g); break;
103             case BACKGROUND_SELECTED_EDITABLE: paintBackgroundSelectedAndEditable(g); break;
104             case FOREGROUND_ENABLED: paintForegroundEnabled(g); break;
105             case FOREGROUND_MOUSEOVER: paintForegroundMouseOver(g); break;
106             case FOREGROUND_DISABLED: paintForegroundDisabled(g); break;
107             case FOREGROUND_PRESSED: paintForegroundPressed(g); break;
108             case FOREGROUND_SELECTED: paintForegroundSelected(g); break;
109
110         }
111     }
112         
113
114
115     @Override JavaDoc
116     protected final PaintContext getPaintContext() {
117         return ctx;
118     }
119
120     private void paintBackgroundDisabledAndEditable(Graphics2D g) {
121         path = decodePath1();
122         g.setPaint(color1);
123         g.fill(path);
124         path = decodePath2();
125         g.setPaint(decodeGradient1(path));
126         g.fill(path);
127         path = decodePath3();
128         g.setPaint(color4);
129         g.fill(path);
130         path = decodePath4();
131         g.setPaint(decodeGradient2(path));
132         g.fill(path);
133
134     }
135
136     private void paintBackgroundEnabledAndEditable(Graphics2D g) {
137         path = decodePath1();
138         g.setPaint(color9);
139         g.fill(path);
140         path = decodePath2();
141         g.setPaint(decodeGradient3(path));
142         g.fill(path);
143         path = decodePath3();
144         g.setPaint(color4);
145         g.fill(path);
146         path = decodePath4();
147         g.setPaint(decodeGradient4(path));
148         g.fill(path);
149
150     }
151
152     private void paintBackgroundMouseOverAndEditable(Graphics2D g) {
153         path = decodePath1();
154         g.setPaint(color9);
155         g.fill(path);
156         path = decodePath2();
157         g.setPaint(decodeGradient5(path));
158         g.fill(path);
159         path = decodePath3();
160         g.setPaint(color4);
161         g.fill(path);
162         path = decodePath4();
163         g.setPaint(decodeGradient6(path));
164         g.fill(path);
165
166     }
167
168     private void paintBackgroundPressedAndEditable(Graphics2D g) {
169         path = decodePath1();
170         g.setPaint(color9);
171         g.fill(path);
172         path = decodePath2();
173         g.setPaint(decodeGradient7(path));
174         g.fill(path);
175         path = decodePath3();
176         g.setPaint(color4);
177         g.fill(path);
178         path = decodePath4();
179         g.setPaint(decodeGradient8(path));
180         g.fill(path);
181
182     }
183
184     private void paintBackgroundSelectedAndEditable(Graphics2D g) {
185         path = decodePath1();
186         g.setPaint(color9);
187         g.fill(path);
188         path = decodePath2();
189         g.setPaint(decodeGradient7(path));
190         g.fill(path);
191         path = decodePath3();
192         g.setPaint(color4);
193         g.fill(path);
194         path = decodePath4();
195         g.setPaint(decodeGradient8(path));
196         g.fill(path);
197
198     }
199
200     private void paintForegroundEnabled(Graphics2D g) {
201         path = decodePath5();
202         g.setPaint(decodeGradient9(path));
203         g.fill(path);
204
205     }
206
207     private void paintForegroundMouseOver(Graphics2D g) {
208         path = decodePath6();
209         g.setPaint(decodeGradient9(path));
210         g.fill(path);
211
212     }
213
214     private void paintForegroundDisabled(Graphics2D g) {
215         path = decodePath7();
216         g.setPaint(color30);
217         g.fill(path);
218
219     }
220
221     private void paintForegroundPressed(Graphics2D g) {
222         path = decodePath8();
223         g.setPaint(color31);
224         g.fill(path);
225
226     }
227
228     private void paintForegroundSelected(Graphics2D g) {
229         path = decodePath7();
230         g.setPaint(color31);
231         g.fill(path);
232
233     }
234
235
236
237     private Path2D decodePath1() {
238         path.reset();
239         path.moveTo(decodeX(0.0f), decodeY(2.0f));
240         path.lineTo(decodeX(2.75f), decodeY(2.0f));
241         path.lineTo(decodeX(2.75f), decodeY(2.25f));
242         path.curveTo(decodeAnchorX(2.75f, 0.0f), decodeAnchorY(2.25f, 4.0f), decodeAnchorX(2.125f, 3.0f), decodeAnchorY(2.875f, 0.0f), decodeX(2.125f), decodeY(2.875f));
243         path.lineTo(decodeX(0.0f), decodeY(2.875f));
244         path.lineTo(decodeX(0.0f), decodeY(2.0f));
245         path.closePath();
246         return path;
247     }
248
249     private Path2D decodePath2() {
250         path.reset();
251         path.moveTo(decodeX(0.0f), decodeY(0.25f));
252         path.lineTo(decodeX(2.125f), decodeY(0.25f));
253         path.curveTo(decodeAnchorX(2.125f, 3.0f), decodeAnchorY(0.25f, 0.0f), decodeAnchorX(2.75f, 0.0f), decodeAnchorY(0.875f, -3.0f), decodeX(2.75f), decodeY(0.875f));
254         path.lineTo(decodeX(2.75f), decodeY(2.125f));
255         path.curveTo(decodeAnchorX(2.75f, 0.0f), decodeAnchorY(2.125f, 3.0f), decodeAnchorX(2.125f, 3.0f), decodeAnchorY(2.75f, 0.0f), decodeX(2.125f), decodeY(2.75f));
256         path.lineTo(decodeX(0.0f), decodeY(2.75f));
257         path.lineTo(decodeX(0.0f), decodeY(0.25f));
258         path.closePath();
259         return path;
260     }
261
262     private Path2D decodePath3() {
263         path.reset();
264         path.moveTo(decodeX(0.85294116f), decodeY(2.639706f));
265         path.lineTo(decodeX(0.85294116f), decodeY(2.639706f));
266         path.closePath();
267         return path;
268     }
269
270     private Path2D decodePath4() {
271         path.reset();
272         path.moveTo(decodeX(1.0f), decodeY(0.375f));
273         path.lineTo(decodeX(2.0f), decodeY(0.375f));
274         path.curveTo(decodeAnchorX(2.0f, 4.0f), decodeAnchorY(0.375f, 0.0f), decodeAnchorX(2.625f, 0.0f), decodeAnchorY(1.0f, -4.0f), decodeX(2.625f), decodeY(1.0f));
275         path.lineTo(decodeX(2.625f), decodeY(2.0f));
276         path.curveTo(decodeAnchorX(2.625f, 0.0f), decodeAnchorY(2.0f, 4.0f), decodeAnchorX(2.0f, 4.0f), decodeAnchorY(2.625f, 0.0f), decodeX(2.0f), decodeY(2.625f));
277         path.lineTo(decodeX(1.0f), decodeY(2.625f));
278         path.lineTo(decodeX(1.0f), decodeY(0.375f));
279         path.closePath();
280         return path;
281     }
282
283     private Path2D decodePath5() {
284         path.reset();
285         path.moveTo(decodeX(0.9995915f), decodeY(1.3616071f));
286         path.lineTo(decodeX(2.0f), decodeY(0.8333333f));
287         path.lineTo(decodeX(2.0f), decodeY(1.8571429f));
288         path.lineTo(decodeX(0.9995915f), decodeY(1.3616071f));
289         path.closePath();
290         return path;
291     }
292
293     private Path2D decodePath6() {
294         path.reset();
295         path.moveTo(decodeX(1.00625f), decodeY(1.3526785f));
296         path.lineTo(decodeX(2.0f), decodeY(0.8333333f));
297         path.lineTo(decodeX(2.0f), decodeY(1.8571429f));
298         path.lineTo(decodeX(1.00625f), decodeY(1.3526785f));
299         path.closePath();
300         return path;
301     }
302
303     private Path2D decodePath7() {
304         path.reset();
305         path.moveTo(decodeX(1.0117648f), decodeY(1.3616071f));
306         path.lineTo(decodeX(2.0f), decodeY(0.8333333f));
307         path.lineTo(decodeX(2.0f), decodeY(1.8571429f));
308         path.lineTo(decodeX(1.0117648f), decodeY(1.3616071f));
309         path.closePath();
310         return path;
311     }
312
313     private Path2D decodePath8() {
314         path.reset();
315         path.moveTo(decodeX(1.0242647f), decodeY(1.3526785f));
316         path.lineTo(decodeX(2.0f), decodeY(0.8333333f));
317         path.lineTo(decodeX(2.0f), decodeY(1.8571429f));
318         path.lineTo(decodeX(1.0242647f), decodeY(1.3526785f));
319         path.closePath();
320         return path;
321     }
322
323
324
325     private Paint decodeGradient1(Shape s) {
326         Rectangle2D bounds = s.getBounds2D();
327         float x = (float)bounds.getX();
328         float y = (float)bounds.getY();
329         float w = (float)bounds.getWidth();
330         float h = (float)bounds.getHeight();
331         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
332                 new float[] { 0.0f,0.5f,1.0f },
333                 new Color[] { color2,
334                             decodeColor(color2,color3,0.5f),
335                             color3});
336     }
337
338     private Paint decodeGradient2(Shape s) {
339         Rectangle2D bounds = s.getBounds2D();
340         float x = (float)bounds.getX();
341         float y = (float)bounds.getY();
342         float w = (float)bounds.getWidth();
343         float h = (float)bounds.getHeight();
344         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
345                 new float[] { 0.0f,0.171875f,0.34375f,0.4815341f,0.6193182f,0.8096591f,1.0f },
346                 new Color[] { color5,
347                             decodeColor(color5,color6,0.5f),
348                             color6,
349                             decodeColor(color6,color7,0.5f),
350                             color7,
351                             decodeColor(color7,color8,0.5f),
352                             color8});
353     }
354
355     private Paint decodeGradient3(Shape s) {
356         Rectangle2D bounds = s.getBounds2D();
357         float x = (float)bounds.getX();
358         float y = (float)bounds.getY();
359         float w = (float)bounds.getWidth();
360         float h = (float)bounds.getHeight();
361         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
362                 new float[] { 0.0f,0.5f,1.0f },
363                 new Color[] { color10,
364                             decodeColor(color10,color11,0.5f),
365                             color11});
366     }
367
368     private Paint decodeGradient4(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.12299465f,0.44652405f,0.5441176f,0.64171124f,0.8208556f,1.0f },
376                 new Color[] { color12,
377                             decodeColor(color12,color13,0.5f),
378                             color13,
379                             decodeColor(color13,color14,0.5f),
380                             color14,
381                             decodeColor(color14,color15,0.5f),
382                             color15});
383     }
384
385     private Paint decodeGradient5(Shape s) {
386         Rectangle2D bounds = s.getBounds2D();
387         float x = (float)bounds.getX();
388         float y = (float)bounds.getY();
389         float w = (float)bounds.getWidth();
390         float h = (float)bounds.getHeight();
391         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
392                 new float[] { 0.0f,0.5f,1.0f },
393                 new Color[] { color16,
394                             decodeColor(color16,color17,0.5f),
395                             color17});
396     }
397
398     private Paint decodeGradient6(Shape s) {
399         Rectangle2D bounds = s.getBounds2D();
400         float x = (float)bounds.getX();
401         float y = (float)bounds.getY();
402         float w = (float)bounds.getWidth();
403         float h = (float)bounds.getHeight();
404         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
405                 new float[] { 0.0f,0.12299465f,0.44652405f,0.5441176f,0.64171124f,0.81283426f,0.98395723f },
406                 new Color[] { color18,
407                             decodeColor(color18,color19,0.5f),
408                             color19,
409                             decodeColor(color19,color20,0.5f),
410                             color20,
411                             decodeColor(color20,color21,0.5f),
412                             color21});
413     }
414
415     private Paint decodeGradient7(Shape s) {
416         Rectangle2D bounds = s.getBounds2D();
417         float x = (float)bounds.getX();
418         float y = (float)bounds.getY();
419         float w = (float)bounds.getWidth();
420         float h = (float)bounds.getHeight();
421         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
422                 new float[] { 0.0f,0.5f,1.0f },
423                 new Color[] { color22,
424                             decodeColor(color22,color23,0.5f),
425                             color23});
426     }
427
428     private Paint decodeGradient8(Shape s) {
429         Rectangle2D bounds = s.getBounds2D();
430         float x = (float)bounds.getX();
431         float y = (float)bounds.getY();
432         float w = (float)bounds.getWidth();
433         float h = (float)bounds.getHeight();
434         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
435                 new float[] { 0.0f,0.12299465f,0.44652405f,0.5441176f,0.64171124f,0.8208556f,1.0f },
436                 new Color[] { color24,
437                             decodeColor(color24,color25,0.5f),
438                             color25,
439                             decodeColor(color25,color26,0.5f),
440                             color26,
441                             decodeColor(color26,color27,0.5f),
442                             color27});
443     }
444
445     private Paint decodeGradient9(Shape s) {
446         Rectangle2D bounds = s.getBounds2D();
447         float x = (float)bounds.getX();
448         float y = (float)bounds.getY();
449         float w = (float)bounds.getWidth();
450         float h = (float)bounds.getHeight();
451         return decodeGradient((1.0f * w) + x, (0.5f * h) + y, (0.0f * w) + x, (0.5f * h) + y,
452                 new float[] { 0.0f,0.5f,1.0f },
453                 new Color[] { color28,
454                             decodeColor(color28,color29,0.5f),
455                             color29});
456     }
457
458
459 }
460
Popular Tags