KickJava   Java API By Example, From Geeks To Geeks.

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


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