KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > FileIcon


1 package snow.utils.gui;
2
3 import javax.swing.border.EmptyBorder JavaDoc;
4 import javax.swing.*;
5 import java.awt.*;
6
7 /** Either file or directory shape, with specified color and optional letter
8 */

9 public class FileIcon implements Icon
10 {
11   public enum IconColor { Red, Green, RedGreen, White, System }
12
13   private IconColor type;
14   private int size;
15
16   // the file or directory outer shape
17
Polygon poly;
18
19   private String JavaDoc letter = null;
20   private Font letterFont;
21
22
23   public FileIcon(boolean directory, int size)
24   {
25     this(directory, IconColor.White, size);
26   }
27
28   public FileIcon(boolean directory, IconColor type, int size)
29   {
30     this.type = type;
31     this.size = size;
32
33     if(directory)
34     {
35       initShapeDirectory();
36     }
37     else
38     {
39       initShapeFile();
40     }
41     letterFont = new Font("Dialog", Font.PLAIN, size*5/8);
42   }
43
44   public void setType(IconColor type)
45   {
46     this.type = type;
47   }
48
49   public void setLetter(String JavaDoc letter)
50   {
51     this.letter = letter;
52   }
53
54   boolean delete;
55   public void setRemovedFromProject(boolean delete)
56   {
57     this.delete = delete;
58   }
59
60   private void initShapeFile()
61   {
62     poly = new Polygon();
63     poly.addPoint(size/8, 0);
64     poly.addPoint(size*5/8, 0);
65     poly.addPoint(size*7/8, 2*size/8);
66     poly.addPoint(size*7/8, size);
67     poly.addPoint(size/8, size);
68   }
69
70   private void initShapeDirectory()
71   {
72     poly = new Polygon();
73     poly.addPoint(0, size/6);
74     poly.addPoint((int)(size*0.5), size/6);
75     poly.addPoint((int)(size*0.65), size/11);
76     poly.addPoint((int)(size*0.85), size/11);
77     poly.addPoint(size, size/6);
78     poly.addPoint(size, size*7/8);
79     poly.addPoint(0, size*7/8);
80   }
81
82   public int getIconHeight() {
83     return size;
84   }
85
86   public int getIconWidth() {
87     return size;
88   }
89
90   public void paintIcon( Component c, Graphics g, int x, int y )
91   {
92     Graphics2D g2 = (Graphics2D) g;
93
94     g.translate(x, y);
95
96     Color color1 = Color.white;
97     Color color2 = Color.black;
98
99     if (type==IconColor.RedGreen)
100     {
101       color1 = new Color(220,0,0);
102       color2 = new Color(0,220,0);
103     }
104     else if (type==IconColor.Red)
105     {
106       color1 = new Color(120,80,80);
107       color2 = new Color(220,0,0);
108     }
109     else if (type==IconColor.Green)
110     {
111       color1 = new Color(80,120,80);
112       color2 = new Color(0,220,0);
113     }
114     else if (type==IconColor.White)
115     {
116       color1 = Color.white;
117       color2 = Color.white;
118     }
119     else if (type==IconColor.System)
120     {
121 //blue color1 = new Color(0,80,80);
122
// color2 = new Color(0,220,220);
123
color1 = new Color(180,180,10);
124       color2 = new Color(220,220,10);
125
126     }
127
128     g2.setPaint(new GradientPaint(0,0,color1,size,size,color2,false));
129     g.fillPolygon(poly);
130
131     // for mixed folders
132
if (type==IconColor.RedGreen)
133     {
134     }
135
136     if(letter!=null)
137     {
138       Font oldFont = g2.getFont();
139
140       g2.setColor(Color.black);
141       g2.setFont(letterFont);
142       g2.drawString(letter, size/4+1, size*6/8); // May2007: shift 1
143

144       g2.setFont(oldFont);
145     }
146
147     if(delete)
148     {
149      g.setColor(Color.red);
150
151      g.drawLine(0,0,size,size);
152      g.drawLine(1,0,size,size-1);
153      g.drawLine(0,size,size,0);
154      g.drawLine(0,size-1,size-1,0);
155
156     }
157
158     // border
159
g.setColor(Color.black);
160     g.drawPolygon(poly);
161
162     g.translate(-x, -y);
163   }
164
165 /*test*/
166   public static void main(String JavaDoc[] a)
167   {
168     JFrame frame = new JFrame();
169     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
170
171     frame.setLocationRelativeTo(null);
172     FileIcon fic = new FileIcon(true, 16);
173     fic.setLetter("A");
174     fic.setType(IconColor.System);
175     JLabel lab = new JLabel(fic);
176     frame.getContentPane().add(lab);
177     lab.setBorder(new EmptyBorder JavaDoc(5,5,5,5));
178     frame.pack();
179     frame.setVisible(true);
180   }
181 }
182
Popular Tags