package ch.werner_randelshofer.gui;/*Copyright (C) 1999 Werner Randelshoferwerner.randelshofer@mythen.chhttp://www.mythen.ch/w.randelshofer/    Permission to use this release of Icon is herebygranted without fee provided that the complete copyrightnotice and this permission notice appear in all copiesand in supporting documentation.*/import java.awt.Image;import java.awt.image.ImageObserver;import java.awt.Component;import java.awt.MediaTracker;import java.awt.Toolkit;import java.awt.Graphics;import java.net.URL;/** * Placeholder for javax.swing.ImageIcon. * * @author	Werner Randelshofer, Staldenmattweg 2, CH-6405 Immensee, Switzerland * * @version	1999-02-28	Derived from javax.swing.ImageIcon. */public class ImageIconimplements Icon	{	transient Image image;	transient int loadStatus = 0;	ImageObserver imageObserver;	String description = null;	protected final static Component component = new Component() {};	protected final static MediaTracker tracker = new MediaTracker(component);	int width = -1;	int height = -1;	/**	 * Creates an ImageIcon from the specified file. The image will	 * be preloaded by using MediaTracker to monitor the loading state	 * of the image.	 * @param filename the name of the file containing the image	 * @param description a brief textual description of the image	 * @see #ImageIcon(String)	 */	public ImageIcon(String filename, String description)		{		image = Toolkit.getDefaultToolkit().getImage(filename);		this.description = description;		loadImage(image);		}	/**	 * Creates an ImageIcon from the specified file. The image will	 * be preloaded by using MediaTracker to monitor the loading state	 * of the image. The specified String can be a file name or a	 * file path. When specifying a path, use the Internet-standard	 * forward-slash ("/") as a separator. For example, specify:<pre>	 *    new ImageIcon("images/myImage.gif")	 * </pre>	 * (The string is converted to an URL, so the forward-slash works	 * on all systems.)	 *	 * @param filename a String specifying a filename or path	 */	public ImageIcon (String filename)		{		this(filename, filename);		}	/**	 * Creates an ImageIcon from the specified URL. The image will	 * be preloaded by using MediaTracker to monitor the loaded state	 * of the image.	 * @param URL the URL for the image	 * @param description a brief textual description of the image	 * @see #ImageIcon(String)	 */	public ImageIcon(URL location, String description)		{		image = Toolkit.getDefaultToolkit().getImage(location);		this.description = description;		loadImage(image);		}	/**	 * Creates an ImageIcon from the specified URL. The image will	 * be preloaded by using MediaTracker to monitor the loaded state	 * of the image.	 */	public ImageIcon (URL location)		{		this(location, location.toExternalForm());		}	/**	 * Creates an ImageIcon from the image. 	 * @param image the image	 * @param description a brief textual description of the image	 */	public ImageIcon(Image image, String description)		{		this(image);		this.description = description;		}	/**	 * Creates an ImageIcon from an image object. 	 */	public ImageIcon (Image image)		{		this.image = image;		Object o = image.getProperty("comment", imageObserver);		if (o instanceof String)			{ description = (String) o; }		loadImage(image);		}	/**	 * Creates an ImageIcon from an array of bytes which were	 * read from an image file containing a supported image format,	 * such as GIF or JPEG.  Normally this array is created	 * by reading an image using Class.getResourceAsStream(), but	 * the byte array may also be statically stored in a class.	 *	 * @param  imageData an array of pixels in an image format supported	 *         by the AWT Toolkit, such as GIF or JPEG.	 * @param  description a brief textual description of the image	 * @see    java.awt.Toolkit#createImage	 */	public ImageIcon (byte[] imageData, String description)		{		this.image = Toolkit.getDefaultToolkit().createImage(imageData);		if (image == null)			{ return; }		this.description = description;		loadImage(image);		}	/**	 * Creates an ImageIcon from an array of bytes which were	 * read from an image file containing a supported image format,	 * such as GIF or JPEG.  Normally this array is created	 * by reading an image using Class.getResourceAsStream(), but	 * the byte array may also be statically stored in a class.	 *	 * @param  an array of pixels in an image format supported by	 *         the AWT Toolkit, such as GIF or JPEG.	 * @see    java.awt.Toolkit#createImage	 */	public ImageIcon (byte[] imageData)		{		this.image = Toolkit.getDefaultToolkit().createImage(imageData);		if (image == null)			{ return; }		Object o = image.getProperty("comment", imageObserver);		if (o instanceof String)			{ description = (String) o; }		loadImage(image);		}	/**	 * Creates an uninitialized image icon.	 */	public ImageIcon()    	{ }	/**	 * Wait for the image to load	 */	protected void loadImage(Image image)		{		synchronized(tracker)			{			tracker.addImage(image, 0);			try {				tracker.waitForID(0, 5000);				}			catch (InterruptedException e)				{				System.out.println("INTERRUPTED while loading Image");				}			loadStatus = tracker.statusID(0, false);			tracker.removeImage(image, 0);			width = image.getWidth(imageObserver);			height = image.getHeight(imageObserver);			}		}	/**	 * Returns the status of the image loading operation.	 * @return the loading status as defined by java.awt.MediaTracker.	 * @see java.awt.MediaTracker#ABORTED	 * @see java.awt.MediaTracker#ERRORED	 * @see java.awt.MediaTracker#COMPLETE	 */	public int getImageLoadStatus()		{ return loadStatus; }	/**	 * Returns the Icon's Image	 */	public Image getImage()		{ return image; }    /**     * Set the image displayed by this icon.     */    public void setImage(Image image)    	{    	this.image = image;    	loadImage(image);		}	/**	 * Get the description of the image.  This is meant to be a brief	 * textual description of the object.  For example, it might be	 * presented to a blind user to give an indication of the purpose	 * of the image.	 */	public String getDescription()		{ return description; }	/**	 * Set the description of the image.  This is meant to be a brief	 * textual description of the object.  For example, it might be	 * presented to a blind user to give an indication of the purpose	 * of the image.	 */	public void setDescription(String description)		{ this.description = description; }	/**	 * Paints the Icon	 */	public synchronized void paintIcon(Component c, Graphics g, int x, int y)		{		if(imageObserver == null)			{			g.drawImage(image, x, y, c);			}		else			{			g.drawImage(image, x, y, imageObserver);			}		}	/**	 * Get the width of the Icon	 */	public int getIconWidth()		{ return width; }	/**	 * Get the height of the Icon	 */	public int getIconHeight()		{ return height; }	/** 	 * Set the image observer for the image.  Set this	 * property if the ImageIcon contains an animated GIF, so	 * the observer is notified to update its display.	 * For example:	 * <pre>	 *     icon = new ImageIcon(...)	 *     button.setIcon(icon);	 *     icon.setImageObserver(button);	 * </pre>	 */	public void setImageObserver(ImageObserver observer)		{ imageObserver = observer; }	/**	 *  Return the image observer for the image 	 */	public ImageObserver getImageObserver()		{ return imageObserver; }	}