jdk/src/share/classes/javax/swing/ImageIcon.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 466 6acd5ec503a8
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * An implementation of the Icon interface that paints Icons
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * from Images. Images that are created from a URL, filename or byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * are preloaded using MediaTracker to monitor the loaded state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * For further information and examples of using image icons, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html">How to Use Icons</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author Lynn Monsanto
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public class ImageIcon implements Icon, Serializable, Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /* Keep references to the filename and location so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * alternate persistence schemes have the option to archive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * images symbolically rather than including the image data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * in the archive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    transient private String filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    transient private URL location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    transient Image image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    transient int loadStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    ImageObserver imageObserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    String description = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected final static Component component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected final static MediaTracker tracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        component = new Component() {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    // 6482575 - clear the appContext field so as not to leak it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    Field appContextField =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                 Component.class.getDeclaredField("appContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    appContextField.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    appContextField.set(component, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                catch (NoSuchFieldException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        tracker = new MediaTracker(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Id used in loading images from MediaTracker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static int mediaTrackerID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private final static Object TRACKER_KEY = new StringBuilder("TRACKER_KEY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    int width = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    int height = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Creates an ImageIcon from the specified file. The image will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * be preloaded by using MediaTracker to monitor the loading state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param filename the name of the file containing the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param description a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see #ImageIcon(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public ImageIcon(String filename, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        image = Toolkit.getDefaultToolkit().getImage(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.filename = filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Creates an ImageIcon from the specified file. The image will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * be preloaded by using MediaTracker to monitor the loading state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * of the image. The specified String can be a file name or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * file path. When specifying a path, use the Internet-standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * forward-slash ("/") as a separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * (The string is converted to an URL, so the forward-slash works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * on all systems.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * For example, specify:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *    new ImageIcon("images/myImage.gif") </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The description is initialized to the <code>filename</code> string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param filename a String specifying a filename or path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see #getDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    @ConstructorProperties({"description"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public ImageIcon (String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this(filename, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Creates an ImageIcon from the specified URL. The image will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * be preloaded by using MediaTracker to monitor the loaded state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param location the URL for the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param description a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see #ImageIcon(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public ImageIcon(URL location, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        image = Toolkit.getDefaultToolkit().getImage(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this.location = location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Creates an ImageIcon from the specified URL. The image will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * be preloaded by using MediaTracker to monitor the loaded state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * The icon's description is initialized to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * a string representation of the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param location the URL for the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see #getDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public ImageIcon (URL location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this(location, location.toExternalForm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Creates an ImageIcon from the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param image the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param description a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public ImageIcon(Image image, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Creates an ImageIcon from an image object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * If the image has a "comment" property that is a string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * then the string is used as the description of this icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param image the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @see #getDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @see java.awt.Image#getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public ImageIcon (Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        this.image = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        Object o = image.getProperty("comment", imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (o instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            description = (String) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Creates an ImageIcon from an array of bytes which were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * read from an image file containing a supported image format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * such as GIF, JPEG, or (as of 1.3) PNG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Normally this array is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * by reading an image using Class.getResourceAsStream(), but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * the byte array may also be statically stored in a class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param  imageData an array of pixels in an image format supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *         by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param  description a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see    java.awt.Toolkit#createImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public ImageIcon (byte[] imageData, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        this.image = Toolkit.getDefaultToolkit().createImage(imageData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Creates an ImageIcon from an array of bytes which were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * read from an image file containing a supported image format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * such as GIF, JPEG, or (as of 1.3) PNG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Normally this array is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * by reading an image using Class.getResourceAsStream(), but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * the byte array may also be statically stored in a class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * If the resulting image has a "comment" property that is a string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * then the string is used as the description of this icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param  imageData an array of pixels in an image format supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *             the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see    java.awt.Toolkit#createImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see #getDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see java.awt.Image#getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public ImageIcon (byte[] imageData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        this.image = Toolkit.getDefaultToolkit().createImage(imageData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        Object o = image.getProperty("comment", imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (o instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            description = (String) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Creates an uninitialized image icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public ImageIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Loads the image, returning only when the image is loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param image the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    protected void loadImage(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        MediaTracker mTracker = getTracker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        synchronized(mTracker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            int id = getNextID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            mTracker.addImage(image, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                mTracker.waitForID(id, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                System.out.println("INTERRUPTED while loading Image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            loadStatus = mTracker.statusID(id, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            mTracker.removeImage(image, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            width = image.getWidth(imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            height = image.getHeight(imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Returns an ID to use with the MediaTracker in loading an image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private int getNextID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        synchronized(getTracker()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return ++mediaTrackerID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Returns the MediaTracker for the current AppContext, creating a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * MediaTracker if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private MediaTracker getTracker() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        Object trackerObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        AppContext ac = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // Opt: Only synchronize if trackerObj comes back null?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // If null, synchronize, re-check for null, and put new tracker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        synchronized(ac) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            trackerObj = ac.get(TRACKER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (trackerObj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                Component comp = new Component() {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                trackerObj = new MediaTracker(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                ac.put(TRACKER_KEY, trackerObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return (MediaTracker) trackerObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Returns the status of the image loading operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @return the loading status as defined by java.awt.MediaTracker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @see java.awt.MediaTracker#ABORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @see java.awt.MediaTracker#ERRORED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @see java.awt.MediaTracker#COMPLETE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public int getImageLoadStatus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return loadStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Returns this icon's <code>Image</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @return the <code>Image</code> object for this <code>ImageIcon</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public Image getImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Sets the image displayed by this icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param image the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public void setImage(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        this.image = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Gets the description of the image.  This is meant to be a brief
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * textual description of the object.  For example, it might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * presented to a blind user to give an indication of the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * The description may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public String getDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Sets the description of the image.  This is meant to be a brief
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * textual description of the object.  For example, it might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * presented to a blind user to give an indication of the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param description a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public void setDescription(String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Paints the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * The top-left corner of the icon is drawn at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * the point (<code>x</code>, <code>y</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * in the coordinate space of the graphics context <code>g</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * If this icon has no image observer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * this method uses the <code>c</code> component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * as the observer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param c the component to be used as the observer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *          if this icon has no image observer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @param x the X coordinate of the icon's top-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param y the Y coordinate of the icon's top-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if(imageObserver == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
           g.drawImage(image, x, y, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
           g.drawImage(image, x, y, imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Gets the width of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @return the width in pixels of this icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public int getIconWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Gets the height of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @return the height in pixels of this icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public int getIconHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Sets the image observer for the image.  Set this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * property if the ImageIcon contains an animated GIF, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * the observer is notified to update its display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *     icon = new ImageIcon(...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *     button.setIcon(icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *     icon.setImageObserver(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param observer the image observer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public void setImageObserver(ImageObserver observer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        imageObserver = observer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Returns the image observer for the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @return the image observer, which may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public ImageObserver getImageObserver() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return imageObserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Returns a string representation of this image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @return a string representing this image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (description != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        int w = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        int h = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        int[] pixels = (int[])(s.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (pixels != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            ColorModel cm = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        int w = getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        int h = getIconHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        int[] pixels = image != null? new int[w * h] : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (image != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                pg.grabPixels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    throw new IOException("failed to load image contents");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                throw new IOException("image load interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        s.writeInt(w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        s.writeInt(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        s.writeObject(pixels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * --- Accessibility Support ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    private AccessibleImageIcon accessibleContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Gets the AccessibleContext associated with this ImageIcon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * For image icons, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * AccessibleImageIcon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * A new AccessibleImageIcon instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @return an AccessibleImageIcon that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *         AccessibleContext of this ImageIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *  description: The AccessibleContext associated with this ImageIcon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            accessibleContext = new AccessibleImageIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <code>ImageIcon</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Java Accessibility API appropriate to image icon user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    protected class AccessibleImageIcon extends AccessibleContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        implements AccessibleIcon, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
         * AccessibleContest implementation -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
         * Gets the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            return AccessibleRole.ICON;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * Gets the state of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         * @return an instance of AccessibleStateSet containing the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         * state set of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         * @see AccessibleState
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         * Gets the Accessible parent of this object.  If the parent of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         * object implements Accessible, this method should simply return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
         * getParent().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         * @return the Accessible parent of this object -- can be null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * object does not have an Accessible parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        public Accessible getAccessibleParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * Gets the index of this object in its accessible parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         * @return the index of this object in its parent; -1 if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         * object does not have an accessible parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
         * @see #getAccessibleParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        public int getAccessibleIndexInParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         * Returns the number of accessible children in the object.  If all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         * of the children of this object implement Accessible, than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * method should return the number of children of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         * @return the number of accessible children in the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        public int getAccessibleChildrenCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * Returns the nth Accessible child of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * @param i zero-based index of child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * @return the nth Accessible child of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        public Accessible getAccessibleChild(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * Returns the locale of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         * @return the locale of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        public Locale getLocale() throws IllegalComponentStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         * AccessibleIcon implementation -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * Gets the description of the icon.  This is meant to be a brief
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * textual description of the object.  For example, it might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * presented to a blind user to give an indication of the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         * @return the description of the icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        public String getAccessibleIconDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            return ImageIcon.this.getDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         * Sets the description of the icon.  This is meant to be a brief
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         * textual description of the object.  For example, it might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         * presented to a blind user to give an indication of the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         * of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
         * @param description the description of the icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        public void setAccessibleIconDescription(String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            ImageIcon.this.setDescription(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         * Gets the height of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
         * @return the height of the icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        public int getAccessibleIconHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            return ImageIcon.this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         * Gets the width of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
         * @return the width of the icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        public int getAccessibleIconWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            return ImageIcon.this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }  // AccessibleImageIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
}