KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneCloseButtonPainter.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 InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneCloseButtonPainter 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 InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneCloseButtonPainter 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     static final int BACKGROUND_MOUSEOVER = 3;
25     static final int BACKGROUND_PRESSED = 4;
26     static final int BACKGROUND_ENABLED_WINDOWNOTFOCUSED = 5;
27     static final int BACKGROUND_MOUSEOVER_WINDOWNOTFOCUSED = 6;
28     static final int BACKGROUND_PRESSED_WINDOWNOTFOCUSED = 7;
29
30
31     private int state; //refers to one of the static final ints above
32
private PaintContext ctx;
33
34     //the following 4 variables are reused during the painting code of the layers
35
private Path2D path = new Path2D.Float();
36     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
37     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
38     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
39
40     //All Colors used for painting are stored here. Ideally, only those colors being used
41
//by a particular instance of InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneCloseButtonPainter would be created. For the moment at least,
42
//however, all are created for each instance.
43
private Color color1 = decodeColor("nimbusRed", 0.5893519f, -0.75736576f, 0.09411764f, 0);
44     private Color color2 = decodeColor("nimbusRed", 0.5962963f, -0.71005917f, 0.0f, 0);
45     private Color color3 = decodeColor("nimbusRed", 0.6005698f, -0.7200287f, -0.015686274f, -122);
46     private Color color4 = decodeColor("nimbusBlueGrey", 0.0055555105f, -0.062449392f, 0.07058823f, 0);
47     private Color color5 = decodeColor("nimbusBlueGrey", 0.0055555105f, -0.0029994324f, -0.38039216f, -185);
48     private Color color6 = decodeColor("nimbusRed", -0.014814814f, 0.20118344f, -0.4431373f, 0);
49     private Color color7 = decodeColor("nimbusRed", -2.7342606E-4f, 0.13829035f, -0.039215684f, 0);
50     private Color color8 = decodeColor("nimbusRed", 6.890595E-4f, -0.36665577f, 0.11764705f, 0);
51     private Color color9 = decodeColor("nimbusRed", -0.001021713f, 0.101804554f, -0.031372547f, 0);
52     private Color color10 = decodeColor("nimbusRed", -2.7342606E-4f, 0.13243341f, -0.035294116f, 0);
53     private Color color11 = decodeColor("nimbusRed", -2.7342606E-4f, 0.002258718f, 0.06666666f, 0);
54     private Color color12 = decodeColor("nimbusRed", 0.0056530247f, 0.0040003657f, -0.38431373f, -122);
55     private Color color13 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, 0);
56     private Color color14 = decodeColor("nimbusRed", -0.014814814f, 0.20118344f, -0.3882353f, 0);
57     private Color color15 = decodeColor("nimbusRed", -0.014814814f, 0.20118344f, -0.13333333f, 0);
58     private Color color16 = decodeColor("nimbusRed", 6.890595E-4f, -0.38929275f, 0.1607843f, 0);
59     private Color color17 = decodeColor("nimbusRed", 2.537202E-5f, 0.012294531f, 0.043137252f, 0);
60     private Color color18 = decodeColor("nimbusRed", -2.7342606E-4f, 0.033585668f, 0.039215684f, 0);
61     private Color color19 = decodeColor("nimbusRed", -2.7342606E-4f, -0.07198727f, 0.14117646f, 0);
62     private Color color20 = decodeColor("nimbusRed", -0.014814814f, 0.20118344f, 0.0039215684f, -122);
63     private Color color21 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, -140);
64     private Color color22 = decodeColor("nimbusRed", -0.014814814f, 0.20118344f, -0.49411768f, 0);
65     private Color color23 = decodeColor("nimbusRed", -0.014814814f, 0.20118344f, -0.20392159f, 0);
66     private Color color24 = decodeColor("nimbusRed", -0.014814814f, -0.21260965f, 0.019607842f, 0);
67     private Color color25 = decodeColor("nimbusRed", -0.014814814f, 0.17340565f, -0.09803921f, 0);
68     private Color color26 = decodeColor("nimbusRed", -0.014814814f, 0.20118344f, -0.10588235f, 0);
69     private Color color27 = decodeColor("nimbusRed", -0.014814814f, 0.20118344f, -0.04705882f, 0);
70     private Color color28 = decodeColor("nimbusRed", -0.014814814f, 0.20118344f, -0.31764707f, -122);
71     private Color color29 = decodeColor("nimbusRed", 0.5962963f, -0.6994788f, -0.07058823f, 0);
72     private Color color30 = decodeColor("nimbusRed", 0.5962963f, -0.66245294f, -0.23137257f, 0);
73     private Color color31 = decodeColor("nimbusRed", 0.58518517f, -0.77649516f, 0.21568626f, 0);
74     private Color color32 = decodeColor("nimbusRed", 0.5962963f, -0.7372781f, 0.10196078f, 0);
75     private Color color33 = decodeColor("nimbusRed", 0.5962963f, -0.73911506f, 0.12549019f, 0);
76     private Color color34 = decodeColor("nimbusBlueGrey", 0.0f, -0.027957506f, -0.31764707f, 0);
77
78
79     //Array of current component colors, updated in each paint call
80
private Object JavaDoc[] componentColors;
81
82     public InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneCloseButtonPainter(PaintContext ctx, int state) {
83         super();
84         this.state = state;
85         this.ctx = ctx;
86     }
87
88     @Override JavaDoc
89     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
90         //populate componentColors array with colors calculated in getExtendedCacheKeys call
91
componentColors = extendedCacheKeys;
92         //generate this entire method. Each state/bg/fg/border combo that has
93
//been painted gets its own KEY and paint method.
94
switch(state) {
95             case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
96             case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
97             case BACKGROUND_MOUSEOVER: paintBackgroundMouseOver(g); break;
98             case BACKGROUND_PRESSED: paintBackgroundPressed(g); break;
99             case BACKGROUND_ENABLED_WINDOWNOTFOCUSED: paintBackgroundEnabledAndWindowNotFocused(g); break;
100             case BACKGROUND_MOUSEOVER_WINDOWNOTFOCUSED: paintBackgroundMouseOverAndWindowNotFocused(g); break;
101             case BACKGROUND_PRESSED_WINDOWNOTFOCUSED: paintBackgroundPressedAndWindowNotFocused(g); break;
102
103         }
104     }
105         
106
107
108     @Override JavaDoc
109     protected final PaintContext getPaintContext() {
110         return ctx;
111     }
112
113     private void paintBackgroundDisabled(Graphics2D g) {
114         roundRect = decodeRoundRect1();
115         g.setPaint(decodeGradient1(roundRect));
116         g.fill(roundRect);
117         path = decodePath1();
118         g.setPaint(color3);
119         g.fill(path);
120         path = decodePath2();
121         g.setPaint(color4);
122         g.fill(path);
123
124     }
125
126     private void paintBackgroundEnabled(Graphics2D g) {
127         roundRect = decodeRoundRect2();
128         g.setPaint(color5);
129         g.fill(roundRect);
130         roundRect = decodeRoundRect1();
131         g.setPaint(decodeGradient2(roundRect));
132         g.fill(roundRect);
133         roundRect = decodeRoundRect3();
134         g.setPaint(decodeGradient3(roundRect));
135         g.fill(roundRect);
136         path = decodePath1();
137         g.setPaint(color12);
138         g.fill(path);
139         path = decodePath2();
140         g.setPaint(color13);
141         g.fill(path);
142
143     }
144
145     private void paintBackgroundMouseOver(Graphics2D g) {
146         roundRect = decodeRoundRect2();
147         g.setPaint(color5);
148         g.fill(roundRect);
149         roundRect = decodeRoundRect1();
150         g.setPaint(decodeGradient4(roundRect));
151         g.fill(roundRect);
152         roundRect = decodeRoundRect4();
153         g.setPaint(decodeGradient5(roundRect));
154         g.fill(roundRect);
155         path = decodePath1();
156         g.setPaint(color20);
157         g.fill(path);
158         path = decodePath2();
159         g.setPaint(color13);
160         g.fill(path);
161
162     }
163
164     private void paintBackgroundPressed(Graphics2D g) {
165         roundRect = decodeRoundRect2();
166         g.setPaint(color21);
167         g.fill(roundRect);
168         roundRect = decodeRoundRect1();
169         g.setPaint(decodeGradient6(roundRect));
170         g.fill(roundRect);
171         roundRect = decodeRoundRect3();
172         g.setPaint(decodeGradient7(roundRect));
173         g.fill(roundRect);
174         path = decodePath1();
175         g.setPaint(color28);
176         g.fill(path);
177         path = decodePath2();
178         g.setPaint(color13);
179         g.fill(path);
180
181     }
182
183     private void paintBackgroundEnabledAndWindowNotFocused(Graphics2D g) {
184         roundRect = decodeRoundRect1();
185         g.setPaint(decodeGradient8(roundRect));
186         g.fill(roundRect);
187         roundRect = decodeRoundRect3();
188         g.setPaint(decodeGradient9(roundRect));
189         g.fill(roundRect);
190         path = decodePath2();
191         g.setPaint(color34);
192         g.fill(path);
193
194     }
195
196     private void paintBackgroundMouseOverAndWindowNotFocused(Graphics2D g) {
197         roundRect = decodeRoundRect2();
198         g.setPaint(color5);
199         g.fill(roundRect);
200         roundRect = decodeRoundRect1();
201         g.setPaint(decodeGradient4(roundRect));
202         g.fill(roundRect);
203         roundRect = decodeRoundRect4();
204         g.setPaint(decodeGradient5(roundRect));
205         g.fill(roundRect);
206         path = decodePath1();
207         g.setPaint(color20);
208         g.fill(path);
209         path = decodePath2();
210         g.setPaint(color13);
211         g.fill(path);
212
213     }
214
215     private void paintBackgroundPressedAndWindowNotFocused(Graphics2D g) {
216         roundRect = decodeRoundRect2();
217         g.setPaint(color21);
218         g.fill(roundRect);
219         roundRect = decodeRoundRect1();
220         g.setPaint(decodeGradient6(roundRect));
221         g.fill(roundRect);
222         roundRect = decodeRoundRect3();
223         g.setPaint(decodeGradient7(roundRect));
224         g.fill(roundRect);
225         path = decodePath1();
226         g.setPaint(color28);
227         g.fill(path);
228         path = decodePath2();
229         g.setPaint(color13);
230         g.fill(path);
231
232     }
233
234
235
236     private RoundRectangle2D decodeRoundRect1() {
237         roundRect.setRoundRect(decodeX(1.0f), //x
238
decodeY(1.0f), //y
239
decodeX(2.0f) - decodeX(1.0f), //width
240
decodeY(1.9444444f) - decodeY(1.0f), //height
241
8.6f, 8.6f); //rounding
242
return roundRect;
243     }
244
245     private Path2D decodePath1() {
246         path.reset();
247         path.moveTo(decodeX(1.25f), decodeY(1.7373737f));
248         path.lineTo(decodeX(1.3002392f), decodeY(1.794192f));
249         path.lineTo(decodeX(1.5047847f), decodeY(1.5909091f));
250         path.lineTo(decodeX(1.6842105f), decodeY(1.7954545f));
251         path.lineTo(decodeX(1.7595694f), decodeY(1.719697f));
252         path.lineTo(decodeX(1.5956938f), decodeY(1.5239899f));
253         path.lineTo(decodeX(1.7535884f), decodeY(1.3409091f));
254         path.lineTo(decodeX(1.6830144f), decodeY(1.2537879f));
255         path.lineTo(decodeX(1.5083733f), decodeY(1.4406565f));
256         path.lineTo(decodeX(1.3301436f), decodeY(1.2563131f));
257         path.lineTo(decodeX(1.257177f), decodeY(1.3320707f));
258         path.lineTo(decodeX(1.4270334f), decodeY(1.5252526f));
259         path.lineTo(decodeX(1.25f), decodeY(1.7373737f));
260         path.closePath();
261         return path;
262     }
263
264     private Path2D decodePath2() {
265         path.reset();
266         path.moveTo(decodeX(1.257177f), decodeY(1.2828283f));
267         path.lineTo(decodeX(1.3217703f), decodeY(1.2133838f));
268         path.lineTo(decodeX(1.5f), decodeY(1.4040405f));
269         path.lineTo(decodeX(1.673445f), decodeY(1.2108586f));
270         path.lineTo(decodeX(1.7440192f), decodeY(1.2853535f));
271         path.lineTo(decodeX(1.5669856f), decodeY(1.4709597f));
272         path.lineTo(decodeX(1.7488039f), decodeY(1.6527778f));
273         path.lineTo(decodeX(1.673445f), decodeY(1.7398989f));
274         path.lineTo(decodeX(1.4988039f), decodeY(1.5416667f));
275         path.lineTo(decodeX(1.3313397f), decodeY(1.7424242f));
276         path.lineTo(decodeX(1.2523923f), decodeY(1.6565657f));
277         path.lineTo(decodeX(1.4366028f), decodeY(1.4722222f));
278         path.lineTo(decodeX(1.257177f), decodeY(1.2828283f));
279         path.closePath();
280         return path;
281     }
282
283     private RoundRectangle2D decodeRoundRect2() {
284         roundRect.setRoundRect(decodeX(1.0f), //x
285
decodeY(1.6111112f), //y
286
decodeX(2.0f) - decodeX(1.0f), //width
287
decodeY(2.0f) - decodeY(1.6111112f), //height
288
6.0f, 6.0f); //rounding
289
return roundRect;
290     }
291
292     private RoundRectangle2D decodeRoundRect3() {
293         roundRect.setRoundRect(decodeX(1.0526316f), //x
294
decodeY(1.0530303f), //y
295
decodeX(1.9473684f) - decodeX(1.0526316f), //width
296
decodeY(1.8863636f) - decodeY(1.0530303f), //height
297
6.75f, 6.75f); //rounding
298
return roundRect;
299     }
300
301     private RoundRectangle2D decodeRoundRect4() {
302         roundRect.setRoundRect(decodeX(1.0526316f), //x
303
decodeY(1.0517677f), //y
304
decodeX(1.9473684f) - decodeX(1.0526316f), //width
305
decodeY(1.8851011f) - decodeY(1.0517677f), //height
306
6.75f, 6.75f); //rounding
307
return roundRect;
308     }
309
310
311
312     private Paint decodeGradient1(Shape s) {
313         Rectangle2D bounds = s.getBounds2D();
314         float x = (float)bounds.getX();
315         float y = (float)bounds.getY();
316         float w = (float)bounds.getWidth();
317         float h = (float)bounds.getHeight();
318         return decodeGradient((0.24868421f * w) + x, (0.0014705883f * h) + y, (0.24868421f * w) + x, (1.0f * h) + y,
319                 new float[] { 0.0f,0.5f,1.0f },
320                 new Color[] { color1,
321                             decodeColor(color1,color2,0.5f),
322                             color2});
323     }
324
325     private Paint decodeGradient2(Shape s) {
326         Rectangle2D bounds = s.getBounds2D();
327         float x = (float)bounds.getX();
328         float y = (float)bounds.getY();
329         float w = (float)bounds.getWidth();
330         float h = (float)bounds.getHeight();
331         return decodeGradient((0.24868421f * w) + x, (0.0014705883f * h) + y, (0.24868421f * w) + x, (1.0f * h) + y,
332                 new float[] { 0.0f,0.5f,1.0f },
333                 new Color[] { color6,
334                             decodeColor(color6,color7,0.5f),
335                             color7});
336     }
337
338     private Paint decodeGradient3(Shape s) {
339         Rectangle2D bounds = s.getBounds2D();
340         float x = (float)bounds.getX();
341         float y = (float)bounds.getY();
342         float w = (float)bounds.getWidth();
343         float h = (float)bounds.getHeight();
344         return decodeGradient((0.25f * w) + x, (0.0f * h) + y, (0.25441176f * w) + x, (1.0016667f * h) + y,
345                 new float[] { 0.0f,0.26988637f,0.53977275f,0.5951705f,0.6505682f,0.8252841f,1.0f },
346                 new Color[] { color8,
347                             decodeColor(color8,color9,0.5f),
348                             color9,
349                             decodeColor(color9,color10,0.5f),
350                             color10,
351                             decodeColor(color10,color11,0.5f),
352                             color11});
353     }
354
355     private Paint decodeGradient4(Shape s) {
356         Rectangle2D bounds = s.getBounds2D();
357         float x = (float)bounds.getX();
358         float y = (float)bounds.getY();
359         float w = (float)bounds.getWidth();
360         float h = (float)bounds.getHeight();
361         return decodeGradient((0.24868421f * w) + x, (0.0014705883f * h) + y, (0.24868421f * w) + x, (1.0f * h) + y,
362                 new float[] { 0.0f,0.5f,1.0f },
363                 new Color[] { color14,
364                             decodeColor(color14,color15,0.5f),
365                             color15});
366     }
367
368     private Paint decodeGradient5(Shape s) {
369         Rectangle2D bounds = s.getBounds2D();
370         float x = (float)bounds.getX();
371         float y = (float)bounds.getY();
372         float w = (float)bounds.getWidth();
373         float h = (float)bounds.getHeight();
374         return decodeGradient((0.25f * w) + x, (0.0f * h) + y, (0.25441176f * w) + x, (1.0016667f * h) + y,
375                 new float[] { 0.0f,0.26988637f,0.53977275f,0.5951705f,0.6505682f,0.81480503f,0.97904193f },
376                 new Color[] { color16,
377                             decodeColor(color16,color17,0.5f),
378                             color17,
379                             decodeColor(color17,color18,0.5f),
380                             color18,
381                             decodeColor(color18,color19,0.5f),
382                             color19});
383     }
384
385     private Paint decodeGradient6(Shape s) {
386         Rectangle2D bounds = s.getBounds2D();
387         float x = (float)bounds.getX();
388         float y = (float)bounds.getY();
389         float w = (float)bounds.getWidth();
390         float h = (float)bounds.getHeight();
391         return decodeGradient((0.24868421f * w) + x, (0.0014705883f * h) + y, (0.24868421f * w) + x, (1.0f * h) + y,
392                 new float[] { 0.0f,0.5f,1.0f },
393                 new Color[] { color22,
394                             decodeColor(color22,color23,0.5f),
395                             color23});
396     }
397
398     private Paint decodeGradient7(Shape s) {
399         Rectangle2D bounds = s.getBounds2D();
400         float x = (float)bounds.getX();
401         float y = (float)bounds.getY();
402         float w = (float)bounds.getWidth();
403         float h = (float)bounds.getHeight();
404         return decodeGradient((0.25f * w) + x, (0.0f * h) + y, (0.25441176f * w) + x, (1.0016667f * h) + y,
405                 new float[] { 0.0f,0.26988637f,0.53977275f,0.5951705f,0.6505682f,0.81630206f,0.98203593f },
406                 new Color[] { color24,
407                             decodeColor(color24,color25,0.5f),
408                             color25,
409                             decodeColor(color25,color26,0.5f),
410                             color26,
411                             decodeColor(color26,color27,0.5f),
412                             color27});
413     }
414
415     private Paint decodeGradient8(Shape s) {
416         Rectangle2D bounds = s.getBounds2D();
417         float x = (float)bounds.getX();
418         float y = (float)bounds.getY();
419         float w = (float)bounds.getWidth();
420         float h = (float)bounds.getHeight();
421         return decodeGradient((0.24868421f * w) + x, (0.0014705883f * h) + y, (0.24868421f * w) + x, (1.0f * h) + y,
422                 new float[] { 0.0f,0.5f,1.0f },
423                 new Color[] { color29,
424                             decodeColor(color29,color30,0.5f),
425                             color30});
426     }
427
428     private Paint decodeGradient9(Shape s) {
429         Rectangle2D bounds = s.getBounds2D();
430         float x = (float)bounds.getX();
431         float y = (float)bounds.getY();
432         float w = (float)bounds.getWidth();
433         float h = (float)bounds.getHeight();
434         return decodeGradient((0.25f * w) + x, (0.0f * h) + y, (0.25441176f * w) + x, (1.0016667f * h) + y,
435                 new float[] { 0.0f,0.24101797f,0.48203593f,0.5838324f,0.6856288f,0.8428144f,1.0f },
436                 new Color[] { color31,
437                             decodeColor(color31,color32,0.5f),
438                             color32,
439                             decodeColor(color32,color32,0.5f),
440                             color32,
441                             decodeColor(color32,color33,0.5f),
442                             color33});
443     }
444
445
446 }
447
Popular Tags