< prev index next >

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

Print this page




  43 import java.security.*;
  44 
  45 /**
  46  * An implementation of the Icon interface that paints Icons
  47  * from Images. Images that are created from a URL, filename or byte array
  48  * are preloaded using MediaTracker to monitor the loaded state
  49  * of the image.
  50  *
  51  * <p>
  52  * For further information and examples of using image icons, see
  53  * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html">How to Use Icons</a>
  54  * in <em>The Java Tutorial.</em>
  55  *
  56  * <p>
  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;


 161         image = Toolkit.getDefaultToolkit().getImage(filename);
 162         if (image == null) {
 163             return;
 164         }
 165         this.filename = filename;
 166         this.description = description;
 167         loadImage(image);
 168     }
 169 
 170     /**
 171      * Creates an ImageIcon from the specified file. The image will
 172      * be preloaded by using MediaTracker to monitor the loading state
 173      * of the image. The specified String can be a file name or a
 174      * file path. When specifying a path, use the Internet-standard
 175      * forward-slash ("/") as a separator.
 176      * (The string is converted to an URL, so the forward-slash works
 177      * on all systems.)
 178      * For example, specify:
 179      * <pre>
 180      *    new ImageIcon("images/myImage.gif") </pre>
 181      * The description is initialized to the <code>filename</code> string.
 182      *
 183      * @param filename a String specifying a filename or path
 184      * @see #getDescription
 185      */
 186     @ConstructorProperties({"description"})
 187     public ImageIcon (String filename) {
 188         this(filename, filename);
 189     }
 190 
 191     /**
 192      * Creates an ImageIcon from the specified URL. The image will
 193      * be preloaded by using MediaTracker to monitor the loaded state
 194      * of the image.
 195      * @param location the URL for the image
 196      * @param description a brief textual description of the image
 197      * @see #ImageIcon(String)
 198      */
 199     public ImageIcon(URL location, String description) {
 200         image = Toolkit.getDefaultToolkit().getImage(location);
 201         if (image == null) {


 349                 Component comp = new Component() {};
 350                 trackerObj = new MediaTracker(comp);
 351                 ac.put(TRACKER_KEY, trackerObj);
 352             }
 353         }
 354         return (MediaTracker) trackerObj;
 355     }
 356 
 357     /**
 358      * Returns the status of the image loading operation.
 359      * @return the loading status as defined by java.awt.MediaTracker
 360      * @see java.awt.MediaTracker#ABORTED
 361      * @see java.awt.MediaTracker#ERRORED
 362      * @see java.awt.MediaTracker#COMPLETE
 363      */
 364     public int getImageLoadStatus() {
 365         return loadStatus;
 366     }
 367 
 368     /**
 369      * Returns this icon's <code>Image</code>.
 370      * @return the <code>Image</code> object for this <code>ImageIcon</code>
 371      */
 372     @Transient
 373     public Image getImage() {
 374         return image;
 375     }
 376 
 377     /**
 378      * Sets the image displayed by this icon.
 379      * @param image the image
 380      */
 381     public void setImage(Image image) {
 382         this.image = image;
 383         loadImage(image);
 384     }
 385 
 386     /**
 387      * Gets the description of the image.  This is meant to be a brief
 388      * textual description of the object.  For example, it might be
 389      * presented to a blind user to give an indication of the purpose
 390      * of the image.


 393      * @return a brief textual description of the image
 394      */
 395     public String getDescription() {
 396         return description;
 397     }
 398 
 399     /**
 400      * Sets the description of the image.  This is meant to be a brief
 401      * textual description of the object.  For example, it might be
 402      * presented to a blind user to give an indication of the purpose
 403      * of the image.
 404      * @param description a brief textual description of the image
 405      */
 406     public void setDescription(String description) {
 407         this.description = description;
 408     }
 409 
 410     /**
 411      * Paints the icon.
 412      * The top-left corner of the icon is drawn at
 413      * the point (<code>x</code>, <code>y</code>)
 414      * in the coordinate space of the graphics context <code>g</code>.
 415      * If this icon has no image observer,
 416      * this method uses the <code>c</code> component
 417      * as the observer.
 418      *
 419      * @param c the component to be used as the observer
 420      *          if this icon has no image observer
 421      * @param g the graphics context
 422      * @param x the X coordinate of the icon's top-left corner
 423      * @param y the Y coordinate of the icon's top-left corner
 424      */
 425     public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
 426         if(imageObserver == null) {
 427            g.drawImage(image, x, y, c);
 428         } else {
 429            g.drawImage(image, x, y, imageObserver);
 430         }
 431     }
 432 
 433     /**
 434      * Gets the width of the icon.
 435      *
 436      * @return the width in pixels of this icon


 564      * For image icons, the AccessibleContext takes the form of an
 565      * AccessibleImageIcon.
 566      * A new AccessibleImageIcon instance is created if necessary.
 567      *
 568      * @return an AccessibleImageIcon that serves as the
 569      *         AccessibleContext of this ImageIcon
 570      * @beaninfo
 571      *       expert: true
 572      *  description: The AccessibleContext associated with this ImageIcon.
 573      * @since 1.3
 574      */
 575     public AccessibleContext getAccessibleContext() {
 576         if (accessibleContext == null) {
 577             accessibleContext = new AccessibleImageIcon();
 578         }
 579         return accessibleContext;
 580     }
 581 
 582     /**
 583      * This class implements accessibility support for the
 584      * <code>ImageIcon</code> class.  It provides an implementation of the
 585      * Java Accessibility API appropriate to image icon user-interface
 586      * elements.
 587      * <p>
 588      * <strong>Warning:</strong>
 589      * Serialized objects of this class will not be compatible with
 590      * future Swing releases. The current serialization support is
 591      * appropriate for short term storage or RMI between applications running
 592      * the same version of Swing.  As of 1.4, support for long term storage
 593      * of all JavaBeans&trade;
 594      * has been added to the <code>java.beans</code> package.
 595      * Please see {@link java.beans.XMLEncoder}.
 596      * @since 1.3
 597      */
 598     @SuppressWarnings("serial") // Same-version serialization only
 599     protected class AccessibleImageIcon extends AccessibleContext
 600         implements AccessibleIcon, Serializable {
 601 
 602         /*
 603          * AccessibleContest implementation -----------------
 604          */
 605 
 606         /**
 607          * Gets the role of this object.
 608          *
 609          * @return an instance of AccessibleRole describing the role of the
 610          * object
 611          * @see AccessibleRole
 612          */
 613         public AccessibleRole getAccessibleRole() {
 614             return AccessibleRole.ICON;




  43 import java.security.*;
  44 
  45 /**
  46  * An implementation of the Icon interface that paints Icons
  47  * from Images. Images that are created from a URL, filename or byte array
  48  * are preloaded using MediaTracker to monitor the loaded state
  49  * of the image.
  50  *
  51  * <p>
  52  * For further information and examples of using image icons, see
  53  * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html">How to Use Icons</a>
  54  * in <em>The Java Tutorial.</em>
  55  *
  56  * <p>
  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} 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;


 161         image = Toolkit.getDefaultToolkit().getImage(filename);
 162         if (image == null) {
 163             return;
 164         }
 165         this.filename = filename;
 166         this.description = description;
 167         loadImage(image);
 168     }
 169 
 170     /**
 171      * Creates an ImageIcon from the specified file. The image will
 172      * be preloaded by using MediaTracker to monitor the loading state
 173      * of the image. The specified String can be a file name or a
 174      * file path. When specifying a path, use the Internet-standard
 175      * forward-slash ("/") as a separator.
 176      * (The string is converted to an URL, so the forward-slash works
 177      * on all systems.)
 178      * For example, specify:
 179      * <pre>
 180      *    new ImageIcon("images/myImage.gif") </pre>
 181      * The description is initialized to the {@code filename} string.
 182      *
 183      * @param filename a String specifying a filename or path
 184      * @see #getDescription
 185      */
 186     @ConstructorProperties({"description"})
 187     public ImageIcon (String filename) {
 188         this(filename, filename);
 189     }
 190 
 191     /**
 192      * Creates an ImageIcon from the specified URL. The image will
 193      * be preloaded by using MediaTracker to monitor the loaded state
 194      * of the image.
 195      * @param location the URL for the image
 196      * @param description a brief textual description of the image
 197      * @see #ImageIcon(String)
 198      */
 199     public ImageIcon(URL location, String description) {
 200         image = Toolkit.getDefaultToolkit().getImage(location);
 201         if (image == null) {


 349                 Component comp = new Component() {};
 350                 trackerObj = new MediaTracker(comp);
 351                 ac.put(TRACKER_KEY, trackerObj);
 352             }
 353         }
 354         return (MediaTracker) trackerObj;
 355     }
 356 
 357     /**
 358      * Returns the status of the image loading operation.
 359      * @return the loading status as defined by java.awt.MediaTracker
 360      * @see java.awt.MediaTracker#ABORTED
 361      * @see java.awt.MediaTracker#ERRORED
 362      * @see java.awt.MediaTracker#COMPLETE
 363      */
 364     public int getImageLoadStatus() {
 365         return loadStatus;
 366     }
 367 
 368     /**
 369      * Returns this icon's {@code Image}.
 370      * @return the {@code Image} object for this {@code ImageIcon}
 371      */
 372     @Transient
 373     public Image getImage() {
 374         return image;
 375     }
 376 
 377     /**
 378      * Sets the image displayed by this icon.
 379      * @param image the image
 380      */
 381     public void setImage(Image image) {
 382         this.image = image;
 383         loadImage(image);
 384     }
 385 
 386     /**
 387      * Gets the description of the image.  This is meant to be a brief
 388      * textual description of the object.  For example, it might be
 389      * presented to a blind user to give an indication of the purpose
 390      * of the image.


 393      * @return a brief textual description of the image
 394      */
 395     public String getDescription() {
 396         return description;
 397     }
 398 
 399     /**
 400      * Sets the description of the image.  This is meant to be a brief
 401      * textual description of the object.  For example, it might be
 402      * presented to a blind user to give an indication of the purpose
 403      * of the image.
 404      * @param description a brief textual description of the image
 405      */
 406     public void setDescription(String description) {
 407         this.description = description;
 408     }
 409 
 410     /**
 411      * Paints the icon.
 412      * The top-left corner of the icon is drawn at
 413      * the point ({@code x}, {@code y})
 414      * in the coordinate space of the graphics context {@code g}.
 415      * If this icon has no image observer,
 416      * this method uses the {@code c} component
 417      * as the observer.
 418      *
 419      * @param c the component to be used as the observer
 420      *          if this icon has no image observer
 421      * @param g the graphics context
 422      * @param x the X coordinate of the icon's top-left corner
 423      * @param y the Y coordinate of the icon's top-left corner
 424      */
 425     public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
 426         if(imageObserver == null) {
 427            g.drawImage(image, x, y, c);
 428         } else {
 429            g.drawImage(image, x, y, imageObserver);
 430         }
 431     }
 432 
 433     /**
 434      * Gets the width of the icon.
 435      *
 436      * @return the width in pixels of this icon


 564      * For image icons, the AccessibleContext takes the form of an
 565      * AccessibleImageIcon.
 566      * A new AccessibleImageIcon instance is created if necessary.
 567      *
 568      * @return an AccessibleImageIcon that serves as the
 569      *         AccessibleContext of this ImageIcon
 570      * @beaninfo
 571      *       expert: true
 572      *  description: The AccessibleContext associated with this ImageIcon.
 573      * @since 1.3
 574      */
 575     public AccessibleContext getAccessibleContext() {
 576         if (accessibleContext == null) {
 577             accessibleContext = new AccessibleImageIcon();
 578         }
 579         return accessibleContext;
 580     }
 581 
 582     /**
 583      * This class implements accessibility support for the
 584      * {@code ImageIcon} class.  It provides an implementation of the
 585      * Java Accessibility API appropriate to image icon user-interface
 586      * elements.
 587      * <p>
 588      * <strong>Warning:</strong>
 589      * Serialized objects of this class will not be compatible with
 590      * future Swing releases. The current serialization support is
 591      * appropriate for short term storage or RMI between applications running
 592      * the same version of Swing.  As of 1.4, support for long term storage
 593      * of all JavaBeans&trade;
 594      * has been added to the {@code java.beans} package.
 595      * Please see {@link java.beans.XMLEncoder}.
 596      * @since 1.3
 597      */
 598     @SuppressWarnings("serial") // Same-version serialization only
 599     protected class AccessibleImageIcon extends AccessibleContext
 600         implements AccessibleIcon, Serializable {
 601 
 602         /*
 603          * AccessibleContest implementation -----------------
 604          */
 605 
 606         /**
 607          * Gets the role of this object.
 608          *
 609          * @return an instance of AccessibleRole describing the role of the
 610          * object
 611          * @see AccessibleRole
 612          */
 613         public AccessibleRole getAccessibleRole() {
 614             return AccessibleRole.ICON;


< prev index next >