KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TabbedPaneTabbedPaneTabAreaPainter.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 TabbedPaneTabbedPaneTabAreaPainter 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 TabbedPaneTabbedPaneTabAreaPainter to determine which region/state is being painted
21
//by that instance.
22
static final int BACKGROUND_ENABLED = 1;
23     static final int BACKGROUND_DISABLED = 2;
24     static final int BACKGROUND_ENABLED_MOUSEOVER = 3;
25     static final int BACKGROUND_ENABLED_PRESSED = 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 TabbedPaneTabbedPaneTabAreaPainter would be created. For the moment at least,
39
//however, all are created for each instance.
40
private Color color1 = new Color(255, 200, 0, 255);
41     private Color color2 = decodeColor("nimbusBase", 0.08801502f, 0.3642857f, -0.4784314f, 0);
42     private Color color3 = decodeColor("nimbusBase", 5.1498413E-4f, -0.45471883f, 0.31764704f, 0);
43     private Color color4 = decodeColor("nimbusBase", 5.1498413E-4f, -0.4633005f, 0.3607843f, 0);
44     private Color color5 = decodeColor("nimbusBase", 0.05468172f, -0.58308274f, 0.19607842f, 0);
45     private Color color6 = decodeColor("nimbusBase", -0.57865167f, -0.6357143f, -0.54901963f, 0);
46     private Color color7 = decodeColor("nimbusBase", 5.1498413E-4f, -0.4690476f, 0.39215684f, 0);
47     private Color color8 = decodeColor("nimbusBase", 5.1498413E-4f, -0.47635174f, 0.4352941f, 0);
48     private Color color9 = decodeColor("nimbusBase", 0.0f, -0.05401492f, 0.05098039f, 0);
49     private Color color10 = decodeColor("nimbusBase", 0.0f, -0.09303135f, 0.09411764f, 0);
50
51
52     //Array of current component colors, updated in each paint call
53
private Object JavaDoc[] componentColors;
54
55     public TabbedPaneTabbedPaneTabAreaPainter(PaintContext ctx, int state) {
56         super();
57         this.state = state;
58         this.ctx = ctx;
59     }
60
61     @Override JavaDoc
62     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
63         //populate componentColors array with colors calculated in getExtendedCacheKeys call
64
componentColors = extendedCacheKeys;
65         //generate this entire method. Each state/bg/fg/border combo that has
66
//been painted gets its own KEY and paint method.
67
switch(state) {
68             case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
69             case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
70             case BACKGROUND_ENABLED_MOUSEOVER: paintBackgroundEnabledAndMouseOver(g); break;
71             case BACKGROUND_ENABLED_PRESSED: paintBackgroundEnabledAndPressed(g); break;
72
73         }
74     }
75         
76
77
78     @Override JavaDoc
79     protected final PaintContext getPaintContext() {
80         return ctx;
81     }
82
83     private void paintBackgroundEnabled(Graphics2D g) {
84         rect = decodeRect1();
85         g.setPaint(color1);
86         g.fill(rect);
87         rect = decodeRect2();
88         g.setPaint(decodeGradient1(rect));
89         g.fill(rect);
90
91     }
92
93     private void paintBackgroundDisabled(Graphics2D g) {
94         rect = decodeRect2();
95         g.setPaint(decodeGradient2(rect));
96         g.fill(rect);
97
98     }
99
100     private void paintBackgroundEnabledAndMouseOver(Graphics2D g) {
101         rect = decodeRect2();
102         g.setPaint(decodeGradient3(rect));
103         g.fill(rect);
104
105     }
106
107     private void paintBackgroundEnabledAndPressed(Graphics2D g) {
108         rect = decodeRect2();
109         g.setPaint(decodeGradient4(rect));
110         g.fill(rect);
111
112     }
113
114
115
116     private Rectangle2D decodeRect1() {
117             rect.setRect(decodeX(0.0f), //x
118
decodeY(1.0f), //y
119
decodeX(0.0f) - decodeX(0.0f), //width
120
decodeY(1.0f) - decodeY(1.0f)); //height
121
return rect;
122     }
123
124     private Rectangle2D decodeRect2() {
125             rect.setRect(decodeX(0.0f), //x
126
decodeY(2.1666667f), //y
127
decodeX(3.0f) - decodeX(0.0f), //width
128
decodeY(3.0f) - decodeY(2.1666667f)); //height
129
return rect;
130     }
131
132
133
134     private Paint decodeGradient1(Shape s) {
135         Rectangle2D bounds = s.getBounds2D();
136         float x = (float)bounds.getX();
137         float y = (float)bounds.getY();
138         float w = (float)bounds.getWidth();
139         float h = (float)bounds.getHeight();
140         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
141                 new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f },
142                 new Color[] { color2,
143                             decodeColor(color2,color3,0.5f),
144                             color3,
145                             decodeColor(color3,color4,0.5f),
146                             color4,
147                             decodeColor(color4,color2,0.5f),
148                             color2});
149     }
150
151     private Paint decodeGradient2(Shape s) {
152         Rectangle2D bounds = s.getBounds2D();
153         float x = (float)bounds.getX();
154         float y = (float)bounds.getY();
155         float w = (float)bounds.getWidth();
156         float h = (float)bounds.getHeight();
157         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
158                 new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f },
159                 new Color[] { color5,
160                             decodeColor(color5,color3,0.5f),
161                             color3,
162                             decodeColor(color3,color4,0.5f),
163                             color4,
164                             decodeColor(color4,color5,0.5f),
165                             color5});
166     }
167
168     private Paint decodeGradient3(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.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
175                 new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f },
176                 new Color[] { color6,
177                             decodeColor(color6,color7,0.5f),
178                             color7,
179                             decodeColor(color7,color8,0.5f),
180                             color8,
181                             decodeColor(color8,color2,0.5f),
182                             color2});
183     }
184
185     private Paint decodeGradient4(Shape s) {
186         Rectangle2D bounds = s.getBounds2D();
187         float x = (float)bounds.getX();
188         float y = (float)bounds.getY();
189         float w = (float)bounds.getWidth();
190         float h = (float)bounds.getHeight();
191         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
192                 new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f },
193                 new Color[] { color2,
194                             decodeColor(color2,color9,0.5f),
195                             color9,
196                             decodeColor(color9,color10,0.5f),
197                             color10,
198                             decodeColor(color10,color2,0.5f),
199                             color2});
200     }
201
202
203 }
204
Popular Tags