home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
jfc.bin
/
ImageIcon.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
10KB
|
330 lines
/*
* @(#)ImageIcon.java 1.26 98/02/06
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing;
import java.awt.*;
import java.awt.image.*;
import java.net.URL;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
/**
* An implementation of the Icon interface that paints Icons
* from Images. Images that are created from a URL or filename
* are preloaded using MediaTracker to monitor the loaded state
* of the image.
* <p>
* Warning: serialized objects of this class will not be compatible with
* future swing releases. The current serialization support is appropriate
* for short term storage or RMI between Swing1.0 applications. It will
* not be possible to load serialized Swing1.0 objects with future releases
* of Swing. The JDK1.2 release of Swing will be the compatibility
* baseline for the serialized form of Swing objects.
*
* @version 1.26 02/06/98
* @author Jeff Dinkins
*/
public class ImageIcon implements Icon, Serializable
{
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
*/
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.
*/
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
*/
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 the image.
*/
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.
* For example:
* <pre>
* icon = new ImageIcon(...)
* button.setImage(icon);
* icon.setImageObserver(button);
* </pre>
*/
public void setImageObserver(ImageObserver observer) {
imageObserver = observer;
}
/**
* Return the umage observer for the image
*/
public ImageObserver getImageObserver() {
return imageObserver;
}
private void readObject(ObjectInputStream s)
throws ClassNotFoundException, IOException
{
s.defaultReadObject();
int w = s.readInt();
int h = s.readInt();
int[] pixels = (int[])(s.readObject());
Toolkit tk = Toolkit.getDefaultToolkit();
ColorModel cm = ColorModel.getRGBdefault();
image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w));
}
private void writeObject(ObjectOutputStream s)
throws IOException
{
s.defaultWriteObject();
int w = getIconWidth();
int h = getIconHeight();
int[] pixels = new int[w * h];
try {
PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
pg.grabPixels();
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
throw new IOException("failed to load image contents");
}
}
catch (InterruptedException e) {
throw new IOException("image load interrupted");
}
s.writeInt(w);
s.writeInt(h);
s.writeObject(pixels);
}
}