0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.image.*;
0N/Aimport java.beans.ConstructorProperties;
243N/Aimport java.beans.Transient;
0N/Aimport java.net.URL;
0N/A
0N/Aimport java.io.Serializable;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/A
0N/Aimport java.util.Locale;
0N/Aimport javax.accessibility.*;
0N/A
0N/Aimport sun.awt.AppContext;
0N/Aimport java.lang.reflect.Field;
4321N/Aimport java.security.*;
0N/A
0N/A/**
0N/A * An implementation of the Icon interface that paints Icons
0N/A * from Images. Images that are created from a URL, filename or byte array
0N/A * are preloaded using MediaTracker to monitor the loaded state
0N/A * of the image.
0N/A *
0N/A * <p>
0N/A * For further information and examples of using image icons, see
0N/A * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html">How to Use Icons</a>
0N/A * in <em>The Java Tutorial.</em>
0N/A *
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @author Jeff Dinkins
0N/A * @author Lynn Monsanto
0N/A */
0N/Apublic class ImageIcon implements Icon, Serializable, Accessible {
0N/A /* Keep references to the filename and location so that
0N/A * alternate persistence schemes have the option to archive
0N/A * images symbolically rather than including the image data
0N/A * in the archive.
0N/A */
0N/A transient private String filename;
0N/A transient private URL location;
0N/A
0N/A transient Image image;
0N/A transient int loadStatus = 0;
0N/A ImageObserver imageObserver;
0N/A String description = null;
0N/A
4321N/A // Fields for twisted backward compatibility only. DO NOT USE.
0N/A protected final static Component component;
0N/A protected final static MediaTracker tracker;
0N/A
0N/A static {
4321N/A component = AccessController.doPrivileged(new PrivilegedAction<Component>() {
4321N/A public Component run() {
0N/A try {
4321N/A final Component component = createNoPermsComponent();
4321N/A
0N/A // 6482575 - clear the appContext field so as not to leak it
0N/A Field appContextField =
4321N/A
4321N/A Component.class.getDeclaredField("appContext");
0N/A appContextField.setAccessible(true);
0N/A appContextField.set(component, null);
4321N/A
4321N/A return component;
4321N/A } catch (Throwable e) {
4321N/A // We don't care about component.
4321N/A // So don't prevent class initialisation.
0N/A e.printStackTrace();
4321N/A return null;
0N/A }
0N/A }
0N/A });
0N/A tracker = new MediaTracker(component);
0N/A }
0N/A
4321N/A private static Component createNoPermsComponent() {
4321N/A // 7020198 - set acc field to no permissions and no subject
4321N/A // Note, will have appContext set.
4321N/A return AccessController.doPrivileged(
4321N/A new PrivilegedAction<Component>() {
4321N/A public Component run() {
4321N/A return new Component() {
4321N/A };
4321N/A }
4321N/A },
4321N/A new AccessControlContext(new ProtectionDomain[]{
4321N/A new ProtectionDomain(null, null)
4321N/A })
4321N/A );
4321N/A }
4321N/A
0N/A /**
0N/A * Id used in loading images from MediaTracker.
0N/A */
0N/A private static int mediaTrackerID;
0N/A
0N/A private final static Object TRACKER_KEY = new StringBuilder("TRACKER_KEY");
0N/A
0N/A int width = -1;
0N/A int height = -1;
0N/A
0N/A /**
0N/A * Creates an ImageIcon from the specified file. The image will
0N/A * be preloaded by using MediaTracker to monitor the loading state
0N/A * of the image.
0N/A * @param filename the name of the file containing the image
0N/A * @param description a brief textual description of the image
0N/A * @see #ImageIcon(String)
0N/A */
0N/A public ImageIcon(String filename, String description) {
0N/A image = Toolkit.getDefaultToolkit().getImage(filename);
0N/A if (image == null) {
0N/A return;
0N/A }
0N/A this.filename = filename;
0N/A this.description = description;
0N/A loadImage(image);
0N/A }
0N/A
0N/A /**
0N/A * Creates an ImageIcon from the specified file. The image will
0N/A * be preloaded by using MediaTracker to monitor the loading state
0N/A * of the image. The specified String can be a file name or a
0N/A * file path. When specifying a path, use the Internet-standard
0N/A * forward-slash ("/") as a separator.
0N/A * (The string is converted to an URL, so the forward-slash works
0N/A * on all systems.)
0N/A * For example, specify:
0N/A * <pre>
0N/A * new ImageIcon("images/myImage.gif") </pre>
0N/A * The description is initialized to the <code>filename</code> string.
0N/A *
0N/A * @param filename a String specifying a filename or path
0N/A * @see #getDescription
0N/A */
0N/A @ConstructorProperties({"description"})
0N/A public ImageIcon (String filename) {
0N/A this(filename, filename);
0N/A }
0N/A
0N/A /**
0N/A * Creates an ImageIcon from the specified URL. The image will
0N/A * be preloaded by using MediaTracker to monitor the loaded state
0N/A * of the image.
0N/A * @param location the URL for the image
0N/A * @param description a brief textual description of the image
0N/A * @see #ImageIcon(String)
0N/A */
0N/A public ImageIcon(URL location, String description) {
0N/A image = Toolkit.getDefaultToolkit().getImage(location);
0N/A if (image == null) {
0N/A return;
0N/A }
0N/A this.location = location;
0N/A this.description = description;
0N/A loadImage(image);
0N/A }
0N/A
0N/A /**
0N/A * Creates an ImageIcon from the specified URL. The image will
0N/A * be preloaded by using MediaTracker to monitor the loaded state
0N/A * of the image.
0N/A * The icon's description is initialized to be
0N/A * a string representation of the URL.
0N/A * @param location the URL for the image
0N/A * @see #getDescription
0N/A */
0N/A public ImageIcon (URL location) {
0N/A this(location, location.toExternalForm());
0N/A }
0N/A
0N/A /**
0N/A * Creates an ImageIcon from the image.
0N/A * @param image the image
0N/A * @param description a brief textual description of the image
0N/A */
0N/A public ImageIcon(Image image, String description) {
0N/A this(image);
0N/A this.description = description;
0N/A }
0N/A
0N/A /**
0N/A * Creates an ImageIcon from an image object.
0N/A * If the image has a "comment" property that is a string,
0N/A * then the string is used as the description of this icon.
0N/A * @param image the image
0N/A * @see #getDescription
0N/A * @see java.awt.Image#getProperty
0N/A */
0N/A public ImageIcon (Image image) {
0N/A this.image = image;
0N/A Object o = image.getProperty("comment", imageObserver);
0N/A if (o instanceof String) {
0N/A description = (String) o;
0N/A }
0N/A loadImage(image);
0N/A }
0N/A
0N/A /**
0N/A * Creates an ImageIcon from an array of bytes which were
0N/A * read from an image file containing a supported image format,
0N/A * such as GIF, JPEG, or (as of 1.3) PNG.
0N/A * Normally this array is created
0N/A * by reading an image using Class.getResourceAsStream(), but
0N/A * the byte array may also be statically stored in a class.
0N/A *
0N/A * @param imageData an array of pixels in an image format supported
0N/A * by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
0N/A * @param description a brief textual description of the image
0N/A * @see java.awt.Toolkit#createImage
0N/A */
0N/A public ImageIcon (byte[] imageData, String description) {
0N/A this.image = Toolkit.getDefaultToolkit().createImage(imageData);
0N/A if (image == null) {
0N/A return;
0N/A }
0N/A this.description = description;
0N/A loadImage(image);
0N/A }
0N/A
0N/A /**
0N/A * Creates an ImageIcon from an array of bytes which were
0N/A * read from an image file containing a supported image format,
0N/A * such as GIF, JPEG, or (as of 1.3) PNG.
0N/A * Normally this array is created
0N/A * by reading an image using Class.getResourceAsStream(), but
0N/A * the byte array may also be statically stored in a class.
0N/A * If the resulting image has a "comment" property that is a string,
0N/A * then the string is used as the description of this icon.
0N/A *
0N/A * @param imageData an array of pixels in an image format supported by
0N/A * the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
0N/A * @see java.awt.Toolkit#createImage
0N/A * @see #getDescription
0N/A * @see java.awt.Image#getProperty
0N/A */
0N/A public ImageIcon (byte[] imageData) {
0N/A this.image = Toolkit.getDefaultToolkit().createImage(imageData);
0N/A if (image == null) {
0N/A return;
0N/A }
0N/A Object o = image.getProperty("comment", imageObserver);
0N/A if (o instanceof String) {
0N/A description = (String) o;
0N/A }
0N/A loadImage(image);
0N/A }
0N/A
0N/A /**
0N/A * Creates an uninitialized image icon.
0N/A */
0N/A public ImageIcon() {
0N/A }
0N/A
0N/A /**
0N/A * Loads the image, returning only when the image is loaded.
0N/A * @param image the image
0N/A */
0N/A protected void loadImage(Image image) {
0N/A MediaTracker mTracker = getTracker();
0N/A synchronized(mTracker) {
0N/A int id = getNextID();
0N/A
0N/A mTracker.addImage(image, id);
0N/A try {
0N/A mTracker.waitForID(id, 0);
0N/A } catch (InterruptedException e) {
0N/A System.out.println("INTERRUPTED while loading Image");
0N/A }
0N/A loadStatus = mTracker.statusID(id, false);
0N/A mTracker.removeImage(image, id);
0N/A
0N/A width = image.getWidth(imageObserver);
0N/A height = image.getHeight(imageObserver);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an ID to use with the MediaTracker in loading an image.
0N/A */
0N/A private int getNextID() {
0N/A synchronized(getTracker()) {
0N/A return ++mediaTrackerID;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the MediaTracker for the current AppContext, creating a new
0N/A * MediaTracker if necessary.
0N/A */
0N/A private MediaTracker getTracker() {
0N/A Object trackerObj;
0N/A AppContext ac = AppContext.getAppContext();
0N/A // Opt: Only synchronize if trackerObj comes back null?
0N/A // If null, synchronize, re-check for null, and put new tracker
0N/A synchronized(ac) {
0N/A trackerObj = ac.get(TRACKER_KEY);
0N/A if (trackerObj == null) {
0N/A Component comp = new Component() {};
0N/A trackerObj = new MediaTracker(comp);
0N/A ac.put(TRACKER_KEY, trackerObj);
0N/A }
0N/A }
0N/A return (MediaTracker) trackerObj;
0N/A }
0N/A
0N/A /**
0N/A * Returns the status of the image loading operation.
0N/A * @return the loading status as defined by java.awt.MediaTracker
0N/A * @see java.awt.MediaTracker#ABORTED
0N/A * @see java.awt.MediaTracker#ERRORED
0N/A * @see java.awt.MediaTracker#COMPLETE
0N/A */
0N/A public int getImageLoadStatus() {
0N/A return loadStatus;
0N/A }
0N/A
0N/A /**
0N/A * Returns this icon's <code>Image</code>.
0N/A * @return the <code>Image</code> object for this <code>ImageIcon</code>
0N/A */
243N/A @Transient
0N/A public Image getImage() {
0N/A return image;
0N/A }
0N/A
0N/A /**
0N/A * Sets the image displayed by this icon.
0N/A * @param image the image
0N/A */
0N/A public void setImage(Image image) {
0N/A this.image = image;
0N/A loadImage(image);
0N/A }
0N/A
0N/A /**
0N/A * Gets the description of the image. This is meant to be a brief
0N/A * textual description of the object. For example, it might be
0N/A * presented to a blind user to give an indication of the purpose
0N/A * of the image.
0N/A * The description may be null.
0N/A *
0N/A * @return a brief textual description of the image
0N/A */
0N/A public String getDescription() {
0N/A return description;
0N/A }
0N/A
0N/A /**
0N/A * Sets the description of the image. This is meant to be a brief
0N/A * textual description of the object. For example, it might be
0N/A * presented to a blind user to give an indication of the purpose
0N/A * of the image.
0N/A * @param description a brief textual description of the image
0N/A */
0N/A public void setDescription(String description) {
0N/A this.description = description;
0N/A }
0N/A
0N/A /**
0N/A * Paints the icon.
0N/A * The top-left corner of the icon is drawn at
0N/A * the point (<code>x</code>, <code>y</code>)
0N/A * in the coordinate space of the graphics context <code>g</code>.
0N/A * If this icon has no image observer,
0N/A * this method uses the <code>c</code> component
0N/A * as the observer.
0N/A *
0N/A * @param c the component to be used as the observer
0N/A * if this icon has no image observer
0N/A * @param g the graphics context
0N/A * @param x the X coordinate of the icon's top-left corner
0N/A * @param y the Y coordinate of the icon's top-left corner
0N/A */
0N/A public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
0N/A if(imageObserver == null) {
0N/A g.drawImage(image, x, y, c);
0N/A } else {
0N/A g.drawImage(image, x, y, imageObserver);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the width of the icon.
0N/A *
0N/A * @return the width in pixels of this icon
0N/A */
0N/A public int getIconWidth() {
0N/A return width;
0N/A }
0N/A
0N/A /**
0N/A * Gets the height of the icon.
0N/A *
0N/A * @return the height in pixels of this icon
0N/A */
0N/A public int getIconHeight() {
0N/A return height;
0N/A }
0N/A
0N/A /**
0N/A * Sets the image observer for the image. Set this
0N/A * property if the ImageIcon contains an animated GIF, so
0N/A * the observer is notified to update its display.
0N/A * For example:
0N/A * <pre>
0N/A * icon = new ImageIcon(...)
0N/A * button.setIcon(icon);
0N/A * icon.setImageObserver(button);
0N/A * </pre>
0N/A *
0N/A * @param observer the image observer
0N/A */
0N/A public void setImageObserver(ImageObserver observer) {
0N/A imageObserver = observer;
0N/A }
0N/A
0N/A /**
0N/A * Returns the image observer for the image.
0N/A *
0N/A * @return the image observer, which may be null
0N/A */
243N/A @Transient
0N/A public ImageObserver getImageObserver() {
0N/A return imageObserver;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this image.
0N/A *
0N/A * @return a string representing this image
0N/A */
0N/A public String toString() {
0N/A if (description != null) {
0N/A return description;
0N/A }
0N/A return super.toString();
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream s)
0N/A throws ClassNotFoundException, IOException
0N/A {
0N/A s.defaultReadObject();
0N/A
0N/A int w = s.readInt();
0N/A int h = s.readInt();
0N/A int[] pixels = (int[])(s.readObject());
0N/A
0N/A if (pixels != null) {
0N/A Toolkit tk = Toolkit.getDefaultToolkit();
0N/A ColorModel cm = ColorModel.getRGBdefault();
0N/A image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w));
0N/A loadImage(image);
0N/A }
0N/A }
0N/A
0N/A
0N/A private void writeObject(ObjectOutputStream s)
0N/A throws IOException
0N/A {
0N/A s.defaultWriteObject();
0N/A
0N/A int w = getIconWidth();
0N/A int h = getIconHeight();
0N/A int[] pixels = image != null? new int[w * h] : null;
0N/A
0N/A if (image != null) {
0N/A try {
0N/A PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
0N/A pg.grabPixels();
0N/A if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
0N/A throw new IOException("failed to load image contents");
0N/A }
0N/A }
0N/A catch (InterruptedException e) {
0N/A throw new IOException("image load interrupted");
0N/A }
0N/A }
0N/A
0N/A s.writeInt(w);
0N/A s.writeInt(h);
0N/A s.writeObject(pixels);
0N/A }
0N/A
0N/A /**
0N/A * --- Accessibility Support ---
0N/A */
0N/A
0N/A private AccessibleImageIcon accessibleContext = null;
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this ImageIcon.
0N/A * For image icons, the AccessibleContext takes the form of an
0N/A * AccessibleImageIcon.
0N/A * A new AccessibleImageIcon instance is created if necessary.
0N/A *
0N/A * @return an AccessibleImageIcon that serves as the
0N/A * AccessibleContext of this ImageIcon
0N/A * @beaninfo
0N/A * expert: true
0N/A * description: The AccessibleContext associated with this ImageIcon.
0N/A * @since 1.3
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleImageIcon();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>ImageIcon</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to image icon user-interface
0N/A * elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A * @since 1.3
0N/A */
0N/A protected class AccessibleImageIcon extends AccessibleContext
0N/A implements AccessibleIcon, Serializable {
0N/A
0N/A /*
0N/A * AccessibleContest implementation -----------------
0N/A */
0N/A
0N/A /**
0N/A * Gets the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.ICON;
0N/A }
0N/A
0N/A /**
0N/A * Gets the state of this object.
0N/A *
0N/A * @return an instance of AccessibleStateSet containing the current
0N/A * state set of the object
0N/A * @see AccessibleState
0N/A */
0N/A public AccessibleStateSet getAccessibleStateSet() {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Gets the Accessible parent of this object. If the parent of this
0N/A * object implements Accessible, this method should simply return
0N/A * getParent().
0N/A *
0N/A * @return the Accessible parent of this object -- can be null if this
0N/A * object does not have an Accessible parent
0N/A */
0N/A public Accessible getAccessibleParent() {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Gets the index of this object in its accessible parent.
0N/A *
0N/A * @return the index of this object in its parent; -1 if this
0N/A * object does not have an accessible parent.
0N/A * @see #getAccessibleParent
0N/A */
0N/A public int getAccessibleIndexInParent() {
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of accessible children in the object. If all
0N/A * of the children of this object implement Accessible, than this
0N/A * method should return the number of children of this object.
0N/A *
0N/A * @return the number of accessible children in the object.
0N/A */
0N/A public int getAccessibleChildrenCount() {
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns the nth Accessible child of the object.
0N/A *
0N/A * @param i zero-based index of child
0N/A * @return the nth Accessible child of the object
0N/A */
0N/A public Accessible getAccessibleChild(int i) {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the locale of this object.
0N/A *
0N/A * @return the locale of this object
0N/A */
0N/A public Locale getLocale() throws IllegalComponentStateException {
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * AccessibleIcon implementation -----------------
0N/A */
0N/A
0N/A /**
0N/A * Gets the description of the icon. This is meant to be a brief
0N/A * textual description of the object. For example, it might be
0N/A * presented to a blind user to give an indication of the purpose
0N/A * of the icon.
0N/A *
0N/A * @return the description of the icon
0N/A */
0N/A public String getAccessibleIconDescription() {
0N/A return ImageIcon.this.getDescription();
0N/A }
0N/A
0N/A /**
0N/A * Sets the description of the icon. This is meant to be a brief
0N/A * textual description of the object. For example, it might be
0N/A * presented to a blind user to give an indication of the purpose
0N/A * of the icon.
0N/A *
0N/A * @param description the description of the icon
0N/A */
0N/A public void setAccessibleIconDescription(String description) {
0N/A ImageIcon.this.setDescription(description);
0N/A }
0N/A
0N/A /**
0N/A * Gets the height of the icon.
0N/A *
0N/A * @return the height of the icon
0N/A */
0N/A public int getAccessibleIconHeight() {
0N/A return ImageIcon.this.height;
0N/A }
0N/A
0N/A /**
0N/A * Gets the width of the icon.
0N/A *
0N/A * @return the width of the icon
0N/A */
0N/A public int getAccessibleIconWidth() {
0N/A return ImageIcon.this.width;
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream s)
0N/A throws ClassNotFoundException, IOException
0N/A {
0N/A s.defaultReadObject();
0N/A }
0N/A
0N/A private void writeObject(ObjectOutputStream s)
0N/A throws IOException
0N/A {
0N/A s.defaultWriteObject();
0N/A }
0N/A } // AccessibleImageIcon
0N/A}