KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RadioButtonMenuItemPainter.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 RadioButtonMenuItemPainter 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 RadioButtonMenuItemPainter 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_MOUSEOVER = 3;
25     static final int BACKGROUND_SELECTED_MOUSEOVER = 4;
26     static final int CHECKICON_DISABLED_SELECTED = 5;
27     static final int CHECKICON_ENABLED_SELECTED = 6;
28     static final int CHECKICON_SELECTED_MOUSEOVER = 7;
29     static final int ICON_DISABLED = 8;
30     static final int ICON_ENABLED = 9;
31     static final int ICON_MOUSEOVER = 10;
32
33
34     private int state; //refers to one of the static final ints above
35
private PaintContext ctx;
36
37     //the following 4 variables are reused during the painting code of the layers
38
private Path2D path = new Path2D.Float();
39     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
40     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
41     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
42
43     //All Colors used for painting are stored here. Ideally, only those colors being used
44
//by a particular instance of RadioButtonMenuItemPainter would be created. For the moment at least,
45
//however, all are created for each instance.
46
private Color color1 = decodeColor("nimbusSelection", 0.0f, 0.0f, 0.0f, 0);
47     private Color color2 = decodeColor("nimbusBlueGrey", 0.0f, -0.08983666f, -0.17647058f, 0);
48     private Color color3 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.09663743f, -0.4627451f, 0);
49     private Color color4 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, 0);
50
51
52     //Array of current component colors, updated in each paint call
53
private Object JavaDoc[] componentColors;
54
55     public RadioButtonMenuItemPainter(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_MOUSEOVER: paintBackgroundMouseOver(g); break;
69             case BACKGROUND_SELECTED_MOUSEOVER: paintBackgroundSelectedAndMouseOver(g); break;
70             case CHECKICON_DISABLED_SELECTED: paintcheckIconDisabledAndSelected(g); break;
71             case CHECKICON_ENABLED_SELECTED: paintcheckIconEnabledAndSelected(g); break;
72             case CHECKICON_SELECTED_MOUSEOVER: paintcheckIconSelectedAndMouseOver(g); break;
73
74         }
75     }
76         
77
78
79     @Override JavaDoc
80     protected final PaintContext getPaintContext() {
81         return ctx;
82     }
83
84     private void paintBackgroundMouseOver(Graphics2D g) {
85         rect = decodeRect1();
86         g.setPaint(color1);
87         g.fill(rect);
88
89     }
90
91     private void paintBackgroundSelectedAndMouseOver(Graphics2D g) {
92         rect = decodeRect1();
93         g.setPaint(color1);
94         g.fill(rect);
95
96     }
97
98     private void paintcheckIconDisabledAndSelected(Graphics2D g) {
99         path = decodePath1();
100         g.setPaint(color2);
101         g.fill(path);
102
103     }
104
105     private void paintcheckIconEnabledAndSelected(Graphics2D g) {
106         path = decodePath2();
107         g.setPaint(color3);
108         g.fill(path);
109
110     }
111
112     private void paintcheckIconSelectedAndMouseOver(Graphics2D g) {
113         path = decodePath2();
114         g.setPaint(color4);
115         g.fill(path);
116
117     }
118
119
120
121     private Rectangle2D decodeRect1() {
122             rect.setRect(decodeX(1.0f), //x
123
decodeY(1.0f), //y
124
decodeX(2.0f) - decodeX(1.0f), //width
125
decodeY(2.0f) - decodeY(1.0f)); //height
126
return rect;
127     }
128
129     private Path2D decodePath1() {
130         path.reset();
131         path.moveTo(decodeX(0.0f), decodeY(2.097561f));
132         path.lineTo(decodeX(0.90975606f), decodeY(0.20243903f));
133         path.lineTo(decodeX(3.0f), decodeY(2.102439f));
134         path.lineTo(decodeX(0.90731704f), decodeY(3.0f));
135         path.lineTo(decodeX(0.0f), decodeY(2.097561f));
136         path.closePath();
137         return path;
138     }
139
140     private Path2D decodePath2() {
141         path.reset();
142         path.moveTo(decodeX(0.0024390244f), decodeY(2.097561f));
143         path.lineTo(decodeX(0.90975606f), decodeY(0.20243903f));
144         path.lineTo(decodeX(3.0f), decodeY(2.102439f));
145         path.lineTo(decodeX(0.90731704f), decodeY(3.0f));
146         path.lineTo(decodeX(0.0024390244f), decodeY(2.097561f));
147         path.closePath();
148         return path;
149     }
150
151
152
153
154 }
155
Popular Tags