KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > ImageIcon


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: ImageIcon.java,v $
11    Revision 1.10 2004/05/04 10:06:52 bobintetley
12    Fixes to byte[] pixel data - added getModifiers() to ActionEvent
13
14    Revision 1.9 2004/04/16 10:19:06 dannaab
15    Misc bug fixes, InputMap implementation, preliminary undo support
16
17    Revision 1.8 2004/03/01 15:58:47 bobintetley
18    Various little bug fixes
19
20    Revision 1.7 2003/12/14 09:13:38 bobintetley
21    Added CVS log to source headers
22
23 */

24
25
26 package swingwtx.swing;
27
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.net.URL JavaDoc;
32
33 import swingwt.awt.Component;
34 import swingwt.awt.Graphics;
35
36
37 public class ImageIcon implements Icon {
38     
39     private String JavaDoc description = null;
40     private swingwt.awt.Image image = null;
41     
42     /** Creates an empty imageicon */
43     public ImageIcon() {
44     }
45     
46     public ImageIcon(swingwt.awt.Image image) {
47         this.image = image;
48     }
49     
50     public ImageIcon(swingwt.awt.Image image, String JavaDoc description) {
51         this.image = image;
52         setDescription(description);
53     }
54     
55     public ImageIcon(String JavaDoc filename) {
56         this(filename, "");
57     }
58     
59     public ImageIcon(String JavaDoc filename, String JavaDoc description) {
60         try {
61             image = new swingwt.awt.Image();
62             image.image = new org.eclipse.swt.graphics.Image(SwingWTUtils.getDisplay(),
63                 new FileInputStream JavaDoc(new File JavaDoc(filename)));
64             setDescription(description);
65         } catch (Exception JavaDoc e) { e.printStackTrace(); }
66     }
67     
68     public ImageIcon(URL JavaDoc location) {
69         try {
70             image = new swingwt.awt.Image();
71             image.image = new org.eclipse.swt.graphics.Image(SwingWTUtils.getDisplay(), location.openStream());
72         } catch (Exception JavaDoc e) { e.printStackTrace(); }
73     }
74     
75     public ImageIcon(URL JavaDoc location, String JavaDoc description) {
76         this(location);
77         setDescription(description);
78     }
79
80     public ImageIcon(byte[] data) {
81     this(data, "");
82     }
83      
84     public ImageIcon(byte[] data, String JavaDoc description) {
85         try {
86             image = new swingwt.awt.Image();
87             image.image = new org.eclipse.swt.graphics.Image(SwingWTUtils.getDisplay(),
88                 new ByteArrayInputStream JavaDoc(data));
89             setDescription(description);
90         }
91     catch (Exception JavaDoc e) {
92          e.printStackTrace();
93     }
94     }
95                 
96     
97     /** Getter for property description.
98      * @return Value of property description.
99      *
100      */

101     public java.lang.String JavaDoc getDescription() {
102         return description;
103     }
104     
105     /** Setter for property description.
106      * @param description New value of property description.
107      *
108      */

109     public void setDescription(java.lang.String JavaDoc description) {
110         this.description = description;
111     }
112     
113     /** Getter for property image.
114      * @return Value of property image.
115      *
116      */

117     public swingwt.awt.Image getImage() {
118         return image;
119     }
120         
121     /** Setter for property image.
122      * @param image New value of property image.
123      *
124      */

125     public void setImage(swingwt.awt.Image image) {
126         this.image = image;
127     }
128
129     public int getIconHeight() {
130         if (image == null) return 0;
131         if (image.image == null) return 0;
132         return image.image.getBounds().height;
133     }
134     
135     public int getIconWidth() {
136         if (image == null) return 0;
137         if (image.image == null) return 0;
138         return image.image.getBounds().width;
139     }
140     
141     public void paintIcon(Component c, Graphics g, int x, int y) {
142         if (image != null)
143             g.drawImage(image, x, y, null);
144     }
145     
146 }
147
Popular Tags