KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * MenuPainter.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 MenuPainter 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 MenuPainter 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     static final int BACKGROUND_ENABLED_SELECTED = 3;
25     static final int ARROWICON_DISABLED = 4;
26     static final int ARROWICON_ENABLED = 5;
27     static final int ARROWICON_ENABLED_SELECTED = 6;
28
29
30     private int state; //refers to one of the static final ints above
31
private PaintContext ctx;
32
33     //the following 4 variables are reused during the painting code of the layers
34
private Path2D path = new Path2D.Float();
35     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
36     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
37     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
38
39     //All Colors used for painting are stored here. Ideally, only those colors being used
40
//by a particular instance of MenuPainter would be created. For the moment at least,
41
//however, all are created for each instance.
42
private Color color1 = decodeColor("nimbusSelection", 0.0f, 0.0f, 0.0f, 0);
43     private Color color2 = decodeColor("nimbusBlueGrey", 0.0f, -0.08983666f, -0.17647058f, 0);
44     private Color color3 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.09663743f, -0.4627451f, 0);
45     private Color color4 = new Color(255, 255, 255, 255);
46
47
48     //Array of current component colors, updated in each paint call
49
private Object JavaDoc[] componentColors;
50
51     public MenuPainter(PaintContext ctx, int state) {
52         super();
53         this.state = state;
54         this.ctx = ctx;
55     }
56
57     @Override JavaDoc
58     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
59         //populate componentColors array with colors calculated in getExtendedCacheKeys call
60
componentColors = extendedCacheKeys;
61         //generate this entire method. Each state/bg/fg/border combo that has
62
//been painted gets its own KEY and paint method.
63
switch(state) {
64             case BACKGROUND_ENABLED_SELECTED: paintBackgroundEnabledAndSelected(g); break;
65             case ARROWICON_DISABLED: paintarrowIconDisabled(g); break;
66             case ARROWICON_ENABLED: paintarrowIconEnabled(g); break;
67             case ARROWICON_ENABLED_SELECTED: paintarrowIconEnabledAndSelected(g); break;
68
69         }
70     }
71         
72
73
74     @Override JavaDoc
75     protected final PaintContext getPaintContext() {
76         return ctx;
77     }
78
79     private void paintBackgroundEnabledAndSelected(Graphics2D g) {
80         rect = decodeRect1();
81         g.setPaint(color1);
82         g.fill(rect);
83
84     }
85
86     private void paintarrowIconDisabled(Graphics2D g) {
87         path = decodePath1();
88         g.setPaint(color2);
89         g.fill(path);
90
91     }
92
93     private void paintarrowIconEnabled(Graphics2D g) {
94         path = decodePath1();
95         g.setPaint(color3);
96         g.fill(path);
97
98     }
99
100     private void paintarrowIconEnabledAndSelected(Graphics2D g) {
101         path = decodePath2();
102         g.setPaint(color4);
103         g.fill(path);
104
105     }
106
107
108
109     private Rectangle2D decodeRect1() {
110             rect.setRect(decodeX(1.0f), //x
111
decodeY(1.0f), //y
112
decodeX(2.0f) - decodeX(1.0f), //width
113
decodeY(2.0f) - decodeY(1.0f)); //height
114
return rect;
115     }
116
117     private Path2D decodePath1() {
118         path.reset();
119         path.moveTo(decodeX(0.0f), decodeY(0.2f));
120         path.lineTo(decodeX(2.7512195f), decodeY(2.102439f));
121         path.lineTo(decodeX(0.0f), decodeY(3.0f));
122         path.lineTo(decodeX(0.0f), decodeY(0.2f));
123         path.closePath();
124         return path;
125     }
126
127     private Path2D decodePath2() {
128         path.reset();
129         path.moveTo(decodeX(0.0f), decodeY(1.0f));
130         path.lineTo(decodeX(1.9529617f), decodeY(1.5625f));
131         path.lineTo(decodeX(0.0f), decodeY(3.0f));
132         path.lineTo(decodeX(0.0f), decodeY(1.0f));
133         path.closePath();
134         return path;
135     }
136
137
138
139
140 }
141
Popular Tags