KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticXPIconFactory


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.plastic;
32
33 import java.awt.*;
34 import java.io.Serializable JavaDoc;
35
36 import javax.swing.AbstractButton JavaDoc;
37 import javax.swing.ButtonModel JavaDoc;
38 import javax.swing.Icon JavaDoc;
39 import javax.swing.JCheckBox JavaDoc;
40 import javax.swing.UIManager JavaDoc;
41 import javax.swing.plaf.UIResource JavaDoc;
42 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
43
44 import com.jgoodies.looks.LookUtils;
45
46 /**
47  * Factory class that vends <code>Icon</code>s for the JGoodies
48  * Plastic XP look&amp;feel.
49  * These icons are used extensively in PlasticXP via the defaults mechanism.
50  * While other look and feels often use GIFs for icons, creating icons
51  * in code facilitates switching to other themes.
52  * <p>
53  * Each method in this class returns either an <code>Icon</code> or
54  * <code>null</code>, where <code>null</code> implies that there is
55  * no default icon.
56  *
57  * @author Karsten Lentzsch
58  * @version $Revision: 1.2 $
59  */

60 public final class PlasticXPIconFactory {
61
62     private static CheckBoxIcon checkBoxIcon;
63     private static RadioButtonIcon radioButtonIcon;
64
65
66     /**
67      * Lazily creates and answers the check box icon.
68      *
69      * @return the check box icon
70      */

71     static Icon JavaDoc getCheckBoxIcon() {
72         if (checkBoxIcon == null) {
73             checkBoxIcon = new CheckBoxIcon();
74         }
75         return checkBoxIcon;
76     }
77
78     /**
79      * Lazily creates and answers the radio button icon.
80      *
81      * @return the check box icon
82      */

83     static Icon JavaDoc getRadioButtonIcon() {
84         if (radioButtonIcon == null) {
85             radioButtonIcon = new RadioButtonIcon();
86         }
87         return radioButtonIcon;
88     }
89
90
91     private static class CheckBoxIcon implements Icon JavaDoc, UIResource JavaDoc, Serializable JavaDoc {
92
93         private static final int SIZE = LookUtils.IS_LOW_RESOLUTION ? 13 : 15;
94
95         public int getIconWidth() { return SIZE; }
96         public int getIconHeight() { return SIZE; }
97
98         public void paintIcon(Component c, Graphics g, int x, int y) {
99             JCheckBox JavaDoc cb = (JCheckBox JavaDoc) c;
100             ButtonModel JavaDoc model = cb.getModel();
101             Graphics2D g2 = (Graphics2D) g;
102             Object JavaDoc hint = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
103             g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
104             
105             drawBorder(g2, model.isEnabled(), x, y, SIZE - 1, SIZE - 1);
106             drawFill(g2, model.isPressed(), x + 1, y + 1, SIZE - 2, SIZE - 2);
107             if (model.isEnabled() && (model.isArmed() && !(model.isPressed()))) {
108                 drawFocus(g2, x + 1, y + 1, SIZE - 3, SIZE - 3);
109             }
110             if (model.isSelected()) {
111                 drawCheck(g2, model.isEnabled(), x + 3, y + 3, SIZE - 7, SIZE - 7);
112             }
113             g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, hint);
114         }
115
116         private void drawBorder(Graphics2D g2, boolean enabled, int x, int y, int width, int height) {
117             g2.setColor(enabled
118                     ? PlasticLookAndFeel.getControlDarkShadow()
119                     : MetalLookAndFeel.getControlDisabled());
120             g2.drawRect(x, y, width, height);
121         }
122
123         private void drawCheck(Graphics2D g2, boolean enabled, int x, int y, int width, int height) {
124             g2.setColor(enabled
125                     ? UIManager.getColor("CheckBox.check")
126                     : MetalLookAndFeel.getControlDisabled());
127             int right = x + width;
128             int bottom = y + height;
129             int startY = y + height / 3;
130             int turnX = x + width / 2 - 2;
131             g2.drawLine(x , startY , turnX, bottom-3);
132             g2.drawLine(x , startY + 1, turnX, bottom-2);
133             g2.drawLine(x , startY + 2, turnX, bottom-1);
134             g2.drawLine(turnX + 1, bottom - 2, right, y );
135             g2.drawLine(turnX + 1, bottom - 1, right, y + 1 );
136             g2.drawLine(turnX + 1, bottom , right, y + 2 );
137         }
138
139         private void drawFill(Graphics2D g2, boolean pressed, int x, int y, int w, int h) {
140             Color upperLeft;
141             Color lowerRight;
142             if (pressed) {
143                 upperLeft = MetalLookAndFeel.getControlShadow();
144                 lowerRight = PlasticLookAndFeel.getControlHighlight();
145             } else {
146                 upperLeft = PlasticLookAndFeel.getControl();
147                 lowerRight = PlasticLookAndFeel.getControlHighlight().brighter();
148             }
149             g2.setPaint(new GradientPaint(x, y, upperLeft, x + w, y + h, lowerRight));
150             g2.fillRect(x, y, w, h);
151         }
152
153         private void drawFocus(Graphics2D g2, int x, int y, int width, int height) {
154             g2.setPaint(new GradientPaint(
155                     x,
156                     y,
157                     PlasticLookAndFeel.getFocusColor().brighter(),
158                     width,
159                     height,
160                     PlasticLookAndFeel.getFocusColor() /*.darker()*/
161             ));
162             g2.drawRect(x, y, width, height);
163             g2.drawRect(x + 1, y + 1, width - 2, height - 2);
164         }
165
166     }
167
168     // Paints the icon for a radio button.
169
private static class RadioButtonIcon implements Icon JavaDoc, UIResource JavaDoc, Serializable JavaDoc {
170
171         private static final int SIZE = LookUtils.IS_LOW_RESOLUTION ? 13 : 15;
172         
173         private static final Stroke FOCUS_STROKE = new BasicStroke(2);
174
175         public int getIconWidth() { return SIZE; }
176         public int getIconHeight() { return SIZE; }
177
178         public void paintIcon(Component c, Graphics g, int x, int y) {
179             Graphics2D g2 = (Graphics2D) g;
180             AbstractButton JavaDoc b = (AbstractButton JavaDoc) c;
181             ButtonModel JavaDoc model = b.getModel();
182         
183             Object JavaDoc hint = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
184             g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
185         
186             drawFill(g2, model.isPressed(), x, y, SIZE - 1, SIZE - 1);
187         
188             if (model.isArmed() && !(model.isPressed())) {
189                 drawFocus(g2, x + 1, y + 1, SIZE - 3, SIZE - 3);
190             }
191             if (model.isSelected()) {
192                 drawCheck(g2, c, model.isEnabled(), x + 4, y + 4, SIZE - 8, SIZE - 8);
193             }
194             drawBorder(g2, model.isEnabled(), x, y, SIZE-1, SIZE-1);
195             g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, hint);
196         }
197
198         private void drawBorder(Graphics2D g2, boolean enabled, int x, int y, int w, int h) {
199             g2.setColor(enabled
200                 ? PlasticLookAndFeel.getControlDarkShadow()
201                 : MetalLookAndFeel.getControlDisabled());
202             g2.drawOval(x, y, w, h);
203         }
204
205         private void drawCheck(Graphics2D g2, Component c, boolean enabled, int x, int y, int w, int h) {
206             g2.translate(x,y);
207             if (enabled) {
208                 g2.setColor(UIManager.getColor("RadioButton.check"));
209                 g2.fillOval(0,0,w,h);
210                 UIManager.getIcon("RadioButton.checkIcon").paintIcon(c, g2, 0,0);
211             } else {
212                 g2.setColor(MetalLookAndFeel.getControlDisabled());
213                 g2.fillOval(0,0,w,h);
214             }
215             g2.translate(-x, -y);
216         }
217
218         private void drawFill(Graphics2D g2, boolean pressed, int x, int y, int w, int h) {
219             Color upperLeft;
220             Color lowerRight;
221             if (pressed) {
222                 upperLeft = MetalLookAndFeel.getControlShadow();
223                 lowerRight = PlasticLookAndFeel.getControlHighlight();
224             } else {
225                 upperLeft = PlasticLookAndFeel.getControl();
226                 lowerRight = PlasticLookAndFeel.getControlHighlight().brighter();
227             }
228             g2.setPaint(new GradientPaint(x, y, upperLeft, x + w, y + h, lowerRight));
229             g2.fillOval(x, y, w, h);
230         }
231
232         private void drawFocus(Graphics2D g2, int x, int y, int w, int h) {
233             g2.setPaint(
234                 new GradientPaint(
235                     x,
236                     y,
237                     PlasticLookAndFeel.getFocusColor().brighter(),
238                     w,
239                     h,
240                     PlasticLookAndFeel.getFocusColor()));
241             Stroke stroke = g2.getStroke();
242             g2.setStroke(FOCUS_STROKE);
243             g2.drawOval(x, y, w, h);
244             g2.setStroke(stroke);
245         }
246
247     }
248
249 }
250
Popular Tags