KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * MenuBarPainter.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 MenuBarPainter 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 MenuBarPainter to determine which region/state is being painted
21
//by that instance.
22
static final int BACKGROUND_ENABLED = 1;
23     static final int BORDER_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 MenuBarPainter would be created. For the moment at least,
37
//however, all are created for each instance.
38
private Color color1 = decodeColor("nimbusBlueGrey", 0.0f, -0.07016757f, 0.12941176f, 0);
39     private Color color2 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.10255819f, 0.23921567f, 0);
40     private Color color3 = decodeColor("nimbusBlueGrey", -0.111111104f, -0.10654225f, 0.23921567f, -29);
41     private Color color4 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, -255);
42     private Color color5 = decodeColor("nimbusBorder", 0.0f, 0.0f, 0.0f, 0);
43
44
45     //Array of current component colors, updated in each paint call
46
private Object JavaDoc[] componentColors;
47
48     public MenuBarPainter(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 BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
62             case BORDER_ENABLED: paintBorderEnabled(g); break;
63
64         }
65     }
66         
67
68
69     @Override JavaDoc
70     protected final PaintContext getPaintContext() {
71         return ctx;
72     }
73
74     private void paintBackgroundEnabled(Graphics2D g) {
75         rect = decodeRect1();
76         g.setPaint(color1);
77         g.fill(rect);
78         rect = decodeRect2();
79         g.setPaint(decodeGradient1(rect));
80         g.fill(rect);
81
82     }
83
84     private void paintBorderEnabled(Graphics2D g) {
85         rect = decodeRect3();
86         g.setPaint(color5);
87         g.fill(rect);
88
89     }
90
91
92
93     private Rectangle2D decodeRect1() {
94             rect.setRect(decodeX(1.0f), //x
95
decodeY(0.0f), //y
96
decodeX(2.0f) - decodeX(1.0f), //width
97
decodeY(1.9523809f) - decodeY(0.0f)); //height
98
return rect;
99     }
100
101     private Rectangle2D decodeRect2() {
102             rect.setRect(decodeX(1.0f), //x
103
decodeY(0.0f), //y
104
decodeX(2.0f) - decodeX(1.0f), //width
105
decodeY(2.0f) - decodeY(0.0f)); //height
106
return rect;
107     }
108
109     private Rectangle2D decodeRect3() {
110             rect.setRect(decodeX(1.0f), //x
111
decodeY(2.0f), //y
112
decodeX(2.0f) - decodeX(1.0f), //width
113
decodeY(3.0f) - decodeY(2.0f)); //height
114
return rect;
115     }
116
117
118
119     private Paint decodeGradient1(Shape s) {
120         Rectangle2D bounds = s.getBounds2D();
121         float x = (float)bounds.getX();
122         float y = (float)bounds.getY();
123         float w = (float)bounds.getWidth();
124         float h = (float)bounds.getHeight();
125         return decodeGradient((1.0f * w) + x, (0.0f * h) + y, (1.0f * w) + x, (1.0f * h) + y,
126                 new float[] { 0.0f,0.015f,0.03f,0.23354445f,0.7569444f },
127                 new Color[] { color2,
128                             decodeColor(color2,color3,0.5f),
129                             color3,
130                             decodeColor(color3,color4,0.5f),
131                             color4});
132     }
133
134
135 }
136
Popular Tags