KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ToolBarButtonPainter.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 ToolBarButtonPainter 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 ToolBarButtonPainter to determine which region/state is being painted
21
//by that instance.
22
static final int BACKGROUND_ENABLED = 1;
23     static final int BACKGROUND_FOCUSED = 2;
24     static final int BACKGROUND_MOUSEOVER = 3;
25     static final int BACKGROUND_MOUSEOVER_FOCUSED = 4;
26     static final int BACKGROUND_PRESSED = 5;
27     static final int BACKGROUND_PRESSED_FOCUSED = 6;
28
29
30     private int state; //refers to one of the static final ints above
31
private PaintContext ctx;
32
33     //the following 4 variables are reused during the painting code of the layers
34
private Path2D path = new Path2D.Float();
35     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
36     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
37     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
38
39     //All Colors used for painting are stored here. Ideally, only those colors being used
40
//by a particular instance of ToolBarButtonPainter would be created. For the moment at least,
41
//however, all are created for each instance.
42
private Color color1 = decodeColor("nimbusFocus", 0.0f, 0.0f, 0.0f, 0);
43     private Color color2 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.06885965f, -0.36862746f, -153);
44     private Color color3 = decodeColor("nimbusBlueGrey", 0.0f, -0.020974077f, -0.21960783f, 0);
45     private Color color4 = decodeColor("nimbusBlueGrey", 0.0f, 0.11169591f, -0.53333336f, 0);
46     private Color color5 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.10658931f, 0.25098038f, 0);
47     private Color color6 = decodeColor("nimbusBlueGrey", 0.0f, -0.098526314f, 0.2352941f, 0);
48     private Color color7 = decodeColor("nimbusBlueGrey", 0.0f, -0.07333623f, 0.20392156f, 0);
49     private Color color8 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, 0);
50     private Color color9 = decodeColor("nimbusBlueGrey", -0.00505054f, -0.05960039f, 0.10196078f, 0);
51     private Color color10 = decodeColor("nimbusBlueGrey", -0.008547008f, -0.04772438f, 0.06666666f, 0);
52     private Color color11 = decodeColor("nimbusBlueGrey", -0.0027777553f, -0.0018306673f, -0.02352941f, 0);
53     private Color color12 = decodeColor("nimbusBlueGrey", -0.0027777553f, -0.0212406f, 0.13333333f, 0);
54     private Color color13 = decodeColor("nimbusBlueGrey", 0.0055555105f, -0.030845039f, 0.23921567f, 0);
55
56
57     //Array of current component colors, updated in each paint call
58
private Object JavaDoc[] componentColors;
59
60     public ToolBarButtonPainter(PaintContext ctx, int state) {
61         super();
62         this.state = state;
63         this.ctx = ctx;
64     }
65
66     @Override JavaDoc
67     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
68         //populate componentColors array with colors calculated in getExtendedCacheKeys call
69
componentColors = extendedCacheKeys;
70         //generate this entire method. Each state/bg/fg/border combo that has
71
//been painted gets its own KEY and paint method.
72
switch(state) {
73             case BACKGROUND_FOCUSED: paintBackgroundFocused(g); break;
74             case BACKGROUND_MOUSEOVER: paintBackgroundMouseOver(g); break;
75             case BACKGROUND_MOUSEOVER_FOCUSED: paintBackgroundMouseOverAndFocused(g); break;
76             case BACKGROUND_PRESSED: paintBackgroundPressed(g); break;
77             case BACKGROUND_PRESSED_FOCUSED: paintBackgroundPressedAndFocused(g); break;
78
79         }
80     }
81         
82
83
84     @Override JavaDoc
85     protected final PaintContext getPaintContext() {
86         return ctx;
87     }
88
89     private void paintBackgroundFocused(Graphics2D g) {
90         path = decodePath1();
91         g.setPaint(color1);
92         g.fill(path);
93
94     }
95
96     private void paintBackgroundMouseOver(Graphics2D g) {
97         roundRect = decodeRoundRect1();
98         g.setPaint(color2);
99         g.fill(roundRect);
100         roundRect = decodeRoundRect2();
101         g.setPaint(decodeGradient1(roundRect));
102         g.fill(roundRect);
103         roundRect = decodeRoundRect3();
104         g.setPaint(decodeGradient2(roundRect));
105         g.fill(roundRect);
106
107     }
108
109     private void paintBackgroundMouseOverAndFocused(Graphics2D g) {
110         roundRect = decodeRoundRect4();
111         g.setPaint(color1);
112         g.fill(roundRect);
113         roundRect = decodeRoundRect2();
114         g.setPaint(decodeGradient1(roundRect));
115         g.fill(roundRect);
116         roundRect = decodeRoundRect3();
117         g.setPaint(decodeGradient2(roundRect));
118         g.fill(roundRect);
119
120     }
121
122     private void paintBackgroundPressed(Graphics2D g) {
123         roundRect = decodeRoundRect1();
124         g.setPaint(color2);
125         g.fill(roundRect);
126         roundRect = decodeRoundRect2();
127         g.setPaint(decodeGradient1(roundRect));
128         g.fill(roundRect);
129         roundRect = decodeRoundRect3();
130         g.setPaint(decodeGradient3(roundRect));
131         g.fill(roundRect);
132
133     }
134
135     private void paintBackgroundPressedAndFocused(Graphics2D g) {
136         roundRect = decodeRoundRect4();
137         g.setPaint(color1);
138         g.fill(roundRect);
139         roundRect = decodeRoundRect2();
140         g.setPaint(decodeGradient1(roundRect));
141         g.fill(roundRect);
142         roundRect = decodeRoundRect3();
143         g.setPaint(decodeGradient3(roundRect));
144         g.fill(roundRect);
145
146     }
147
148
149
150     private Path2D decodePath1() {
151         path.reset();
152         path.moveTo(decodeX(1.4133738f), decodeY(0.120000005f));
153         path.lineTo(decodeX(1.9893618f), decodeY(0.120000005f));
154         path.curveTo(decodeAnchorX(1.9893617630004883f, 3.0f), decodeAnchorY(0.12000000476837158f, 0.0f), decodeAnchorX(2.8857147693634033f, 0.0f), decodeAnchorY(1.04347825050354f, -3.0f), decodeX(2.8857148f), decodeY(1.0434783f));
155         path.lineTo(decodeX(2.9f), decodeY(1.9565217f));
156         path.curveTo(decodeAnchorX(2.9000000953674316f, 0.0f), decodeAnchorY(1.95652174949646f, 3.0f), decodeAnchorX(1.9893617630004883f, 3.0f), decodeAnchorY(2.8714287281036377f, 0.0f), decodeX(1.9893618f), decodeY(2.8714287f));
157         path.lineTo(decodeX(1.0106384f), decodeY(2.8714287f));
158         path.curveTo(decodeAnchorX(1.0106383562088013f, -3.0f), decodeAnchorY(2.8714287281036377f, 0.0f), decodeAnchorX(0.12000000476837158f, 0.0f), decodeAnchorY(1.95652174949646f, 3.0f), decodeX(0.120000005f), decodeY(1.9565217f));
159         path.lineTo(decodeX(0.120000005f), decodeY(1.0465839f));
160         path.curveTo(decodeAnchorX(0.12000000476837158f, 0.0f), decodeAnchorY(1.046583890914917f, -3.000000000000001f), decodeAnchorX(1.0106383562088013f, -3.0f), decodeAnchorY(0.12000000476837158f, 0.0f), decodeX(1.0106384f), decodeY(0.120000005f));
161         path.lineTo(decodeX(1.4148936f), decodeY(0.120000005f));
162         path.lineTo(decodeX(1.4148936f), decodeY(0.4857143f));
163         path.lineTo(decodeX(1.0106384f), decodeY(0.4857143f));
164         path.curveTo(decodeAnchorX(1.0106383562088013f, -1.928571428571427f), decodeAnchorY(0.48571428656578064f, 0.0f), decodeAnchorX(0.4714285731315613f, -0.04427948362011014f), decodeAnchorY(1.040372610092163f, -2.429218094741624f), decodeX(0.47142857f), decodeY(1.0403726f));
165         path.lineTo(decodeX(0.47142857f), decodeY(1.9565217f));
166         path.curveTo(decodeAnchorX(0.4714285731315613f, 0.0f), decodeAnchorY(1.95652174949646f, 2.2142857142856975f), decodeAnchorX(1.0106383562088013f, -1.7857142857142847f), decodeAnchorY(2.5142855644226074f, 0.0f), decodeX(1.0106384f), decodeY(2.5142856f));
167         path.lineTo(decodeX(1.9893618f), decodeY(2.5142856f));
168         path.curveTo(decodeAnchorX(1.9893617630004883f, 2.071428571428598f), decodeAnchorY(2.5142855644226074f, 0.0f), decodeAnchorX(2.5f, 0.0f), decodeAnchorY(1.95652174949646f, 2.2142857142857046f), decodeX(2.5f), decodeY(1.9565217f));
169         path.lineTo(decodeX(2.5142853f), decodeY(1.0434783f));
170         path.curveTo(decodeAnchorX(2.5142853260040283f, 0.0f), decodeAnchorY(1.04347825050354f, -2.1428571428571406f), decodeAnchorX(1.990121603012085f, 2.142857142857167f), decodeAnchorY(0.4714285731315613f, 0.0f), decodeX(1.9901216f), decodeY(0.47142857f));
171         path.lineTo(decodeX(1.4148936f), decodeY(0.4857143f));
172         path.lineTo(decodeX(1.4133738f), decodeY(0.120000005f));
173         path.closePath();
174         return path;
175     }
176
177     private RoundRectangle2D decodeRoundRect1() {
178         roundRect.setRoundRect(decodeX(0.4f), //x
179
decodeY(0.6f), //y
180
decodeX(2.6f) - decodeX(0.4f), //width
181
decodeY(2.8f) - decodeY(0.6f), //height
182
12.0f, 12.0f); //rounding
183
return roundRect;
184     }
185
186     private RoundRectangle2D decodeRoundRect2() {
187         roundRect.setRoundRect(decodeX(0.4f), //x
188
decodeY(0.4f), //y
189
decodeX(2.6f) - decodeX(0.4f), //width
190
decodeY(2.6f) - decodeY(0.4f), //height
191
12.0f, 12.0f); //rounding
192
return roundRect;
193     }
194
195     private RoundRectangle2D decodeRoundRect3() {
196         roundRect.setRoundRect(decodeX(0.6f), //x
197
decodeY(0.6f), //y
198
decodeX(2.4f) - decodeX(0.6f), //width
199
decodeY(2.4f) - decodeY(0.6f), //height
200
9.0f, 9.0f); //rounding
201
return roundRect;
202     }
203
204     private RoundRectangle2D decodeRoundRect4() {
205         roundRect.setRoundRect(decodeX(0.120000005f), //x
206
decodeY(0.120000005f), //y
207
decodeX(2.8800004f) - decodeX(0.120000005f), //width
208
decodeY(2.8800004f) - decodeY(0.120000005f), //height
209
13.0f, 13.0f); //rounding
210
return roundRect;
211     }
212
213
214
215     private Paint decodeGradient1(Shape s) {
216         Rectangle2D bounds = s.getBounds2D();
217         float x = (float)bounds.getX();
218         float y = (float)bounds.getY();
219         float w = (float)bounds.getWidth();
220         float h = (float)bounds.getHeight();
221         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
222                 new float[] { 0.09f,0.52f,0.95f },
223                 new Color[] { color3,
224                             decodeColor(color3,color4,0.5f),
225                             color4});
226     }
227
228     private Paint decodeGradient2(Shape s) {
229         Rectangle2D bounds = s.getBounds2D();
230         float x = (float)bounds.getX();
231         float y = (float)bounds.getY();
232         float w = (float)bounds.getWidth();
233         float h = (float)bounds.getHeight();
234         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
235                 new float[] { 0.0f,0.03f,0.06f,0.33f,0.6f,0.65f,0.7f,0.825f,0.95f,0.975f,1.0f },
236                 new Color[] { color5,
237                             decodeColor(color5,color6,0.5f),
238                             color6,
239                             decodeColor(color6,color7,0.5f),
240                             color7,
241                             decodeColor(color7,color7,0.5f),
242                             color7,
243                             decodeColor(color7,color8,0.5f),
244                             color8,
245                             decodeColor(color8,color8,0.5f),
246                             color8});
247     }
248
249     private Paint decodeGradient3(Shape s) {
250         Rectangle2D bounds = s.getBounds2D();
251         float x = (float)bounds.getX();
252         float y = (float)bounds.getY();
253         float w = (float)bounds.getWidth();
254         float h = (float)bounds.getHeight();
255         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
256                 new float[] { 0.0f,0.03f,0.06f,0.33f,0.6f,0.65f,0.7f,0.825f,0.95f,0.975f,1.0f },
257                 new Color[] { color9,
258                             decodeColor(color9,color10,0.5f),
259                             color10,
260                             decodeColor(color10,color11,0.5f),
261                             color11,
262                             decodeColor(color11,color11,0.5f),
263                             color11,
264                             decodeColor(color11,color12,0.5f),
265                             color12,
266                             decodeColor(color12,color13,0.5f),
267                             color13});
268     }
269
270
271 }
272
Popular Tags