KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * PopupMenuPainter.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 PopupMenuPainter 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 PopupMenuPainter 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
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 PopupMenuPainter would be created. For the moment at least,
37
//however, all are created for each instance.
38
private Color color1 = decodeColor("nimbusBlueGrey", -0.6111111f, -0.110526316f, -0.39607844f, 0);
39     private Color color2 = decodeColor("nimbusBase", 0.0f, -0.6357143f, 0.45098037f, 0);
40     private Color color3 = decodeColor("nimbusBase", 0.021348298f, -0.6150531f, 0.39999998f, 0);
41
42
43     //Array of current component colors, updated in each paint call
44
private Object JavaDoc[] componentColors;
45
46     public PopupMenuPainter(PaintContext ctx, int state) {
47         super();
48         this.state = state;
49         this.ctx = ctx;
50     }
51
52     @Override JavaDoc
53     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
54         //populate componentColors array with colors calculated in getExtendedCacheKeys call
55
componentColors = extendedCacheKeys;
56         //generate this entire method. Each state/bg/fg/border combo that has
57
//been painted gets its own KEY and paint method.
58
switch(state) {
59             case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
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 paintBackgroundDisabled(Graphics2D g) {
73         rect = decodeRect1();
74         g.setPaint(color1);
75         g.fill(rect);
76         rect = decodeRect2();
77         g.setPaint(decodeGradient1(rect));
78         g.fill(rect);
79
80     }
81
82     private void paintBackgroundEnabled(Graphics2D g) {
83         rect = decodeRect3();
84         g.setPaint(color1);
85         g.fill(rect);
86         rect = decodeRect4();
87         g.setPaint(decodeGradient1(rect));
88         g.fill(rect);
89
90     }
91
92
93
94     private Rectangle2D decodeRect1() {
95             rect.setRect(decodeX(1.0f), //x
96
decodeY(0.0f), //y
97
decodeX(2.0f) - decodeX(1.0f), //width
98
decodeY(3.0f) - decodeY(0.0f)); //height
99
return rect;
100     }
101
102     private Rectangle2D decodeRect2() {
103             rect.setRect(decodeX(1.0045455f), //x
104
decodeY(0.11111111f), //y
105
decodeX(1.9954545f) - decodeX(1.0045455f), //width
106
decodeY(2.909091f) - decodeY(0.11111111f)); //height
107
return rect;
108     }
109
110     private Rectangle2D decodeRect3() {
111             rect.setRect(decodeX(0.0f), //x
112
decodeY(0.0f), //y
113
decodeX(3.0f) - decodeX(0.0f), //width
114
decodeY(3.0f) - decodeY(0.0f)); //height
115
return rect;
116     }
117
118     private Rectangle2D decodeRect4() {
119             rect.setRect(decodeX(0.5f), //x
120
decodeY(0.09090909f), //y
121
decodeX(2.5f) - decodeX(0.5f), //width
122
decodeY(2.909091f) - decodeY(0.09090909f)); //height
123
return rect;
124     }
125
126
127
128     private Paint decodeGradient1(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.0030f,0.02f,0.5f,0.98f,0.996f,1.0f },
136                 new Color[] { color2,
137                             decodeColor(color2,color3,0.5f),
138                             color3,
139                             decodeColor(color3,color3,0.5f),
140                             color3,
141                             decodeColor(color3,color2,0.5f),
142                             color2});
143     }
144
145
146 }
147
Popular Tags