KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SliderSliderTrackPainter.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 SliderSliderTrackPainter 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 SliderSliderTrackPainter 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 SliderSliderTrackPainter would be created. For the moment at least,
37
//however, all are created for each instance.
38
private Color color1 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, -245);
39     private Color color2 = decodeColor("nimbusBlueGrey", 0.0055555105f, -0.061265234f, 0.05098039f, 0);
40     private Color color3 = decodeColor("nimbusBlueGrey", 0.01010108f, -0.059835073f, 0.10588235f, 0);
41     private Color color4 = decodeColor("nimbusBlueGrey", -0.01111114f, -0.061982628f, 0.062745094f, 0);
42     private Color color5 = decodeColor("nimbusBlueGrey", -0.00505054f, -0.058639523f, 0.086274505f, 0);
43     private Color color6 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, -111);
44     private Color color7 = decodeColor("nimbusBlueGrey", 0.0f, -0.034093194f, -0.12941176f, 0);
45     private Color color8 = decodeColor("nimbusBlueGrey", 0.01111114f, -0.023821115f, -0.06666666f, 0);
46     private Color color9 = decodeColor("nimbusBlueGrey", -0.008547008f, -0.03314536f, -0.086274505f, 0);
47     private Color color10 = decodeColor("nimbusBlueGrey", 0.004273474f, -0.040256046f, -0.019607842f, 0);
48     private Color color11 = decodeColor("nimbusBlueGrey", 0.0f, -0.03626889f, 0.04705882f, 0);
49
50
51     //Array of current component colors, updated in each paint call
52
private Object JavaDoc[] componentColors;
53
54     public SliderSliderTrackPainter(PaintContext ctx, int state) {
55         super();
56         this.state = state;
57         this.ctx = ctx;
58     }
59
60     @Override JavaDoc
61     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
62         //populate componentColors array with colors calculated in getExtendedCacheKeys call
63
componentColors = extendedCacheKeys;
64         //generate this entire method. Each state/bg/fg/border combo that has
65
//been painted gets its own KEY and paint method.
66
switch(state) {
67             case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
68             case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
69
70         }
71     }
72         
73
74
75     @Override JavaDoc
76     protected final PaintContext getPaintContext() {
77         return ctx;
78     }
79
80     private void paintBackgroundDisabled(Graphics2D g) {
81         roundRect = decodeRoundRect1();
82         g.setPaint(color1);
83         g.fill(roundRect);
84         roundRect = decodeRoundRect2();
85         g.setPaint(decodeGradient1(roundRect));
86         g.fill(roundRect);
87         roundRect = decodeRoundRect3();
88         g.setPaint(decodeGradient2(roundRect));
89         g.fill(roundRect);
90
91     }
92
93     private void paintBackgroundEnabled(Graphics2D g) {
94         roundRect = decodeRoundRect4();
95         g.setPaint(color6);
96         g.fill(roundRect);
97         roundRect = decodeRoundRect2();
98         g.setPaint(decodeGradient3(roundRect));
99         g.fill(roundRect);
100         roundRect = decodeRoundRect5();
101         g.setPaint(decodeGradient4(roundRect));
102         g.fill(roundRect);
103
104     }
105
106
107
108     private RoundRectangle2D decodeRoundRect1() {
109         roundRect.setRoundRect(decodeX(0.2f), //x
110
decodeY(1.6f), //y
111
decodeX(2.8f) - decodeX(0.2f), //width
112
decodeY(2.8333333f) - decodeY(1.6f), //height
113
8.705882f, 8.705882f); //rounding
114
return roundRect;
115     }
116
117     private RoundRectangle2D decodeRoundRect2() {
118         roundRect.setRoundRect(decodeX(0.0f), //x
119
decodeY(1.0f), //y
120
decodeX(3.0f) - decodeX(0.0f), //width
121
decodeY(2.0f) - decodeY(1.0f), //height
122
4.9411764f, 4.9411764f); //rounding
123
return roundRect;
124     }
125
126     private RoundRectangle2D decodeRoundRect3() {
127         roundRect.setRoundRect(decodeX(0.29411763f), //x
128
decodeY(1.2f), //y
129
decodeX(2.7058823f) - decodeX(0.29411763f), //width
130
decodeY(2.0f) - decodeY(1.2f), //height
131
4.0f, 4.0f); //rounding
132
return roundRect;
133     }
134
135     private RoundRectangle2D decodeRoundRect4() {
136         roundRect.setRoundRect(decodeX(0.2f), //x
137
decodeY(1.6f), //y
138
decodeX(2.8f) - decodeX(0.2f), //width
139
decodeY(2.1666667f) - decodeY(1.6f), //height
140
8.705882f, 8.705882f); //rounding
141
return roundRect;
142     }
143
144     private RoundRectangle2D decodeRoundRect5() {
145         roundRect.setRoundRect(decodeX(0.28823528f), //x
146
decodeY(1.2f), //y
147
decodeX(2.7f) - decodeX(0.28823528f), //width
148
decodeY(2.0f) - decodeY(1.2f), //height
149
4.0f, 4.0f); //rounding
150
return roundRect;
151     }
152
153
154
155     private Paint decodeGradient1(Shape s) {
156         Rectangle2D bounds = s.getBounds2D();
157         float x = (float)bounds.getX();
158         float y = (float)bounds.getY();
159         float w = (float)bounds.getWidth();
160         float h = (float)bounds.getHeight();
161         return decodeGradient((0.25f * w) + x, (0.07647059f * h) + y, (0.25f * w) + x, (0.9117647f * h) + y,
162                 new float[] { 0.0f,0.5f,1.0f },
163                 new Color[] { color2,
164                             decodeColor(color2,color3,0.5f),
165                             color3});
166     }
167
168     private Paint decodeGradient2(Shape s) {
169         Rectangle2D bounds = s.getBounds2D();
170         float x = (float)bounds.getX();
171         float y = (float)bounds.getY();
172         float w = (float)bounds.getWidth();
173         float h = (float)bounds.getHeight();
174         return decodeGradient((0.25f * w) + x, (0.0f * h) + y, (0.25f * w) + x, (1.0f * h) + y,
175                 new float[] { 0.0f,0.13770053f,0.27540106f,0.63770056f,1.0f },
176                 new Color[] { color4,
177                             decodeColor(color4,color5,0.5f),
178                             color5,
179                             decodeColor(color5,color3,0.5f),
180                             color3});
181     }
182
183     private Paint decodeGradient3(Shape s) {
184         Rectangle2D bounds = s.getBounds2D();
185         float x = (float)bounds.getX();
186         float y = (float)bounds.getY();
187         float w = (float)bounds.getWidth();
188         float h = (float)bounds.getHeight();
189         return decodeGradient((0.25f * w) + x, (0.07647059f * h) + y, (0.25f * w) + x, (0.9117647f * h) + y,
190                 new float[] { 0.0f,0.5f,1.0f },
191                 new Color[] { color7,
192                             decodeColor(color7,color8,0.5f),
193                             color8});
194     }
195
196     private Paint decodeGradient4(Shape s) {
197         Rectangle2D bounds = s.getBounds2D();
198         float x = (float)bounds.getX();
199         float y = (float)bounds.getY();
200         float w = (float)bounds.getWidth();
201         float h = (float)bounds.getHeight();
202         return decodeGradient((0.25f * w) + x, (0.0f * h) + y, (0.25f * w) + x, (1.0f * h) + y,
203                 new float[] { 0.0f,0.13770053f,0.27540106f,0.4906417f,0.7058824f },
204                 new Color[] { color9,
205                             decodeColor(color9,color10,0.5f),
206                             color10,
207                             decodeColor(color10,color11,0.5f),
208                             color11});
209     }
210
211
212 }
213
Popular Tags