KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * DesktopIconPainter.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 DesktopIconPainter 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 DesktopIconPainter to determine which region/state is being painted
21
//by that instance.
22
static final int BACKGROUND_ENABLED = 1;
23
24
25     private int state; //refers to one of the static final ints above
26
private PaintContext ctx;
27
28     //the following 4 variables are reused during the painting code of the layers
29
private Path2D path = new Path2D.Float();
30     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
31     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
32     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
33
34     //All Colors used for painting are stored here. Ideally, only those colors being used
35
//by a particular instance of DesktopIconPainter would be created. For the moment at least,
36
//however, all are created for each instance.
37
private Color color1 = decodeColor("nimbusBase", 0.02551502f, -0.47885156f, -0.34901965f, 0);
38     private Color color2 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.102261856f, 0.20392156f, 0);
39     private Color color3 = decodeColor("nimbusBlueGrey", 0.0f, -0.0682728f, 0.09019607f, 0);
40     private Color color4 = decodeColor("nimbusBlueGrey", -0.01111114f, -0.088974595f, 0.16470587f, 0);
41     private Color color5 = decodeColor("nimbusBlueGrey", 0.0f, -0.029445238f, -0.019607842f, 0);
42
43
44     //Array of current component colors, updated in each paint call
45
private Object JavaDoc[] componentColors;
46
47     public DesktopIconPainter(PaintContext ctx, int state) {
48         super();
49         this.state = state;
50         this.ctx = ctx;
51     }
52
53     @Override JavaDoc
54     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
55         //populate componentColors array with colors calculated in getExtendedCacheKeys call
56
componentColors = extendedCacheKeys;
57         //generate this entire method. Each state/bg/fg/border combo that has
58
//been painted gets its own KEY and paint method.
59
switch(state) {
60             case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
61
62         }
63     }
64         
65
66
67     @Override JavaDoc
68     protected final PaintContext getPaintContext() {
69         return ctx;
70     }
71
72     private void paintBackgroundEnabled(Graphics2D g) {
73         roundRect = decodeRoundRect1();
74         g.setPaint(color1);
75         g.fill(roundRect);
76         roundRect = decodeRoundRect2();
77         g.setPaint(decodeGradient1(roundRect));
78         g.fill(roundRect);
79         rect = decodeRect1();
80         g.setPaint(decodeGradient2(rect));
81         g.fill(rect);
82
83     }
84
85
86
87     private RoundRectangle2D decodeRoundRect1() {
88         roundRect.setRoundRect(decodeX(0.4f), //x
89
decodeY(0.0f), //y
90
decodeX(2.8f) - decodeX(0.4f), //width
91
decodeY(2.6f) - decodeY(0.0f), //height
92
4.8333335f, 4.8333335f); //rounding
93
return roundRect;
94     }
95
96     private RoundRectangle2D decodeRoundRect2() {
97         roundRect.setRoundRect(decodeX(0.6f), //x
98
decodeY(0.2f), //y
99
decodeX(2.8f) - decodeX(0.6f), //width
100
decodeY(2.4f) - decodeY(0.2f), //height
101
3.1f, 3.1f); //rounding
102
return roundRect;
103     }
104
105     private Rectangle2D decodeRect1() {
106             rect.setRect(decodeX(0.8f), //x
107
decodeY(0.4f), //y
108
decodeX(2.4f) - decodeX(0.8f), //width
109
decodeY(2.2f) - decodeY(0.4f)); //height
110
return rect;
111     }
112
113
114
115     private Paint decodeGradient1(Shape s) {
116         Rectangle2D bounds = s.getBounds2D();
117         float x = (float)bounds.getX();
118         float y = (float)bounds.getY();
119         float w = (float)bounds.getWidth();
120         float h = (float)bounds.getHeight();
121         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
122                 new float[] { 0.0f,0.5f,1.0f },
123                 new Color[] { color2,
124                             decodeColor(color2,color3,0.5f),
125                             color3});
126     }
127
128     private Paint decodeGradient2(Shape s) {
129         Rectangle2D bounds = s.getBounds2D();
130         float x = (float)bounds.getX();
131         float y = (float)bounds.getY();
132         float w = (float)bounds.getWidth();
133         float h = (float)bounds.getHeight();
134         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
135                 new float[] { 0.0f,0.24f,1.0f },
136                 new Color[] { color4,
137                             decodeColor(color4,color5,0.5f),
138                             color5});
139     }
140
141
142 }
143
Popular Tags