KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ScrollBarScrollBarButtonPainter.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 ScrollBarScrollBarButtonPainter 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 ScrollBarScrollBarButtonPainter to determine which region/state is being painted
21
//by that instance.
22
static final int FOREGROUND_ENABLED = 1;
23     static final int FOREGROUND_DISABLED = 2;
24     static final int FOREGROUND_MOUSEOVER = 3;
25     static final int FOREGROUND_PRESSED = 4;
26
27
28     private int state; //refers to one of the static final ints above
29
private PaintContext ctx;
30
31     //the following 4 variables are reused during the painting code of the layers
32
private Path2D path = new Path2D.Float();
33     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
34     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
35     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
36
37     //All Colors used for painting are stored here. Ideally, only those colors being used
38
//by a particular instance of ScrollBarScrollBarButtonPainter would be created. For the moment at least,
39
//however, all are created for each instance.
40
private Color color1 = new Color(255, 200, 0, 255);
41     private Color color2 = decodeColor("nimbusBlueGrey", -0.01111114f, -0.07763158f, -0.1490196f, 0);
42     private Color color3 = decodeColor("nimbusBlueGrey", -0.111111104f, -0.10580933f, 0.086274505f, 0);
43     private Color color4 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.102261856f, 0.20392156f, 0);
44     private Color color5 = decodeColor("nimbusBlueGrey", -0.039682567f, -0.079276316f, 0.13333333f, 0);
45     private Color color6 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.07382907f, 0.109803915f, 0);
46     private Color color7 = decodeColor("nimbusBlueGrey", -0.039682567f, -0.08241387f, 0.23137254f, 0);
47     private Color color8 = decodeColor("nimbusBlueGrey", -0.055555522f, -0.08443936f, -0.29411766f, -136);
48     private Color color9 = decodeColor("nimbusBlueGrey", -0.055555522f, -0.09876161f, 0.25490195f, -178);
49     private Color color10 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.08878718f, -0.5647059f, 0);
50     private Color color11 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.080223285f, -0.4862745f, 0);
51     private Color color12 = decodeColor("nimbusBlueGrey", -0.111111104f, -0.09525914f, -0.23137254f, 0);
52     private Color color13 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, -165);
53     private Color color14 = decodeColor("nimbusBlueGrey", -0.04444444f, -0.080223285f, -0.09803921f, 0);
54     private Color color15 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, 0.10588235f, 0);
55     private Color color16 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, 0);
56     private Color color17 = decodeColor("nimbusBlueGrey", -0.039682567f, -0.081719734f, 0.20784312f, 0);
57     private Color color18 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.07677104f, 0.18431371f, 0);
58     private Color color19 = decodeColor("nimbusBlueGrey", -0.04444444f, -0.080223285f, -0.09803921f, -69);
59     private Color color20 = decodeColor("nimbusBlueGrey", -0.055555522f, -0.09876161f, 0.25490195f, -39);
60     private Color color21 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.0951417f, -0.49019608f, 0);
61     private Color color22 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.086996906f, -0.4117647f, 0);
62     private Color color23 = decodeColor("nimbusBlueGrey", -0.111111104f, -0.09719298f, -0.15686274f, 0);
63     private Color color24 = decodeColor("nimbusBlueGrey", -0.037037015f, -0.043859646f, -0.21568626f, 0);
64     private Color color25 = decodeColor("nimbusBlueGrey", -0.06349206f, -0.07309316f, -0.011764705f, 0);
65     private Color color26 = decodeColor("nimbusBlueGrey", -0.048611104f, -0.07296763f, 0.09019607f, 0);
66     private Color color27 = decodeColor("nimbusBlueGrey", -0.03535354f, -0.05497076f, 0.031372547f, 0);
67     private Color color28 = decodeColor("nimbusBlueGrey", -0.034188032f, -0.043168806f, 0.011764705f, 0);
68     private Color color29 = decodeColor("nimbusBlueGrey", -0.03535354f, -0.0600676f, 0.109803915f, 0);
69     private Color color30 = decodeColor("nimbusBlueGrey", -0.037037015f, -0.043859646f, -0.21568626f, -44);
70     private Color color31 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, -0.74509805f, 0);
71
72
73     //Array of current component colors, updated in each paint call
74
private Object JavaDoc[] componentColors;
75
76     public ScrollBarScrollBarButtonPainter(PaintContext ctx, int state) {
77         super();
78         this.state = state;
79         this.ctx = ctx;
80     }
81
82     @Override JavaDoc
83     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
84         //populate componentColors array with colors calculated in getExtendedCacheKeys call
85
componentColors = extendedCacheKeys;
86         //generate this entire method. Each state/bg/fg/border combo that has
87
//been painted gets its own KEY and paint method.
88
switch(state) {
89             case FOREGROUND_ENABLED: paintForegroundEnabled(g); break;
90             case FOREGROUND_DISABLED: paintForegroundDisabled(g); break;
91             case FOREGROUND_MOUSEOVER: paintForegroundMouseOver(g); break;
92             case FOREGROUND_PRESSED: paintForegroundPressed(g); break;
93
94         }
95     }
96         
97
98
99     @Override JavaDoc
100     protected final PaintContext getPaintContext() {
101         return ctx;
102     }
103
104     private void paintForegroundEnabled(Graphics2D g) {
105         path = decodePath1();
106         g.setPaint(color1);
107         g.fill(path);
108         path = decodePath2();
109         g.setPaint(decodeGradient1(path));
110         g.fill(path);
111         path = decodePath3();
112         g.setPaint(decodeGradient2(path));
113         g.fill(path);
114         path = decodePath4();
115         g.setPaint(decodeGradient3(path));
116         g.fill(path);
117         path = decodePath5();
118         g.setPaint(color13);
119         g.fill(path);
120
121     }
122
123     private void paintForegroundDisabled(Graphics2D g) {
124         path = decodePath1();
125         g.setPaint(color1);
126         g.fill(path);
127
128     }
129
130     private void paintForegroundMouseOver(Graphics2D g) {
131         path = decodePath1();
132         g.setPaint(color1);
133         g.fill(path);
134         path = decodePath2();
135         g.setPaint(decodeGradient4(path));
136         g.fill(path);
137         path = decodePath3();
138         g.setPaint(decodeGradient5(path));
139         g.fill(path);
140         path = decodePath4();
141         g.setPaint(decodeGradient6(path));
142         g.fill(path);
143         path = decodePath5();
144         g.setPaint(color13);
145         g.fill(path);
146
147     }
148
149     private void paintForegroundPressed(Graphics2D g) {
150         path = decodePath1();
151         g.setPaint(color1);
152         g.fill(path);
153         path = decodePath2();
154         g.setPaint(decodeGradient7(path));
155         g.fill(path);
156         path = decodePath3();
157         g.setPaint(decodeGradient8(path));
158         g.fill(path);
159         path = decodePath4();
160         g.setPaint(color31);
161         g.fill(path);
162         path = decodePath5();
163         g.setPaint(color13);
164         g.fill(path);
165
166     }
167
168
169
170     private Path2D decodePath1() {
171         path.reset();
172         path.moveTo(decodeX(3.0f), decodeY(3.0f));
173         path.lineTo(decodeX(3.0f), decodeY(3.0f));
174         path.closePath();
175         return path;
176     }
177
178     private Path2D decodePath2() {
179         path.reset();
180         path.moveTo(decodeX(0.0f), decodeY(0.0f));
181         path.lineTo(decodeX(1.6956522f), decodeY(0.0f));
182         path.curveTo(decodeAnchorX(1.6956522464752197f, 0.0f), decodeAnchorY(0.0f, 0.0f), decodeAnchorX(1.6956522464752197f, -0.7058823529411633f), decodeAnchorY(1.307692289352417f, -3.0294117647058822f), decodeX(1.6956522f), decodeY(1.3076923f));
183         path.curveTo(decodeAnchorX(1.6956522464752197f, 0.7058823529411633f), decodeAnchorY(1.307692289352417f, 3.0294117647058822f), decodeAnchorX(1.8260869979858398f, -2.0f), decodeAnchorY(1.769230842590332f, -1.9411764705882355f), decodeX(1.826087f), decodeY(1.7692308f));
184         path.curveTo(decodeAnchorX(1.8260869979858398f, 2.0f), decodeAnchorY(1.769230842590332f, 1.9411764705882355f), decodeAnchorX(3.0f, 0.0f), decodeAnchorY(2.0f, 0.0f), decodeX(3.0f), decodeY(2.0f));
185         path.lineTo(decodeX(3.0f), decodeY(3.0f));
186         path.lineTo(decodeX(0.0f), decodeY(3.0f));
187         path.lineTo(decodeX(0.0f), decodeY(0.0f));
188         path.closePath();
189         return path;
190     }
191
192     private Path2D decodePath3() {
193         path.reset();
194         path.moveTo(decodeX(0.0f), decodeY(1.0022625f));
195         path.lineTo(decodeX(0.9705882f), decodeY(1.0384616f));
196         path.lineTo(decodeX(1.0409207f), decodeY(1.0791855f));
197         path.lineTo(decodeX(1.0409207f), decodeY(3.0f));
198         path.lineTo(decodeX(0.0f), decodeY(3.0f));
199         path.lineTo(decodeX(0.0f), decodeY(1.0022625f));
200         path.closePath();
201         return path;
202     }
203
204     private Path2D decodePath4() {
205         path.reset();
206         path.moveTo(decodeX(1.4782609f), decodeY(1.2307693f));
207         path.lineTo(decodeX(1.4782609f), decodeY(1.7692308f));
208         path.lineTo(decodeX(1.1713555f), decodeY(1.5f));
209         path.lineTo(decodeX(1.4782609f), decodeY(1.2307693f));
210         path.closePath();
211         return path;
212     }
213
214     private Path2D decodePath5() {
215         path.reset();
216         path.moveTo(decodeX(1.6713555f), decodeY(1.0769231f));
217         path.curveTo(decodeAnchorX(1.6713554859161377f, 0.7352941176470615f), decodeAnchorY(1.076923131942749f, 0.0f), decodeAnchorX(1.718670129776001f, -0.911764705882355f), decodeAnchorY(1.4095022678375244f, -2.2058823529411784f), decodeX(1.7186701f), decodeY(1.4095023f));
218         path.curveTo(decodeAnchorX(1.718670129776001f, 0.911764705882355f), decodeAnchorY(1.4095022678375244f, 2.2058823529411784f), decodeAnchorX(1.8439897298812866f, -2.3529411764705905f), decodeAnchorY(1.7941176891326904f, -1.852941176470587f), decodeX(1.8439897f), decodeY(1.7941177f));
219         path.curveTo(decodeAnchorX(1.8439897298812866f, 2.3529411764705905f), decodeAnchorY(1.7941176891326904f, 1.852941176470587f), decodeAnchorX(2.5f, 0.0f), decodeAnchorY(2.2352943420410156f, 0.0f), decodeX(2.5f), decodeY(2.2352943f));
220         path.lineTo(decodeX(2.3529415f), decodeY(2.8235292f));
221         path.curveTo(decodeAnchorX(2.3529415130615234f, 0.0f), decodeAnchorY(2.8235292434692383f, 0.0f), decodeAnchorX(1.818414330482483f, 1.5588235294117645f), decodeAnchorY(1.8438913822174072f, 1.3823529411764675f), decodeX(1.8184143f), decodeY(1.8438914f));
222         path.curveTo(decodeAnchorX(1.818414330482483f, -1.5588235294117645f), decodeAnchorY(1.8438913822174072f, -1.3823529411764675f), decodeAnchorX(1.694373369216919f, 0.7941176470588225f), decodeAnchorY(1.4841628074645996f, 1.9999999999999991f), decodeX(1.6943734f), decodeY(1.4841628f));
223         path.curveTo(decodeAnchorX(1.694373369216919f, -0.7941176470588225f), decodeAnchorY(1.4841628074645996f, -1.9999999999999991f), decodeAnchorX(1.6713554859161377f, -0.7352941176470598f), decodeAnchorY(1.076923131942749f, 0.0f), decodeX(1.6713555f), decodeY(1.0769231f));
224         path.closePath();
225         return path;
226     }
227
228
229
230     private Paint decodeGradient1(Shape s) {
231         Rectangle2D bounds = s.getBounds2D();
232         float x = (float)bounds.getX();
233         float y = (float)bounds.getY();
234         float w = (float)bounds.getWidth();
235         float h = (float)bounds.getHeight();
236         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
237                 new float[] { 0.0f,0.032934133f,0.065868266f,0.089820355f,0.11377245f,0.23053892f,0.3473054f,0.494012f,0.6407186f,0.78443116f,0.92814374f },
238                 new Color[] { color2,
239                             decodeColor(color2,color3,0.5f),
240                             color3,
241                             decodeColor(color3,color4,0.5f),
242                             color4,
243                             decodeColor(color4,color5,0.5f),
244                             color5,
245                             decodeColor(color5,color6,0.5f),
246                             color6,
247                             decodeColor(color6,color7,0.5f),
248                             color7});
249     }
250
251     private Paint decodeGradient2(Shape s) {
252         Rectangle2D bounds = s.getBounds2D();
253         float x = (float)bounds.getX();
254         float y = (float)bounds.getY();
255         float w = (float)bounds.getWidth();
256         float h = (float)bounds.getHeight();
257         return decodeGradient((0.0f * w) + x, (0.5f * h) + y, (0.5735294f * w) + x, (0.5f * h) + y,
258                 new float[] { 0.0f,0.5f,1.0f },
259                 new Color[] { color8,
260                             decodeColor(color8,color9,0.5f),
261                             color9});
262     }
263
264     private Paint decodeGradient3(Shape s) {
265         Rectangle2D bounds = s.getBounds2D();
266         float x = (float)bounds.getX();
267         float y = (float)bounds.getY();
268         float w = (float)bounds.getWidth();
269         float h = (float)bounds.getHeight();
270         return decodeGradient((0.925f * w) + x, (0.9285714f * h) + y, (0.925f * w) + x, (0.004201681f * h) + y,
271                 new float[] { 0.0f,0.2964072f,0.5928144f,0.79341316f,0.994012f },
272                 new Color[] { color10,
273                             decodeColor(color10,color11,0.5f),
274                             color11,
275                             decodeColor(color11,color12,0.5f),
276                             color12});
277     }
278
279     private Paint decodeGradient4(Shape s) {
280         Rectangle2D bounds = s.getBounds2D();
281         float x = (float)bounds.getX();
282         float y = (float)bounds.getY();
283         float w = (float)bounds.getWidth();
284         float h = (float)bounds.getHeight();
285         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
286                 new float[] { 0.0f,0.032934133f,0.065868266f,0.089820355f,0.11377245f,0.23053892f,0.3473054f,0.494012f,0.6407186f,0.78443116f,0.92814374f },
287                 new Color[] { color14,
288                             decodeColor(color14,color15,0.5f),
289                             color15,
290                             decodeColor(color15,color16,0.5f),
291                             color16,
292                             decodeColor(color16,color17,0.5f),
293                             color17,
294                             decodeColor(color17,color18,0.5f),
295                             color18,
296                             decodeColor(color18,color16,0.5f),
297                             color16});
298     }
299
300     private Paint decodeGradient5(Shape s) {
301         Rectangle2D bounds = s.getBounds2D();
302         float x = (float)bounds.getX();
303         float y = (float)bounds.getY();
304         float w = (float)bounds.getWidth();
305         float h = (float)bounds.getHeight();
306         return decodeGradient((0.0f * w) + x, (0.5f * h) + y, (0.5735294f * w) + x, (0.5f * h) + y,
307                 new float[] { 0.19518717f,0.5975936f,1.0f },
308                 new Color[] { color19,
309                             decodeColor(color19,color20,0.5f),
310                             color20});
311     }
312
313     private Paint decodeGradient6(Shape s) {
314         Rectangle2D bounds = s.getBounds2D();
315         float x = (float)bounds.getX();
316         float y = (float)bounds.getY();
317         float w = (float)bounds.getWidth();
318         float h = (float)bounds.getHeight();
319         return decodeGradient((0.925f * w) + x, (0.9285714f * h) + y, (0.925f * w) + x, (0.004201681f * h) + y,
320                 new float[] { 0.0f,0.2964072f,0.5928144f,0.79341316f,0.994012f },
321                 new Color[] { color21,
322                             decodeColor(color21,color22,0.5f),
323                             color22,
324                             decodeColor(color22,color23,0.5f),
325                             color23});
326     }
327
328     private Paint decodeGradient7(Shape s) {
329         Rectangle2D bounds = s.getBounds2D();
330         float x = (float)bounds.getX();
331         float y = (float)bounds.getY();
332         float w = (float)bounds.getWidth();
333         float h = (float)bounds.getHeight();
334         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
335                 new float[] { 0.0f,0.032934133f,0.065868266f,0.089820355f,0.11377245f,0.23053892f,0.3473054f,0.494012f,0.6407186f,0.78443116f,0.92814374f },
336                 new Color[] { color24,
337                             decodeColor(color24,color25,0.5f),
338                             color25,
339                             decodeColor(color25,color26,0.5f),
340                             color26,
341                             decodeColor(color26,color27,0.5f),
342                             color27,
343                             decodeColor(color27,color28,0.5f),
344                             color28,
345                             decodeColor(color28,color29,0.5f),
346                             color29});
347     }
348
349     private Paint decodeGradient8(Shape s) {
350         Rectangle2D bounds = s.getBounds2D();
351         float x = (float)bounds.getX();
352         float y = (float)bounds.getY();
353         float w = (float)bounds.getWidth();
354         float h = (float)bounds.getHeight();
355         return decodeGradient((0.0f * w) + x, (0.5f * h) + y, (0.5735294f * w) + x, (0.5f * h) + y,
356                 new float[] { 0.0f,0.5f,1.0f },
357                 new Color[] { color30,
358                             decodeColor(color30,color9,0.5f),
359                             color9});
360     }
361
362
363 }
364
Popular Tags