KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicIconFactory


1 /*
2  * @(#)BasicIconFactory.java 1.27 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.basic;
9
10 import javax.swing.*;
11 import javax.swing.plaf.UIResource JavaDoc;
12
13 import java.awt.Graphics JavaDoc;
14 import java.awt.Color JavaDoc;
15 import java.awt.Component JavaDoc;
16 import java.awt.Polygon JavaDoc;
17 import java.io.Serializable JavaDoc;
18
19 /**
20  * Factory object that can vend Icons appropriate for the basic L & F.
21  * <p>
22  * <strong>Warning:</strong>
23  * Serialized objects of this class will not be compatible with
24  * future Swing releases. The current serialization support is
25  * appropriate for short term storage or RMI between applications running
26  * the same version of Swing. As of 1.4, support for long term storage
27  * of all JavaBeans<sup><font size="-2">TM</font></sup>
28  * has been added to the <code>java.beans</code> package.
29  * Please see {@link java.beans.XMLEncoder}.
30  *
31  * @version 1.27 12/19/03
32  * @author David Kloba
33  * @author Georges Saab
34  */

35 public class BasicIconFactory implements Serializable JavaDoc
36 {
37     private static Icon frame_icon;
38     private static Icon checkBoxIcon;
39     private static Icon radioButtonIcon;
40     private static Icon checkBoxMenuItemIcon;
41     private static Icon radioButtonMenuItemIcon;
42     private static Icon menuItemCheckIcon;
43     private static Icon menuItemArrowIcon;
44     private static Icon menuArrowIcon;
45
46     public static Icon getMenuItemCheckIcon() {
47     if (menuItemCheckIcon == null) {
48         menuItemCheckIcon = new MenuItemCheckIcon();
49     }
50     return menuItemCheckIcon;
51     }
52
53     public static Icon getMenuItemArrowIcon() {
54     if (menuItemArrowIcon == null) {
55         menuItemArrowIcon = new MenuItemArrowIcon();
56     }
57     return menuItemArrowIcon;
58     }
59
60     public static Icon getMenuArrowIcon() {
61     if (menuArrowIcon == null) {
62         menuArrowIcon = new MenuArrowIcon();
63     }
64     return menuArrowIcon;
65     }
66
67     public static Icon getCheckBoxIcon() {
68     if (checkBoxIcon == null) {
69         checkBoxIcon = new CheckBoxIcon();
70     }
71     return checkBoxIcon;
72     }
73
74     public static Icon getRadioButtonIcon() {
75     if (radioButtonIcon == null) {
76         radioButtonIcon = new RadioButtonIcon();
77     }
78     return radioButtonIcon;
79     }
80
81     public static Icon getCheckBoxMenuItemIcon() {
82     if (checkBoxMenuItemIcon == null) {
83         checkBoxMenuItemIcon = new CheckBoxMenuItemIcon();
84     }
85     return checkBoxMenuItemIcon;
86     }
87
88     public static Icon getRadioButtonMenuItemIcon() {
89     if (radioButtonMenuItemIcon == null) {
90         radioButtonMenuItemIcon = new RadioButtonMenuItemIcon();
91     }
92     return radioButtonMenuItemIcon;
93     }
94
95     public static Icon createEmptyFrameIcon() {
96     if(frame_icon == null)
97         frame_icon = new EmptyFrameIcon();
98     return frame_icon;
99     }
100
101     private static class EmptyFrameIcon implements Icon, Serializable JavaDoc {
102         int height = 16;
103         int width = 14;
104         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
105         }
106         public int getIconWidth() { return width; }
107         public int getIconHeight() { return height; }
108     };
109
110     private static class CheckBoxIcon implements Icon, Serializable JavaDoc
111     {
112     final static int csize = 13;
113     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
114     }
115
116     public int getIconWidth() {
117         return csize;
118     }
119         
120     public int getIconHeight() {
121         return csize;
122     }
123     }
124
125     private static class RadioButtonIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc
126     {
127     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
128     }
129
130     public int getIconWidth() {
131         return 13;
132     }
133         
134     public int getIconHeight() {
135         return 13;
136     }
137     } // end class RadioButtonIcon
138

139
140     private static class CheckBoxMenuItemIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc
141     {
142     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
143         AbstractButton b = (AbstractButton) c;
144         ButtonModel model = b.getModel();
145         boolean isSelected = model.isSelected();
146         if (isSelected) {
147                 g.drawLine(x+7, y+1, x+7, y+3);
148                 g.drawLine(x+6, y+2, x+6, y+4);
149                 g.drawLine(x+5, y+3, x+5, y+5);
150                 g.drawLine(x+4, y+4, x+4, y+6);
151                 g.drawLine(x+3, y+5, x+3, y+7);
152                 g.drawLine(x+2, y+4, x+2, y+6);
153                 g.drawLine(x+1, y+3, x+1, y+5);
154         }
155     }
156     public int getIconWidth() { return 9; }
157     public int getIconHeight() { return 9; }
158
159     } // End class CheckBoxMenuItemIcon
160

161     
162     private static class RadioButtonMenuItemIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc
163     {
164     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
165         AbstractButton b = (AbstractButton) c;
166         ButtonModel model = b.getModel();
167         if (b.isSelected() == true) {
168         g.fillOval(x+1, y+1, getIconWidth(), getIconHeight());
169         }
170     }
171         public int getIconWidth() { return 6; }
172     public int getIconHeight() { return 6; }
173
174     } // End class RadioButtonMenuItemIcon
175

176
177     private static class MenuItemCheckIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc{
178     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
179     }
180     public int getIconWidth() { return 9; }
181     public int getIconHeight() { return 9; }
182
183     } // End class MenuItemCheckIcon
184

185     private static class MenuItemArrowIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc {
186     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
187     }
188     public int getIconWidth() { return 4; }
189     public int getIconHeight() { return 8; }
190
191     } // End class MenuItemArrowIcon
192

193     private static class MenuArrowIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc {
194     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
195         Polygon JavaDoc p = new Polygon JavaDoc();
196         p.addPoint(x, y);
197         p.addPoint(x+getIconWidth(), y+getIconHeight()/2);
198         p.addPoint(x, y+getIconHeight());
199         g.fillPolygon(p);
200
201     }
202     public int getIconWidth() { return 4; }
203     public int getIconHeight() { return 8; }
204     } // End class MenuArrowIcon
205
}
206
207
Popular Tags