src/java.desktop/share/classes/javax/swing/ImageIcon.java

Print this page




  57  * <strong>Warning:</strong>
  58  * Serialized objects of this class will not be compatible with
  59  * future Swing releases. The current serialization support is
  60  * appropriate for short term storage or RMI between applications running
  61  * the same version of Swing.  As of 1.4, support for long term storage
  62  * of all JavaBeans&trade;
  63  * has been added to the <code>java.beans</code> package.
  64  * Please see {@link java.beans.XMLEncoder}.
  65  *
  66  * @author Jeff Dinkins
  67  * @author Lynn Monsanto
  68  * @since 1.2
  69  */
  70 @SuppressWarnings("serial") // Same-version serialization only
  71 public class ImageIcon implements Icon, Serializable, Accessible {
  72     /* Keep references to the filename and location so that
  73      * alternate persistence schemes have the option to archive
  74      * images symbolically rather than including the image data
  75      * in the archive.
  76      */
  77     transient private String filename;
  78     transient private URL location;
  79 
  80     transient Image image;
  81     transient int loadStatus = 0;
  82     ImageObserver imageObserver;
  83     String description = null;
  84 
  85     /**
  86      * Do not use this shared component, which is used to track image loading.
  87      * It is left for backward compatibility only.
  88      * @deprecated since 1.8
  89      */
  90     @Deprecated
  91     protected final static Component component;
  92 
  93     /**
  94      * Do not use this shared media tracker, which is used to load images.
  95      * It is left for backward compatibility only.
  96      * @deprecated since 1.8
  97      */
  98     @Deprecated
  99     protected final static MediaTracker tracker;
 100 
 101     static {
 102         component = AccessController.doPrivileged(new PrivilegedAction<Component>() {
 103             public Component run() {
 104                 try {
 105                     final Component component = createNoPermsComponent();
 106 
 107                     // 6482575 - clear the appContext field so as not to leak it
 108                     Field appContextField =
 109 
 110                             Component.class.getDeclaredField("appContext");
 111                     appContextField.setAccessible(true);
 112                     appContextField.set(component, null);
 113 
 114                     return component;
 115                 } catch (Throwable e) {
 116                     // We don't care about component.
 117                     // So don't prevent class initialisation.
 118                     e.printStackTrace();
 119                     return null;


 127         // 7020198 - set acc field to no permissions and no subject
 128         // Note, will have appContext set.
 129         return AccessController.doPrivileged(
 130                 new PrivilegedAction<Component>() {
 131                     public Component run() {
 132                         return new Component() {
 133                         };
 134                     }
 135                 },
 136                 new AccessControlContext(new ProtectionDomain[]{
 137                         new ProtectionDomain(null, null)
 138                 })
 139         );
 140     }
 141 
 142     /**
 143      * Id used in loading images from MediaTracker.
 144      */
 145     private static int mediaTrackerID;
 146 
 147     private final static Object TRACKER_KEY = new StringBuilder("TRACKER_KEY");
 148 
 149     int width = -1;
 150     int height = -1;
 151 
 152     /**
 153      * Creates an ImageIcon from the specified file. The image will
 154      * be preloaded by using MediaTracker to monitor the loading state
 155      * of the image.
 156      * @param filename the name of the file containing the image
 157      * @param description a brief textual description of the image
 158      * @see #ImageIcon(String)
 159      */
 160     public ImageIcon(String filename, String description) {
 161         image = Toolkit.getDefaultToolkit().getImage(filename);
 162         if (image == null) {
 163             return;
 164         }
 165         this.filename = filename;
 166         this.description = description;
 167         loadImage(image);




  57  * <strong>Warning:</strong>
  58  * Serialized objects of this class will not be compatible with
  59  * future Swing releases. The current serialization support is
  60  * appropriate for short term storage or RMI between applications running
  61  * the same version of Swing.  As of 1.4, support for long term storage
  62  * of all JavaBeans&trade;
  63  * has been added to the <code>java.beans</code> package.
  64  * Please see {@link java.beans.XMLEncoder}.
  65  *
  66  * @author Jeff Dinkins
  67  * @author Lynn Monsanto
  68  * @since 1.2
  69  */
  70 @SuppressWarnings("serial") // Same-version serialization only
  71 public class ImageIcon implements Icon, Serializable, Accessible {
  72     /* Keep references to the filename and location so that
  73      * alternate persistence schemes have the option to archive
  74      * images symbolically rather than including the image data
  75      * in the archive.
  76      */
  77     private transient String filename;
  78     private transient URL location;
  79 
  80     transient Image image;
  81     transient int loadStatus = 0;
  82     ImageObserver imageObserver;
  83     String description = null;
  84 
  85     /**
  86      * Do not use this shared component, which is used to track image loading.
  87      * It is left for backward compatibility only.
  88      * @deprecated since 1.8
  89      */
  90     @Deprecated
  91     protected static final Component component;
  92 
  93     /**
  94      * Do not use this shared media tracker, which is used to load images.
  95      * It is left for backward compatibility only.
  96      * @deprecated since 1.8
  97      */
  98     @Deprecated
  99     protected static final MediaTracker tracker;
 100 
 101     static {
 102         component = AccessController.doPrivileged(new PrivilegedAction<Component>() {
 103             public Component run() {
 104                 try {
 105                     final Component component = createNoPermsComponent();
 106 
 107                     // 6482575 - clear the appContext field so as not to leak it
 108                     Field appContextField =
 109 
 110                             Component.class.getDeclaredField("appContext");
 111                     appContextField.setAccessible(true);
 112                     appContextField.set(component, null);
 113 
 114                     return component;
 115                 } catch (Throwable e) {
 116                     // We don't care about component.
 117                     // So don't prevent class initialisation.
 118                     e.printStackTrace();
 119                     return null;


 127         // 7020198 - set acc field to no permissions and no subject
 128         // Note, will have appContext set.
 129         return AccessController.doPrivileged(
 130                 new PrivilegedAction<Component>() {
 131                     public Component run() {
 132                         return new Component() {
 133                         };
 134                     }
 135                 },
 136                 new AccessControlContext(new ProtectionDomain[]{
 137                         new ProtectionDomain(null, null)
 138                 })
 139         );
 140     }
 141 
 142     /**
 143      * Id used in loading images from MediaTracker.
 144      */
 145     private static int mediaTrackerID;
 146 
 147     private static final Object TRACKER_KEY = new StringBuilder("TRACKER_KEY");
 148 
 149     int width = -1;
 150     int height = -1;
 151 
 152     /**
 153      * Creates an ImageIcon from the specified file. The image will
 154      * be preloaded by using MediaTracker to monitor the loading state
 155      * of the image.
 156      * @param filename the name of the file containing the image
 157      * @param description a brief textual description of the image
 158      * @see #ImageIcon(String)
 159      */
 160     public ImageIcon(String filename, String description) {
 161         image = Toolkit.getDefaultToolkit().getImage(filename);
 162         if (image == null) {
 163             return;
 164         }
 165         this.filename = filename;
 166         this.description = description;
 167         loadImage(image);