1. /*
  2. * @(#)ImageIcon.java 1.34 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.*;
  9. import java.awt.image.*;
  10. import java.net.URL;
  11. import java.io.Serializable;
  12. import java.io.ObjectOutputStream;
  13. import java.io.ObjectInputStream;
  14. import java.io.IOException;
  15. /**
  16. * An implementation of the Icon interface that paints Icons
  17. * from Images. Images that are created from a URL or filename
  18. * are preloaded using MediaTracker to monitor the loaded state
  19. * of the image.
  20. * <p>
  21. * <strong>Warning:</strong>
  22. * Serialized objects of this class will not be compatible with
  23. * future Swing releases. The current serialization support is appropriate
  24. * for short term storage or RMI between applications running the same
  25. * version of Swing. A future release of Swing will provide support for
  26. * long term persistence.
  27. *
  28. * @version 1.34 11/29/01
  29. * @author Jeff Dinkins
  30. */
  31. public class ImageIcon implements Icon, Serializable
  32. {
  33. transient Image image;
  34. transient int loadStatus = 0;
  35. ImageObserver imageObserver;
  36. String description = null;
  37. protected final static Component component = new Component() {};
  38. protected final static MediaTracker tracker = new MediaTracker(component);
  39. int width = -1;
  40. int height = -1;
  41. /**
  42. * Creates an ImageIcon from the specified file. The image will
  43. * be preloaded by using MediaTracker to monitor the loading state
  44. * of the image.
  45. * @param filename the name of the file containing the image
  46. * @param description a brief textual description of the image
  47. * @see #ImageIcon(String)
  48. */
  49. public ImageIcon(String filename, String description) {
  50. image = Toolkit.getDefaultToolkit().getImage(filename);
  51. this.description = description;
  52. loadImage(image);
  53. }
  54. /**
  55. * Creates an ImageIcon from the specified file. The image will
  56. * be preloaded by using MediaTracker to monitor the loading state
  57. * of the image. The specified String can be a file name or a
  58. * file path. When specifying a path, use the Internet-standard
  59. * forward-slash ("/") as a separator. For example, specify:<pre>
  60. * new ImageIcon("images/myImage.gif")
  61. * </pre>
  62. * (The string is converted to an URL, so the forward-slash works
  63. * on all systems.)
  64. *
  65. * @param filename a String specifying a filename or path
  66. */
  67. public ImageIcon (String filename) {
  68. this(filename, filename);
  69. }
  70. /**
  71. * Creates an ImageIcon from the specified URL. The image will
  72. * be preloaded by using MediaTracker to monitor the loaded state
  73. * of the image.
  74. * @param URL the URL for the image
  75. * @param description a brief textual description of the image
  76. * @see #ImageIcon(String)
  77. */
  78. public ImageIcon(URL location, String description) {
  79. image = Toolkit.getDefaultToolkit().getImage(location);
  80. this.description = description;
  81. loadImage(image);
  82. }
  83. /**
  84. * Creates an ImageIcon from the specified URL. The image will
  85. * be preloaded by using MediaTracker to monitor the loaded state
  86. * of the image.
  87. */
  88. public ImageIcon (URL location) {
  89. this(location, location.toExternalForm());
  90. }
  91. /**
  92. * Creates an ImageIcon from the image.
  93. * @param image the image
  94. * @param description a brief textual description of the image
  95. */
  96. public ImageIcon(Image image, String description) {
  97. this(image);
  98. this.description = description;
  99. }
  100. /**
  101. * Creates an ImageIcon from an image object.
  102. */
  103. public ImageIcon (Image image) {
  104. this.image = image;
  105. Object o = image.getProperty("comment", imageObserver);
  106. if (o instanceof String) {
  107. description = (String) o;
  108. }
  109. loadImage(image);
  110. }
  111. /**
  112. * Creates an ImageIcon from an array of bytes which were
  113. * read from an image file containing a supported image format,
  114. * such as GIF or JPEG. Normally this array is created
  115. * by reading an image using Class.getResourceAsStream(), but
  116. * the byte array may also be statically stored in a class.
  117. *
  118. * @param imageData an array of pixels in an image format supported
  119. * by the AWT Toolkit, such as GIF or JPEG.
  120. * @param description a brief textual description of the image
  121. * @see java.awt.Toolkit#createImage
  122. */
  123. public ImageIcon (byte[] imageData, String description) {
  124. this.image = Toolkit.getDefaultToolkit().createImage(imageData);
  125. if (image == null) {
  126. return;
  127. }
  128. this.description = description;
  129. loadImage(image);
  130. }
  131. /**
  132. * Creates an ImageIcon from an array of bytes which were
  133. * read from an image file containing a supported image format,
  134. * such as GIF or JPEG. Normally this array is created
  135. * by reading an image using Class.getResourceAsStream(), but
  136. * the byte array may also be statically stored in a class.
  137. *
  138. * @param an array of pixels in an image format supported by
  139. * the AWT Toolkit, such as GIF or JPEG.
  140. * @see java.awt.Toolkit#createImage
  141. */
  142. public ImageIcon (byte[] imageData) {
  143. this.image = Toolkit.getDefaultToolkit().createImage(imageData);
  144. if (image == null) {
  145. return;
  146. }
  147. Object o = image.getProperty("comment", imageObserver);
  148. if (o instanceof String) {
  149. description = (String) o;
  150. }
  151. loadImage(image);
  152. }
  153. /**
  154. * Creates an uninitialized image icon.
  155. */
  156. public ImageIcon() {
  157. }
  158. /**
  159. * Wait for the image to load
  160. */
  161. protected void loadImage(Image image) {
  162. synchronized(tracker) {
  163. tracker.addImage(image, 0);
  164. try {
  165. tracker.waitForID(0, 5000);
  166. } catch (InterruptedException e) {
  167. System.out.println("INTERRUPTED while loading Image");
  168. }
  169. loadStatus = tracker.statusID(0, false);
  170. tracker.removeImage(image, 0);
  171. width = image.getWidth(imageObserver);
  172. height = image.getHeight(imageObserver);
  173. }
  174. }
  175. /**
  176. * Returns the status of the image loading operation.
  177. * @return the loading status as defined by java.awt.MediaTracker.
  178. * @see java.awt.MediaTracker#ABORTED
  179. * @see java.awt.MediaTracker#ERRORED
  180. * @see java.awt.MediaTracker#COMPLETE
  181. */
  182. public int getImageLoadStatus() {
  183. return loadStatus;
  184. }
  185. /**
  186. * Returns the Icon's Image
  187. */
  188. public Image getImage() {
  189. return image;
  190. }
  191. /**
  192. * Set the image displayed by this icon.
  193. */
  194. public void setImage(Image image) {
  195. this.image = image;
  196. loadImage(image);
  197. }
  198. /**
  199. * Get the description of the image. This is meant to be a brief
  200. * textual description of the object. For example, it might be
  201. * presented to a blind user to give an indication of the purpose
  202. * of the image.
  203. */
  204. public String getDescription() {
  205. return description;
  206. }
  207. /**
  208. * Set the description of the image. This is meant to be a brief
  209. * textual description of the object. For example, it might be
  210. * presented to a blind user to give an indication of the purpose
  211. * of the image.
  212. */
  213. public void setDescription(String description) {
  214. this.description = description;
  215. }
  216. /**
  217. * Paints the Icon
  218. */
  219. public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
  220. if(imageObserver == null) {
  221. g.drawImage(image, x, y, c);
  222. } else {
  223. g.drawImage(image, x, y, imageObserver);
  224. }
  225. }
  226. /**
  227. * Get the width of the Icon
  228. */
  229. public int getIconWidth() {
  230. return width;
  231. }
  232. /**
  233. * Get the height of the Icon
  234. */
  235. public int getIconHeight() {
  236. return height;
  237. }
  238. /**
  239. * Set the image observer for the image. Set this
  240. * property if the ImageIcon contains an animated GIF, so
  241. * the observer is notified to update its display.
  242. * For example:
  243. * <pre>
  244. * icon = new ImageIcon(...)
  245. * button.setIcon(icon);
  246. * icon.setImageObserver(button);
  247. * </pre>
  248. */
  249. public void setImageObserver(ImageObserver observer) {
  250. imageObserver = observer;
  251. }
  252. /**
  253. * Return the umage observer for the image
  254. */
  255. public ImageObserver getImageObserver() {
  256. return imageObserver;
  257. }
  258. private void readObject(ObjectInputStream s)
  259. throws ClassNotFoundException, IOException
  260. {
  261. s.defaultReadObject();
  262. int w = s.readInt();
  263. int h = s.readInt();
  264. int[] pixels = (int[])(s.readObject());
  265. if (pixels != null) {
  266. Toolkit tk = Toolkit.getDefaultToolkit();
  267. ColorModel cm = ColorModel.getRGBdefault();
  268. image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w));
  269. }
  270. }
  271. private void writeObject(ObjectOutputStream s)
  272. throws IOException
  273. {
  274. s.defaultWriteObject();
  275. int w = getIconWidth();
  276. int h = getIconHeight();
  277. int[] pixels = image != null? new int[w * h] : null;
  278. if (image != null) {
  279. try {
  280. PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
  281. pg.grabPixels();
  282. if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
  283. throw new IOException("failed to load image contents");
  284. }
  285. }
  286. catch (InterruptedException e) {
  287. throw new IOException("image load interrupted");
  288. }
  289. }
  290. s.writeInt(w);
  291. s.writeInt(h);
  292. s.writeObject(pixels);
  293. }
  294. }