KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ComboBoxComboBoxTextFieldPainter.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 ComboBoxComboBoxTextFieldPainter 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 ComboBoxComboBoxTextFieldPainter 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
25
26     private int state; //refers to one of the static final ints above
27
private PaintContext ctx;
28
29     //the following 4 variables are reused during the painting code of the layers
30
private Path2D path = new Path2D.Float();
31     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
32     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
33     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
34
35     //All Colors used for painting are stored here. Ideally, only those colors being used
36
//by a particular instance of ComboBoxComboBoxTextFieldPainter would be created. For the moment at least,
37
//however, all are created for each instance.
38
private Color color1 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, -0.74509805f, -237);
39     private Color color2 = decodeColor("nimbusBlueGrey", -0.006944418f, -0.07187897f, 0.06666666f, 0);
40     private Color color3 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07703349f, 0.0745098f, 0);
41     private Color color4 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07968931f, 0.14509803f, 0);
42     private Color color5 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07856284f, 0.11372548f, 0);
43     private Color color6 = decodeColor("nimbusBase", 0.040395975f, -0.60315615f, 0.29411763f, 0);
44     private Color color7 = decodeColor("nimbusBase", 0.016586483f, -0.6051466f, 0.3490196f, 0);
45     private Color color8 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.0965403f, -0.18431371f, 0);
46     private Color color9 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.1048766f, -0.05098039f, 0);
47     private Color color10 = decodeColor("nimbusLightBackground", 0.6666667f, 0.004901961f, -0.19999999f, 0);
48     private Color color11 = decodeColor("nimbusLightBackground", 0.0f, 0.0f, 0.0f, 0);
49     private Color color12 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.105344966f, 0.011764705f, 0);
50
51
52     //Array of current component colors, updated in each paint call
53
private Object JavaDoc[] componentColors;
54
55     public ComboBoxComboBoxTextFieldPainter(PaintContext ctx, int state) {
56         super();
57         this.state = state;
58         this.ctx = ctx;
59     }
60
61     @Override JavaDoc
62     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
63         //populate componentColors array with colors calculated in getExtendedCacheKeys call
64
componentColors = extendedCacheKeys;
65         //generate this entire method. Each state/bg/fg/border combo that has
66
//been painted gets its own KEY and paint method.
67
switch(state) {
68             case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
69             case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
70
71         }
72     }
73         
74
75
76     @Override JavaDoc
77     protected final PaintContext getPaintContext() {
78         return ctx;
79     }
80
81     private void paintBackgroundDisabled(Graphics2D g) {
82         rect = decodeRect1();
83         g.setPaint(color1);
84         g.fill(rect);
85         rect = decodeRect2();
86         g.setPaint(decodeGradient1(rect));
87         g.fill(rect);
88         rect = decodeRect3();
89         g.setPaint(decodeGradient2(rect));
90         g.fill(rect);
91         rect = decodeRect4();
92         g.setPaint(color6);
93         g.fill(rect);
94         rect = decodeRect5();
95         g.setPaint(color7);
96         g.fill(rect);
97
98     }
99
100     private void paintBackgroundEnabled(Graphics2D g) {
101         rect = decodeRect1();
102         g.setPaint(color1);
103         g.fill(rect);
104         rect = decodeRect2();
105         g.setPaint(decodeGradient3(rect));
106         g.fill(rect);
107         rect = decodeRect3();
108         g.setPaint(decodeGradient4(rect));
109         g.fill(rect);
110         rect = decodeRect4();
111         g.setPaint(color12);
112         g.fill(rect);
113         rect = decodeRect5();
114         g.setPaint(color11);
115         g.fill(rect);
116
117     }
118
119
120
121     private Rectangle2D decodeRect1() {
122             rect.setRect(decodeX(0.6666667f), //x
123
decodeY(2.3333333f), //y
124
decodeX(3.0f) - decodeX(0.6666667f), //width
125
decodeY(2.6666667f) - decodeY(2.3333333f)); //height
126
return rect;
127     }
128
129     private Rectangle2D decodeRect2() {
130             rect.setRect(decodeX(0.6666667f), //x
131
decodeY(0.4f), //y
132
decodeX(3.0f) - decodeX(0.6666667f), //width
133
decodeY(1.0f) - decodeY(0.4f)); //height
134
return rect;
135     }
136
137     private Rectangle2D decodeRect3() {
138             rect.setRect(decodeX(1.0f), //x
139
decodeY(0.6f), //y
140
decodeX(3.0f) - decodeX(1.0f), //width
141
decodeY(1.0f) - decodeY(0.6f)); //height
142
return rect;
143     }
144
145     private Rectangle2D decodeRect4() {
146             rect.setRect(decodeX(0.6666667f), //x
147
decodeY(1.0f), //y
148
decodeX(3.0f) - decodeX(0.6666667f), //width
149
decodeY(2.3333333f) - decodeY(1.0f)); //height
150
return rect;
151     }
152
153     private Rectangle2D decodeRect5() {
154             rect.setRect(decodeX(1.0f), //x
155
decodeY(1.0f), //y
156
decodeX(3.0f) - decodeX(1.0f), //width
157
decodeY(2.0f) - decodeY(1.0f)); //height
158
return rect;
159     }
160
161
162
163     private Paint decodeGradient1(Shape s) {
164         Rectangle2D bounds = s.getBounds2D();
165         float x = (float)bounds.getX();
166         float y = (float)bounds.getY();
167         float w = (float)bounds.getWidth();
168         float h = (float)bounds.getHeight();
169         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
170                 new float[] { 0.0f,0.5f,1.0f },
171                 new Color[] { color2,
172                             decodeColor(color2,color3,0.5f),
173                             color3});
174     }
175
176     private Paint decodeGradient2(Shape s) {
177         Rectangle2D bounds = s.getBounds2D();
178         float x = (float)bounds.getX();
179         float y = (float)bounds.getY();
180         float w = (float)bounds.getWidth();
181         float h = (float)bounds.getHeight();
182         return decodeGradient((0.5f * w) + x, (1.0f * h) + y, (0.5f * w) + x, (0.0f * h) + y,
183                 new float[] { 0.0f,0.5f,1.0f },
184                 new Color[] { color4,
185                             decodeColor(color4,color5,0.5f),
186                             color5});
187     }
188
189     private Paint decodeGradient3(Shape s) {
190         Rectangle2D bounds = s.getBounds2D();
191         float x = (float)bounds.getX();
192         float y = (float)bounds.getY();
193         float w = (float)bounds.getWidth();
194         float h = (float)bounds.getHeight();
195         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
196                 new float[] { 0.0f,0.49573863f,0.99147725f },
197                 new Color[] { color8,
198                             decodeColor(color8,color9,0.5f),
199                             color9});
200     }
201
202     private Paint decodeGradient4(Shape s) {
203         Rectangle2D bounds = s.getBounds2D();
204         float x = (float)bounds.getX();
205         float y = (float)bounds.getY();
206         float w = (float)bounds.getWidth();
207         float h = (float)bounds.getHeight();
208         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
209                 new float[] { 0.1f,0.49999997f,0.9f },
210                 new Color[] { color10,
211                             decodeColor(color10,color11,0.5f),
212                             color11});
213     }
214
215
216 }
217
Popular Tags