KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ScrollBarScrollBarTrackPainter.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 ScrollBarScrollBarTrackPainter 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 ScrollBarScrollBarTrackPainter 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 ScrollBarScrollBarTrackPainter would be created. For the moment at least,
37
//however, all are created for each instance.
38
private Color color1 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.10016362f, 0.011764705f, 0);
39     private Color color2 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.100476064f, 0.035294116f, 0);
40     private Color color3 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.10606203f, 0.13333333f, 0);
41     private Color color4 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, 0.24705881f, 0);
42     private Color color5 = decodeColor("nimbusBlueGrey", 0.02222228f, -0.06465475f, -0.31764707f, 0);
43     private Color color6 = decodeColor("nimbusBlueGrey", 0.0f, -0.06766917f, -0.19607842f, 0);
44     private Color color7 = decodeColor("nimbusBlueGrey", -0.006944418f, -0.0655825f, -0.04705882f, 0);
45     private Color color8 = decodeColor("nimbusBlueGrey", 0.0138888955f, -0.071117446f, 0.05098039f, 0);
46     private Color color9 = decodeColor("nimbusBlueGrey", 0.0f, -0.07016757f, 0.12941176f, 0);
47     private Color color10 = decodeColor("nimbusBlueGrey", 0.0f, -0.05967886f, -0.5137255f, 0);
48     private Color color11 = decodeColor("nimbusBlueGrey", 0.0f, -0.05967886f, -0.5137255f, -255);
49     private Color color12 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.07826825f, -0.5019608f, -255);
50     private Color color13 = decodeColor("nimbusBlueGrey", -0.015872955f, -0.06731644f, -0.109803915f, 0);
51     private Color color14 = decodeColor("nimbusBlueGrey", 0.0f, -0.06924191f, 0.109803915f, 0);
52     private Color color15 = decodeColor("nimbusBlueGrey", -0.015872955f, -0.06861015f, -0.09019607f, 0);
53     private Color color16 = decodeColor("nimbusBlueGrey", 0.0f, -0.06766917f, 0.07843137f, 0);
54
55
56     //Array of current component colors, updated in each paint call
57
private Object JavaDoc[] componentColors;
58
59     public ScrollBarScrollBarTrackPainter(PaintContext ctx, int state) {
60         super();
61         this.state = state;
62         this.ctx = ctx;
63     }
64
65     @Override JavaDoc
66     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
67         //populate componentColors array with colors calculated in getExtendedCacheKeys call
68
componentColors = extendedCacheKeys;
69         //generate this entire method. Each state/bg/fg/border combo that has
70
//been painted gets its own KEY and paint method.
71
switch(state) {
72             case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
73             case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
74
75         }
76     }
77         
78
79
80     @Override JavaDoc
81     protected final PaintContext getPaintContext() {
82         return ctx;
83     }
84
85     private void paintBackgroundDisabled(Graphics2D g) {
86         rect = decodeRect1();
87         g.setPaint(decodeGradient1(rect));
88         g.fill(rect);
89
90     }
91
92     private void paintBackgroundEnabled(Graphics2D g) {
93         rect = decodeRect1();
94         g.setPaint(decodeGradient2(rect));
95         g.fill(rect);
96         path = decodePath1();
97         g.setPaint(decodeGradient3(path));
98         g.fill(path);
99         path = decodePath2();
100         g.setPaint(decodeGradient4(path));
101         g.fill(path);
102         path = decodePath3();
103         g.setPaint(decodeGradient5(path));
104         g.fill(path);
105         path = decodePath4();
106         g.setPaint(decodeGradient6(path));
107         g.fill(path);
108
109     }
110
111
112
113     private Rectangle2D decodeRect1() {
114             rect.setRect(decodeX(0.0f), //x
115
decodeY(0.0f), //y
116
decodeX(3.0f) - decodeX(0.0f), //width
117
decodeY(3.0f) - decodeY(0.0f)); //height
118
return rect;
119     }
120
121     private Path2D decodePath1() {
122         path.reset();
123         path.moveTo(decodeX(0.7f), decodeY(0.0f));
124         path.lineTo(decodeX(0.0f), decodeY(0.0f));
125         path.lineTo(decodeX(0.0f), decodeY(1.2f));
126         path.curveTo(decodeAnchorX(0.0f, 0.0f), decodeAnchorY(1.2000000476837158f, 0.0f), decodeAnchorX(0.30000001192092896f, -1.0f), decodeAnchorY(2.200000047683716f, -1.0f), decodeX(0.3f), decodeY(2.2f));
127         path.curveTo(decodeAnchorX(0.30000001192092896f, 1.0f), decodeAnchorY(2.200000047683716f, 1.0f), decodeAnchorX(0.6785714030265808f, 0.0f), decodeAnchorY(2.799999952316284f, 0.0f), decodeX(0.6785714f), decodeY(2.8f));
128         path.lineTo(decodeX(0.7f), decodeY(0.0f));
129         path.closePath();
130         return path;
131     }
132
133     private Path2D decodePath2() {
134         path.reset();
135         path.moveTo(decodeX(3.0f), decodeY(0.0f));
136         path.lineTo(decodeX(2.2222223f), decodeY(0.0f));
137         path.lineTo(decodeX(2.2222223f), decodeY(2.8f));
138         path.curveTo(decodeAnchorX(2.222222328186035f, 0.0f), decodeAnchorY(2.799999952316284f, 0.0f), decodeAnchorX(2.674603223800659f, -1.0f), decodeAnchorY(2.1857142448425293f, 1.0f), decodeX(2.6746032f), decodeY(2.1857142f));
139         path.curveTo(decodeAnchorX(2.674603223800659f, 1.0000000000000036f), decodeAnchorY(2.1857142448425293f, -1.0f), decodeAnchorX(3.0f, 0.0f), decodeAnchorY(1.2000000476837158f, 0.0f), decodeX(3.0f), decodeY(1.2f));
140         path.lineTo(decodeX(3.0f), decodeY(0.0f));
141         path.closePath();
142         return path;
143     }
144
145     private Path2D decodePath3() {
146         path.reset();
147         path.moveTo(decodeX(0.11428572f), decodeY(1.3714286f));
148         path.curveTo(decodeAnchorX(0.11428572237491608f, 0.7857142857142856f), decodeAnchorY(1.3714286088943481f, -0.571428571428573f), decodeAnchorX(0.4642857015132904f, -1.3571428571428572f), decodeAnchorY(2.0714285373687744f, -1.5714285714285694f), decodeX(0.4642857f), decodeY(2.0714285f));
149         path.curveTo(decodeAnchorX(0.4642857015132904f, 1.3571428571428577f), decodeAnchorY(2.0714285373687744f, 1.5714285714285694f), decodeAnchorX(0.8714286088943481f, 0.21428571428571352f), decodeAnchorY(2.7285714149475098f, -1.0f), decodeX(0.8714286f), decodeY(2.7285714f));
150         path.curveTo(decodeAnchorX(0.8714286088943481f, -0.21428571428571352f), decodeAnchorY(2.7285714149475098f, 1.0f), decodeAnchorX(0.3571428656578064f, 1.5000000000000004f), decodeAnchorY(2.3142857551574707f, 1.642857142857146f), decodeX(0.35714287f), decodeY(2.3142858f));
151         path.curveTo(decodeAnchorX(0.3571428656578064f, -1.5000000000000004f), decodeAnchorY(2.3142857551574707f, -1.642857142857146f), decodeAnchorX(0.11428572237491608f, -0.7857142857142856f), decodeAnchorY(1.3714286088943481f, 0.571428571428573f), decodeX(0.11428572f), decodeY(1.3714286f));
152         path.closePath();
153         return path;
154     }
155
156     private Path2D decodePath4() {
157         path.reset();
158         path.moveTo(decodeX(2.1111112f), decodeY(2.7f));
159         path.curveTo(decodeAnchorX(2.1111111640930176f, 0.4285714285714306f), decodeAnchorY(2.700000047683716f, 0.6428571428571388f), decodeAnchorX(2.626984119415283f, -1.571428571428573f), decodeAnchorY(2.200000047683716f, 1.6428571428571388f), decodeX(2.6269841f), decodeY(2.2f));
160         path.curveTo(decodeAnchorX(2.626984119415283f, 1.571428571428573f), decodeAnchorY(2.200000047683716f, -1.6428571428571388f), decodeAnchorX(2.8412699699401855f, 0.7142857142857224f), decodeAnchorY(1.3857142925262451f, 0.6428571428571459f), decodeX(2.84127f), decodeY(1.3857143f));
161         path.curveTo(decodeAnchorX(2.8412699699401855f, -0.7142857142857224f), decodeAnchorY(1.3857142925262451f, -0.6428571428571459f), decodeAnchorX(2.5238094329833984f, 0.7142857142857117f), decodeAnchorY(2.057142734527588f, -0.8571428571428541f), decodeX(2.5238094f), decodeY(2.0571427f));
162         path.curveTo(decodeAnchorX(2.5238094329833984f, -0.7142857142857117f), decodeAnchorY(2.057142734527588f, 0.8571428571428541f), decodeAnchorX(2.1111111640930176f, -0.4285714285714306f), decodeAnchorY(2.700000047683716f, -0.6428571428571388f), decodeX(2.1111112f), decodeY(2.7f));
163         path.closePath();
164         return path;
165     }
166
167
168
169     private Paint decodeGradient1(Shape s) {
170         Rectangle2D bounds = s.getBounds2D();
171         float x = (float)bounds.getX();
172         float y = (float)bounds.getY();
173         float w = (float)bounds.getWidth();
174         float h = (float)bounds.getHeight();
175         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
176                 new float[] { 0.016129032f,0.038709678f,0.061290324f,0.16091082f,0.26451612f,0.4378071f,0.88387096f },
177                 new Color[] { color1,
178                             decodeColor(color1,color2,0.5f),
179                             color2,
180                             decodeColor(color2,color3,0.5f),
181                             color3,
182                             decodeColor(color3,color4,0.5f),
183                             color4});
184     }
185
186     private Paint decodeGradient2(Shape s) {
187         Rectangle2D bounds = s.getBounds2D();
188         float x = (float)bounds.getX();
189         float y = (float)bounds.getY();
190         float w = (float)bounds.getWidth();
191         float h = (float)bounds.getHeight();
192         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
193                 new float[] { 0.0f,0.030645162f,0.061290324f,0.09677419f,0.13225806f,0.22096774f,0.30967742f,0.47434634f,0.82258064f },
194                 new Color[] { color5,
195                             decodeColor(color5,color6,0.5f),
196                             color6,
197                             decodeColor(color6,color7,0.5f),
198                             color7,
199                             decodeColor(color7,color8,0.5f),
200                             color8,
201                             decodeColor(color8,color9,0.5f),
202                             color9});
203     }
204
205     private Paint decodeGradient3(Shape s) {
206         Rectangle2D bounds = s.getBounds2D();
207         float x = (float)bounds.getX();
208         float y = (float)bounds.getY();
209         float w = (float)bounds.getWidth();
210         float h = (float)bounds.getHeight();
211         return decodeGradient((0.0f * w) + x, (0.0f * h) + y, (0.9285714f * w) + x, (0.12244898f * h) + y,
212                 new float[] { 0.0f,0.1f,1.0f },
213                 new Color[] { color10,
214                             decodeColor(color10,color11,0.5f),
215                             color11});
216     }
217
218     private Paint decodeGradient4(Shape s) {
219         Rectangle2D bounds = s.getBounds2D();
220         float x = (float)bounds.getX();
221         float y = (float)bounds.getY();
222         float w = (float)bounds.getWidth();
223         float h = (float)bounds.getHeight();
224         return decodeGradient((-0.045918368f * w) + x, (0.18336426f * h) + y, (0.872449f * w) + x, (0.04050711f * h) + y,
225                 new float[] { 0.0f,0.87096775f,1.0f },
226                 new Color[] { color12,
227                             decodeColor(color12,color10,0.5f),
228                             color10});
229     }
230
231     private Paint decodeGradient5(Shape s) {
232         Rectangle2D bounds = s.getBounds2D();
233         float x = (float)bounds.getX();
234         float y = (float)bounds.getY();
235         float w = (float)bounds.getWidth();
236         float h = (float)bounds.getHeight();
237         return decodeGradient((0.12719299f * w) + x, (0.13157894f * h) + y, (0.90789473f * w) + x, (0.877193f * h) + y,
238                 new float[] { 0.0f,0.5f,1.0f },
239                 new Color[] { color13,
240                             decodeColor(color13,color14,0.5f),
241                             color14});
242     }
243
244     private Paint decodeGradient6(Shape s) {
245         Rectangle2D bounds = s.getBounds2D();
246         float x = (float)bounds.getX();
247         float y = (float)bounds.getY();
248         float w = (float)bounds.getWidth();
249         float h = (float)bounds.getHeight();
250         return decodeGradient((0.86458343f * w) + x, (0.20952381f * h) + y, (0.020833189f * w) + x, (0.95238096f * h) + y,
251                 new float[] { 0.0f,0.5f,1.0f },
252                 new Color[] { color15,
253                             decodeColor(color15,color16,0.5f),
254                             color16});
255     }
256
257
258 }
259
Popular Tags