jdk/src/share/classes/javax/swing/ImageIcon.java
author cl
Mon, 07 Oct 2013 11:34:44 -0700
changeset 20458 f2423fb3fd19
parent 20455 f6f9a0c2796b
child 21257 57214b6e9c3e
permissions -rw-r--r--
8025840: Fix all the doclint warnings about trademark Reviewed-by: art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1843
diff changeset
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1843
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1843
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1843
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1843
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1843
diff changeset
    23
 * questions.
2
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;
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
    30
import java.beans.Transient;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.lang.reflect.Field;
9850
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
    43
import java.security.*;
2
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
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 9850
diff changeset
    53
 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html">How to Use Icons</a>
2
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
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
    62
 * of all JavaBeans&trade;
2
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
9850
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
    83
    // Fields for twisted backward compatibility only. DO NOT USE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected final static Component component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected final static MediaTracker tracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static {
9850
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
    88
        component = AccessController.doPrivileged(new PrivilegedAction<Component>() {
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
    89
            public Component run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                try {
9850
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
    91
                    final Component component = createNoPermsComponent();
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
    92
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    // 6482575 - clear the appContext field so as not to leak it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    Field appContextField =
9850
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
    95
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
    96
                            Component.class.getDeclaredField("appContext");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    appContextField.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    appContextField.set(component, null);
9850
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
    99
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   100
                    return component;
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   101
                } catch (Throwable e) {
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   102
                    // We don't care about component.
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   103
                    // So don't prevent class initialisation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    e.printStackTrace();
9850
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   105
                    return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        tracker = new MediaTracker(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
9850
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   112
    private static Component createNoPermsComponent() {
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   113
        // 7020198 - set acc field to no permissions and no subject
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   114
        // Note, will have appContext set.
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   115
        return AccessController.doPrivileged(
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   116
                new PrivilegedAction<Component>() {
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   117
                    public Component run() {
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   118
                        return new Component() {
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   119
                        };
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   120
                    }
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   121
                },
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   122
                new AccessControlContext(new ProtectionDomain[]{
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   123
                        new ProtectionDomain(null, null)
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   124
                })
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   125
        );
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   126
    }
2689977af15b 7020198: ImageIcon creates Component with null acc
alexp
parents: 5506
diff changeset
   127
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Id used in loading images from MediaTracker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static int mediaTrackerID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private final static Object TRACKER_KEY = new StringBuilder("TRACKER_KEY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    int width = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    int height = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Creates an ImageIcon from the specified file. The image will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * be preloaded by using MediaTracker to monitor the loading state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param filename the name of the file containing the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param description a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see #ImageIcon(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public ImageIcon(String filename, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        image = Toolkit.getDefaultToolkit().getImage(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this.filename = filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Creates an ImageIcon from the specified file. The image will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * be preloaded by using MediaTracker to monitor the loading state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * of the image. The specified String can be a file name or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * file path. When specifying a path, use the Internet-standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * forward-slash ("/") as a separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * (The string is converted to an URL, so the forward-slash works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * on all systems.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * For example, specify:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *    new ImageIcon("images/myImage.gif") </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * The description is initialized to the <code>filename</code> string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param filename a String specifying a filename or path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @see #getDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    @ConstructorProperties({"description"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public ImageIcon (String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this(filename, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Creates an ImageIcon from the specified URL. The image will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * be preloaded by using MediaTracker to monitor the loaded state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param location the URL for the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param description a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see #ImageIcon(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public ImageIcon(URL location, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        image = Toolkit.getDefaultToolkit().getImage(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this.location = location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Creates an ImageIcon from the specified URL. The image will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * be preloaded by using MediaTracker to monitor the loaded state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * The icon's description is initialized to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * a string representation of the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param location the URL for the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @see #getDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public ImageIcon (URL location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        this(location, location.toExternalForm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Creates an ImageIcon from the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param image the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param description a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public ImageIcon(Image image, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        this(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Creates an ImageIcon from an image object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * If the image has a "comment" property that is a string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * then the string is used as the description of this icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param image the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @see #getDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @see java.awt.Image#getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public ImageIcon (Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        this.image = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        Object o = image.getProperty("comment", imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (o instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            description = (String) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Creates an ImageIcon from an array of bytes which were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * read from an image file containing a supported image format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * such as GIF, JPEG, or (as of 1.3) PNG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Normally this array is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * by reading an image using Class.getResourceAsStream(), but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * the byte array may also be statically stored in a class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param  imageData an array of pixels in an image format supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *         by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param  description a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @see    java.awt.Toolkit#createImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public ImageIcon (byte[] imageData, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        this.image = Toolkit.getDefaultToolkit().createImage(imageData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Creates an ImageIcon from an array of bytes which were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * read from an image file containing a supported image format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * such as GIF, JPEG, or (as of 1.3) PNG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Normally this array is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * by reading an image using Class.getResourceAsStream(), but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * the byte array may also be statically stored in a class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * If the resulting image has a "comment" property that is a string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * then the string is used as the description of this icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param  imageData an array of pixels in an image format supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *             the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @see    java.awt.Toolkit#createImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see #getDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @see java.awt.Image#getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public ImageIcon (byte[] imageData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        this.image = Toolkit.getDefaultToolkit().createImage(imageData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        Object o = image.getProperty("comment", imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (o instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            description = (String) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Creates an uninitialized image icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public ImageIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Loads the image, returning only when the image is loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param image the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    protected void loadImage(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        MediaTracker mTracker = getTracker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        synchronized(mTracker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            int id = getNextID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            mTracker.addImage(image, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                mTracker.waitForID(id, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                System.out.println("INTERRUPTED while loading Image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            loadStatus = mTracker.statusID(id, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            mTracker.removeImage(image, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            width = image.getWidth(imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            height = image.getHeight(imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Returns an ID to use with the MediaTracker in loading an image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    private int getNextID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        synchronized(getTracker()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return ++mediaTrackerID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Returns the MediaTracker for the current AppContext, creating a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * MediaTracker if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private MediaTracker getTracker() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        Object trackerObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        AppContext ac = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // Opt: Only synchronize if trackerObj comes back null?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // If null, synchronize, re-check for null, and put new tracker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        synchronized(ac) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            trackerObj = ac.get(TRACKER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (trackerObj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                Component comp = new Component() {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                trackerObj = new MediaTracker(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                ac.put(TRACKER_KEY, trackerObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return (MediaTracker) trackerObj;
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
     * Returns the status of the image loading operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @return the loading status as defined by java.awt.MediaTracker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @see java.awt.MediaTracker#ABORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see java.awt.MediaTracker#ERRORED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @see java.awt.MediaTracker#COMPLETE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public int getImageLoadStatus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return loadStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Returns this icon's <code>Image</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return the <code>Image</code> object for this <code>ImageIcon</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   358
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public Image getImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Sets the image displayed by this icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @param image the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public void setImage(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        this.image = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Gets the description of the image.  This is meant to be a brief
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * textual description of the object.  For example, it might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * presented to a blind user to give an indication of the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * The description may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @return a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public String getDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Sets the description of the image.  This is meant to be a brief
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * textual description of the object.  For example, it might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * presented to a blind user to give an indication of the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @param description a brief textual description of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public void setDescription(String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Paints the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * The top-left corner of the icon is drawn at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * the point (<code>x</code>, <code>y</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * in the coordinate space of the graphics context <code>g</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * If this icon has no image observer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * this method uses the <code>c</code> component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * as the observer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param c the component to be used as the observer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *          if this icon has no image observer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param x the X coordinate of the icon's top-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param y the Y coordinate of the icon's top-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if(imageObserver == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
           g.drawImage(image, x, y, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
           g.drawImage(image, x, y, imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Gets the width of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @return the width in pixels of this icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public int getIconWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Gets the height of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @return the height in pixels of this icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public int getIconHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Sets the image observer for the image.  Set this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * property if the ImageIcon contains an animated GIF, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * the observer is notified to update its display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *     icon = new ImageIcon(...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *     button.setIcon(icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *     icon.setImageObserver(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @param observer the image observer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public void setImageObserver(ImageObserver observer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        imageObserver = observer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Returns the image observer for the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @return the image observer, which may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   459
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public ImageObserver getImageObserver() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return imageObserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Returns a string representation of this image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @return a string representing this image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (description != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        int w = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        int h = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        int[] pixels = (int[])(s.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (pixels != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            ColorModel cm = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            loadImage(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        int w = getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        int h = getIconHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        int[] pixels = image != null? new int[w * h] : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (image != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                pg.grabPixels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    throw new IOException("failed to load image contents");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                throw new IOException("image load interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        s.writeInt(w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        s.writeInt(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        s.writeObject(pixels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * --- Accessibility Support ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    private AccessibleImageIcon accessibleContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Gets the AccessibleContext associated with this ImageIcon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * For image icons, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * AccessibleImageIcon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * A new AccessibleImageIcon instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @return an AccessibleImageIcon that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *         AccessibleContext of this ImageIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *  description: The AccessibleContext associated with this ImageIcon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            accessibleContext = new AccessibleImageIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * <code>ImageIcon</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Java Accessibility API appropriate to image icon user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
   558
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    protected class AccessibleImageIcon extends AccessibleContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        implements AccessibleIcon, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         * AccessibleContest implementation -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
         * Gets the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return AccessibleRole.ICON;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * Gets the state of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * @return an instance of AccessibleStateSet containing the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         * state set of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         * @see AccessibleState
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         * Gets the Accessible parent of this object.  If the parent of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * object implements Accessible, this method should simply return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         * getParent().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * @return the Accessible parent of this object -- can be null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         * object does not have an Accessible parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        public Accessible getAccessibleParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * Gets the index of this object in its accessible parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * @return the index of this object in its parent; -1 if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * object does not have an accessible parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * @see #getAccessibleParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        public int getAccessibleIndexInParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            return -1;
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 number of accessible children in the object.  If all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         * of the children of this object implement Accessible, than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         * method should return the number of children of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
         * @return the number of accessible children in the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        public int getAccessibleChildrenCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * Returns the nth Accessible child of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * @param i zero-based index of child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * @return the nth Accessible child of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        public Accessible getAccessibleChild(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
         * Returns the locale of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
         * @return the locale of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        public Locale getLocale() throws IllegalComponentStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
         * AccessibleIcon implementation -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         * Gets the description of the icon.  This is meant to be a brief
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         * textual description of the object.  For example, it might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         * presented to a blind user to give an indication of the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         * of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
         * @return the description of the icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        public String getAccessibleIconDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            return ImageIcon.this.getDescription();
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
         * Sets the description of the icon.  This is meant to be a brief
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         * textual description of the object.  For example, it might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
         * presented to a blind user to give an indication of the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         * of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
         * @param description the description of the icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        public void setAccessibleIconDescription(String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            ImageIcon.this.setDescription(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         * Gets the height of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         * @return the height of the icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        public int getAccessibleIconHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            return ImageIcon.this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         * Gets the width of the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
         * @return the width of the icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        public int getAccessibleIconWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            return ImageIcon.this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }  // AccessibleImageIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
}