KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ToolBarPainter.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 ToolBarPainter 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 ToolBarPainter to determine which region/state is being painted
21
//by that instance.
22
static final int BORDER_ENABLED = 1;
23     static final int HANDLEICON_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 ToolBarPainter would be created. For the moment at least,
37
//however, all are created for each instance.
38
private Color color1 = decodeColor("nimbusBorder", 0.0f, 0.0f, 0.0f, 0);
39     private Color color2 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, 0);
40     private Color color3 = decodeColor("nimbusBlueGrey", -0.006944418f, -0.07399663f, 0.11372548f, 0);
41     private Color color4 = decodeColor("nimbusBorder", 0.0f, -0.029675633f, 0.109803915f, 0);
42     private Color color5 = decodeColor("nimbusBlueGrey", -0.008547008f, -0.03494492f, -0.07058823f, 0);
43
44
45     //Array of current component colors, updated in each paint call
46
private Object JavaDoc[] componentColors;
47
48     public ToolBarPainter(PaintContext ctx, int state) {
49         super();
50         this.state = state;
51         this.ctx = ctx;
52     }
53
54     @Override JavaDoc
55     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
56         //populate componentColors array with colors calculated in getExtendedCacheKeys call
57
componentColors = extendedCacheKeys;
58         //generate this entire method. Each state/bg/fg/border combo that has
59
//been painted gets its own KEY and paint method.
60
switch(state) {
61             case BORDER_ENABLED: paintBorderEnabled(g); break;
62             case HANDLEICON_ENABLED: painthandleIconEnabled(g); break;
63
64         }
65     }
66         
67
68
69     @Override JavaDoc
70     protected final PaintContext getPaintContext() {
71         return ctx;
72     }
73
74     private void paintBorderEnabled(Graphics2D g) {
75         rect = decodeRect1();
76         g.setPaint(color1);
77         g.fill(rect);
78
79     }
80
81     private void painthandleIconEnabled(Graphics2D g) {
82         rect = decodeRect2();
83         g.setPaint(decodeGradient1(rect));
84         g.fill(rect);
85         rect = decodeRect3();
86         g.setPaint(color4);
87         g.fill(rect);
88         path = decodePath1();
89         g.setPaint(color5);
90         g.fill(path);
91         path = decodePath2();
92         g.setPaint(color5);
93         g.fill(path);
94
95     }
96
97
98
99     private Rectangle2D decodeRect1() {
100             rect.setRect(decodeX(1.0f), //x
101
decodeY(2.0f), //y
102
decodeX(2.0f) - decodeX(1.0f), //width
103
decodeY(3.0f) - decodeY(2.0f)); //height
104
return rect;
105     }
106
107     private Rectangle2D decodeRect2() {
108             rect.setRect(decodeX(0.0f), //x
109
decodeY(0.0f), //y
110
decodeX(2.8f) - decodeX(0.0f), //width
111
decodeY(3.0f) - decodeY(0.0f)); //height
112
return rect;
113     }
114
115     private Rectangle2D decodeRect3() {
116             rect.setRect(decodeX(2.8f), //x
117
decodeY(0.0f), //y
118
decodeX(3.0f) - decodeX(2.8f), //width
119
decodeY(3.0f) - decodeY(0.0f)); //height
120
return rect;
121     }
122
123     private Path2D decodePath1() {
124         path.reset();
125         path.moveTo(decodeX(0.0f), decodeY(0.0f));
126         path.lineTo(decodeX(0.0f), decodeY(0.4f));
127         path.lineTo(decodeX(0.4f), decodeY(0.0f));
128         path.lineTo(decodeX(0.0f), decodeY(0.0f));
129         path.closePath();
130         return path;
131     }
132
133     private Path2D decodePath2() {
134         path.reset();
135         path.moveTo(decodeX(0.0f), decodeY(3.0f));
136         path.lineTo(decodeX(0.0f), decodeY(2.6f));
137         path.lineTo(decodeX(0.4f), decodeY(3.0f));
138         path.lineTo(decodeX(0.0f), decodeY(3.0f));
139         path.closePath();
140         return path;
141     }
142
143
144
145     private Paint decodeGradient1(Shape s) {
146         Rectangle2D bounds = s.getBounds2D();
147         float x = (float)bounds.getX();
148         float y = (float)bounds.getY();
149         float w = (float)bounds.getWidth();
150         float h = (float)bounds.getHeight();
151         return decodeGradient((0.0f * w) + x, (0.5f * h) + y, (1.0f * w) + x, (0.5f * h) + y,
152                 new float[] { 0.0f,0.5f,1.0f },
153                 new Color[] { color2,
154                             decodeColor(color2,color3,0.5f),
155                             color3});
156     }
157
158
159 }
160
Popular Tags