KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SplitPaneSplitPaneDividerPainter.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 SplitPaneSplitPaneDividerPainter 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 SplitPaneSplitPaneDividerPainter 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 FOREGROUND_ENABLED = 3;
25     static final int FOREGROUND_ENABLED_VERTICAL = 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 SplitPaneSplitPaneDividerPainter would be created. For the moment at least,
39
//however, all are created for each instance.
40
private Color color1 = decodeColor("nimbusBlueGrey", 0.0f, -0.017358616f, -0.11372548f, 0);
41     private Color color2 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.102396235f, 0.21960783f, 0);
42     private Color color3 = decodeColor("nimbusBlueGrey", 0.0f, -0.07016757f, 0.12941176f, 0);
43     private Color color4 = decodeColor("nimbusFocus", 0.0f, 0.0f, 0.0f, 0);
44     private Color color5 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, 0);
45     private Color color6 = decodeColor("nimbusBlueGrey", 0.0f, -0.048026316f, 0.007843137f, 0);
46     private Color color7 = decodeColor("nimbusBlueGrey", 0.0055555105f, -0.06970999f, 0.21568626f, 0);
47     private Color color8 = decodeColor("nimbusBlueGrey", 0.0f, -0.06704806f, 0.06666666f, 0);
48     private Color color9 = decodeColor("nimbusBlueGrey", 0.0f, -0.019617222f, -0.09803921f, 0);
49     private Color color10 = decodeColor("nimbusBlueGrey", 0.004273474f, -0.03790062f, -0.043137252f, 0);
50     private Color color11 = decodeColor("nimbusBlueGrey", -0.111111104f, -0.106573746f, 0.24705881f, 0);
51     private Color color12 = decodeColor("nimbusBlueGrey", 0.0f, -0.049301825f, 0.02352941f, 0);
52     private Color color13 = decodeColor("nimbusBlueGrey", -0.006944418f, -0.07399663f, 0.11372548f, 0);
53     private Color color14 = decodeColor("nimbusBlueGrey", -0.018518567f, -0.06998578f, 0.12549019f, 0);
54     private Color color15 = decodeColor("nimbusBlueGrey", 0.0f, -0.050526317f, 0.039215684f, 0);
55
56
57     //Array of current component colors, updated in each paint call
58
private Object JavaDoc[] componentColors;
59
60     public SplitPaneSplitPaneDividerPainter(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_ENABLED: paintBackgroundEnabled(g); break;
74             case BACKGROUND_FOCUSED: paintBackgroundFocused(g); break;
75             case FOREGROUND_ENABLED: paintForegroundEnabled(g); break;
76             case FOREGROUND_ENABLED_VERTICAL: paintForegroundEnabledAndVertical(g); break;
77
78         }
79     }
80         
81
82
83     @Override JavaDoc
84     protected final PaintContext getPaintContext() {
85         return ctx;
86     }
87
88     private void paintBackgroundEnabled(Graphics2D g) {
89         rect = decodeRect1();
90         g.setPaint(decodeGradient1(rect));
91         g.fill(rect);
92
93     }
94
95     private void paintBackgroundFocused(Graphics2D g) {
96         rect = decodeRect1();
97         g.setPaint(decodeGradient2(rect));
98         g.fill(rect);
99
100     }
101
102     private void paintForegroundEnabled(Graphics2D g) {
103         roundRect = decodeRoundRect1();
104         g.setPaint(decodeGradient3(roundRect));
105         g.fill(roundRect);
106         roundRect = decodeRoundRect2();
107         g.setPaint(decodeGradient4(roundRect));
108         g.fill(roundRect);
109
110     }
111
112     private void paintForegroundEnabledAndVertical(Graphics2D g) {
113         roundRect = decodeRoundRect3();
114         g.setPaint(decodeGradient5(roundRect));
115         g.fill(roundRect);
116         rect = decodeRect2();
117         g.setPaint(decodeGradient6(rect));
118         g.fill(rect);
119
120     }
121
122
123
124     private Rectangle2D decodeRect1() {
125             rect.setRect(decodeX(1.0f), //x
126
decodeY(0.0f), //y
127
decodeX(2.0f) - decodeX(1.0f), //width
128
decodeY(3.0f) - decodeY(0.0f)); //height
129
return rect;
130     }
131
132     private RoundRectangle2D decodeRoundRect1() {
133         roundRect.setRoundRect(decodeX(1.05f), //x
134
decodeY(1.3f), //y
135
decodeX(1.95f) - decodeX(1.05f), //width
136
decodeY(1.8f) - decodeY(1.3f), //height
137
3.6666667f, 3.6666667f); //rounding
138
return roundRect;
139     }
140
141     private RoundRectangle2D decodeRoundRect2() {
142         roundRect.setRoundRect(decodeX(1.1f), //x
143
decodeY(1.4f), //y
144
decodeX(1.9f) - decodeX(1.1f), //width
145
decodeY(1.7f) - decodeY(1.4f), //height
146
4.0f, 4.0f); //rounding
147
return roundRect;
148     }
149
150     private RoundRectangle2D decodeRoundRect3() {
151         roundRect.setRoundRect(decodeX(1.3f), //x
152
decodeY(1.1428572f), //y
153
decodeX(1.7f) - decodeX(1.3f), //width
154
decodeY(1.8214285f) - decodeY(1.1428572f), //height
155
4.0f, 4.0f); //rounding
156
return roundRect;
157     }
158
159     private Rectangle2D decodeRect2() {
160             rect.setRect(decodeX(1.4f), //x
161
decodeY(1.1785715f), //y
162
decodeX(1.6f) - decodeX(1.4f), //width
163
decodeY(1.7678571f) - decodeY(1.1785715f)); //height
164
return rect;
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.058064517f,0.08064516f,0.103225805f,0.116129026f,0.12903225f,0.43387097f,0.7387097f,0.77903223f,0.81935483f,0.85806453f,0.8967742f },
177                 new Color[] { color1,
178                             decodeColor(color1,color2,0.5f),
179                             color2,
180                             decodeColor(color2,color3,0.5f),
181                             color3,
182                             decodeColor(color3,color3,0.5f),
183                             color3,
184                             decodeColor(color3,color2,0.5f),
185                             color2,
186                             decodeColor(color2,color1,0.5f),
187                             color1});
188     }
189
190     private Paint decodeGradient2(Shape s) {
191         Rectangle2D bounds = s.getBounds2D();
192         float x = (float)bounds.getX();
193         float y = (float)bounds.getY();
194         float w = (float)bounds.getWidth();
195         float h = (float)bounds.getHeight();
196         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
197                 new float[] { 0.058064517f,0.08064516f,0.103225805f,0.1166129f,0.13f,0.43f,0.73f,0.7746774f,0.81935483f,0.85806453f,0.8967742f },
198                 new Color[] { color1,
199                             decodeColor(color1,color4,0.5f),
200                             color4,
201                             decodeColor(color4,color3,0.5f),
202                             color3,
203                             decodeColor(color3,color3,0.5f),
204                             color3,
205                             decodeColor(color3,color4,0.5f),
206                             color4,
207                             decodeColor(color4,color1,0.5f),
208                             color1});
209     }
210
211     private Paint decodeGradient3(Shape s) {
212         Rectangle2D bounds = s.getBounds2D();
213         float x = (float)bounds.getX();
214         float y = (float)bounds.getY();
215         float w = (float)bounds.getWidth();
216         float h = (float)bounds.getHeight();
217         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
218                 new float[] { 0.20645161f,0.5f,0.7935484f },
219                 new Color[] { color1,
220                             decodeColor(color1,color5,0.5f),
221                             color5});
222     }
223
224     private Paint decodeGradient4(Shape s) {
225         Rectangle2D bounds = s.getBounds2D();
226         float x = (float)bounds.getX();
227         float y = (float)bounds.getY();
228         float w = (float)bounds.getWidth();
229         float h = (float)bounds.getHeight();
230         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
231                 new float[] { 0.090322584f,0.2951613f,0.5f,0.5822581f,0.66451615f },
232                 new Color[] { color6,
233                             decodeColor(color6,color7,0.5f),
234                             color7,
235                             decodeColor(color7,color8,0.5f),
236                             color8});
237     }
238
239     private Paint decodeGradient5(Shape s) {
240         Rectangle2D bounds = s.getBounds2D();
241         float x = (float)bounds.getX();
242         float y = (float)bounds.getY();
243         float w = (float)bounds.getWidth();
244         float h = (float)bounds.getHeight();
245         return decodeGradient((0.25f * w) + x, (0.0f * h) + y, (0.75f * w) + x, (1.0f * h) + y,
246                 new float[] { 0.0f,0.42096773f,0.84193546f,0.8951613f,0.9483871f },
247                 new Color[] { color9,
248                             decodeColor(color9,color10,0.5f),
249                             color10,
250                             decodeColor(color10,color11,0.5f),
251                             color11});
252     }
253
254     private Paint decodeGradient6(Shape s) {
255         Rectangle2D bounds = s.getBounds2D();
256         float x = (float)bounds.getX();
257         float y = (float)bounds.getY();
258         float w = (float)bounds.getWidth();
259         float h = (float)bounds.getHeight();
260         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
261                 new float[] { 0.0f,0.08064516f,0.16129032f,0.5129032f,0.86451614f,0.88548386f,0.90645164f },
262                 new Color[] { color12,
263                             decodeColor(color12,color13,0.5f),
264                             color13,
265                             decodeColor(color13,color14,0.5f),
266                             color14,
267                             decodeColor(color14,color15,0.5f),
268                             color15});
269     }
270
271
272 }
273
Popular Tags