home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / ImageIcon.java < prev    next >
Text File  |  1998-02-26  |  10KB  |  330 lines

  1. /*
  2.  * @(#)ImageIcon.java    1.26 98/02/06
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22. import java.awt.*;
  23. import java.awt.image.*;
  24. import java.net.URL;
  25.  
  26. import java.io.Serializable;
  27. import java.io.ObjectOutputStream;
  28. import java.io.ObjectInputStream;
  29. import java.io.IOException;
  30.  
  31.  
  32. /**
  33.  * An implementation of the Icon interface that paints Icons
  34.  * from Images. Images that are created from a URL or filename
  35.  * are preloaded using MediaTracker to monitor the loaded state
  36.  * of the image.
  37.  * <p>
  38.  * Warning: serialized objects of this class will not be compatible with
  39.  * future swing releases.  The current serialization support is appropriate 
  40.  * for short term storage or RMI between Swing1.0 applications.  It will
  41.  * not be possible to load serialized Swing1.0 objects with future releases
  42.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  43.  * baseline for the serialized form of Swing objects.
  44.  * 
  45.  * @version 1.26 02/06/98
  46.  * @author Jeff Dinkins
  47.  */
  48. public class ImageIcon implements Icon, Serializable
  49. {
  50.     transient Image image;
  51.     transient int loadStatus = 0;
  52.     ImageObserver imageObserver;
  53.     String description = null;
  54.  
  55.     protected final static Component component = new Component() {};
  56.     protected final static MediaTracker tracker = new MediaTracker(component);
  57.  
  58.     int width = -1;
  59.     int height = -1;
  60.  
  61.     /**
  62.      * Creates an ImageIcon from the specified file. The image will
  63.      * be preloaded by using MediaTracker to monitor the loading state
  64.      * of the image.
  65.      * @param filename the name of the file containing the image
  66.      * @param description a brief textual description of the image
  67.      */
  68.     public ImageIcon(String filename, String description) {
  69.     image = Toolkit.getDefaultToolkit().getImage(filename);
  70.         this.description = description;
  71.     loadImage(image);
  72.     }
  73.  
  74.     /**
  75.      * Creates an ImageIcon from the specified file. The image will
  76.      * be preloaded by using MediaTracker to monitor the loading state
  77.      * of the image.
  78.      */
  79.     public ImageIcon (String filename) {
  80.         this(filename, filename);
  81.     }
  82.  
  83.     /**
  84.      * Creates an ImageIcon from the specified URL. The image will
  85.      * be preloaded by using MediaTracker to monitor the loaded state
  86.      * of the image.
  87.      * @param URL the URL for the image
  88.      * @param description a brief textual description of the image
  89.      */
  90.     public ImageIcon(URL location, String description) {
  91.     image = Toolkit.getDefaultToolkit().getImage(location);
  92.         this.description = description;
  93.     loadImage(image);
  94.     }
  95.  
  96.     /**
  97.      * Creates an ImageIcon from the specified URL. The image will
  98.      * be preloaded by using MediaTracker to monitor the loaded state
  99.      * of the image.
  100.      */
  101.     public ImageIcon (URL location) {
  102.         this(location, location.toExternalForm());
  103.     }
  104.  
  105.     /**
  106.      * Creates an ImageIcon from the image. 
  107.      * @param image the image
  108.      * @param description a brief textual description of the image
  109.      */
  110.     public ImageIcon(Image image, String description) {
  111.         this(image);
  112.         this.description = description;
  113.     }
  114.  
  115.     /**
  116.      * Creates an ImageIcon from the image. 
  117.      */
  118.     public ImageIcon (Image image) {
  119.     this.image = image;
  120.         Object o = image.getProperty("comment", imageObserver);
  121.         if (o instanceof String) {
  122.             description = (String) o;
  123.         }
  124.     loadImage(image);
  125.     }
  126.  
  127.     /**
  128.      * Creates an ImageIcon from an array of bytes which were
  129.      * read from an image file containing a supported image format,
  130.      * such as GIF or JPEG.  Normally this array is created
  131.      * by reading an image using Class.getResourceAsStream(), but
  132.      * the byte array may also be statically stored in a class.
  133.      *
  134.      * @param  imageData an array of pixels in an image format supported
  135.      *         by the AWT Toolkit, such as GIF or JPEG.
  136.      * @param  description a brief textual description of the image
  137.      * @see    java.awt.Toolkit#createImage
  138.      */
  139.     public ImageIcon (byte[] imageData, String description) {
  140.     this.image = Toolkit.getDefaultToolkit().createImage(imageData);
  141.         if (image == null) {
  142.             return;
  143.         }
  144.         this.description = description;
  145.     loadImage(image);
  146.     }
  147.  
  148.     /**
  149.      * Creates an ImageIcon from an array of bytes which were
  150.      * read from an image file containing a supported image format,
  151.      * such as GIF or JPEG.  Normally this array is created
  152.      * by reading an image using Class.getResourceAsStream(), but
  153.      * the byte array may also be statically stored in a class.
  154.      *
  155.      * @param  an array of pixels in an image format supported by
  156.      *         the AWT Toolkit, such as GIF or JPEG.
  157.      * @see    java.awt.Toolkit#createImage
  158.      */
  159.     public ImageIcon (byte[] imageData) {
  160.     this.image = Toolkit.getDefaultToolkit().createImage(imageData);
  161.         if (image == null) {
  162.             return;
  163.         }
  164.         Object o = image.getProperty("comment", imageObserver);
  165.         if (o instanceof String) {
  166.             description = (String) o;
  167.         }
  168.     loadImage(image);
  169.     }
  170.  
  171.     /**
  172.      * Creates an uninitialized image icon.
  173.      */
  174.     public ImageIcon() {
  175.     }
  176.  
  177.     /**
  178.      * Wait for the image to load
  179.      */
  180.     protected void loadImage(Image image) {
  181.     synchronized(tracker) {
  182.         tracker.addImage(image, 0);
  183.         try {
  184.         tracker.waitForID(0, 5000);
  185.         } catch (InterruptedException e) {
  186.         System.out.println("INTERRUPTED while loading Image");
  187.         }
  188.             loadStatus = tracker.statusID(0, false);
  189.         tracker.removeImage(image, 0);
  190.  
  191.         width = image.getWidth(imageObserver);
  192.         height = image.getHeight(imageObserver);
  193.     }
  194.     }
  195.  
  196.     /**
  197.      * Returns the status of the image loading operation.
  198.      * @return the loading status as defined by java.awt.MediaTracker.
  199.      * @see java.awt.MediaTracker#ABORTED
  200.      * @see java.awt.MediaTracker#ERRORED
  201.      * @see java.awt.MediaTracker#COMPLETE
  202.      */
  203.     public int getImageLoadStatus() {
  204.         return loadStatus;
  205.     }
  206.  
  207.     /**
  208.      * Returns the Icon's Image
  209.      */
  210.     public Image getImage() {
  211.     return image;
  212.     }
  213.  
  214.     /**
  215.      * Set the image displayed by this icon.
  216.      */
  217.     public void setImage(Image image) {
  218.     this.image = image;
  219.     loadImage(image);
  220.     }
  221.  
  222.     /**
  223.      * Get the description of the image.  This is meant to be a brief
  224.      * textual description of the object.  For example, it might be
  225.      * presented to a blind user to give an indication of the purpose
  226.      * of the image.
  227.      */
  228.     public String getDescription() {
  229.     return description;
  230.     }
  231.  
  232.     /**
  233.      * Set the description of the image.  This is meant to be a brief
  234.      * textual description of the object.  For example, it might be
  235.      * presented to a blind user to give an indication of the purpose
  236.      * of the image.
  237.      */
  238.     public void setDescription(String description) {
  239.     this.description = description;
  240.     }
  241.  
  242.     /**
  243.      * Paints the Icon
  244.      */
  245.     public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
  246.         if(imageObserver == null) {
  247.            g.drawImage(image, x, y, c);
  248.         } else {
  249.        g.drawImage(image, x, y, imageObserver);
  250.         }
  251.     }
  252.  
  253.     /**
  254.      * Get the width of the Icon
  255.      */
  256.     public int getIconWidth() {
  257.     return width;
  258.     }
  259.  
  260.     /**
  261.      * Get the height of the Icon
  262.      */
  263.     public int getIconHeight() {
  264.     return height;
  265.     }
  266.  
  267.     /** 
  268.      * Set the image observer for the image.  Set this
  269.      * property if the ImageIcon contains an animated GIF.
  270.      * For example:
  271.      * <pre>
  272.      *     icon = new ImageIcon(...)
  273.      *     button.setImage(icon);
  274.      *     icon.setImageObserver(button);
  275.      * </pre>
  276.      */
  277.     public void setImageObserver(ImageObserver observer) {
  278.         imageObserver = observer;
  279.     }
  280.  
  281.     /**
  282.      *  Return the umage observer for the image 
  283.      */
  284.     public ImageObserver getImageObserver() {
  285.         return imageObserver;
  286.     }
  287.  
  288.  
  289.     private void readObject(ObjectInputStream s)
  290.     throws ClassNotFoundException, IOException 
  291.     {
  292.     s.defaultReadObject();
  293.     
  294.     int w = s.readInt();
  295.     int h = s.readInt();
  296.     int[] pixels = (int[])(s.readObject());
  297.  
  298.     Toolkit tk = Toolkit.getDefaultToolkit();
  299.     ColorModel cm = ColorModel.getRGBdefault();
  300.     image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w));
  301.     }
  302.  
  303.  
  304.     private void writeObject(ObjectOutputStream s) 
  305.     throws IOException 
  306.     {
  307.     s.defaultWriteObject();
  308.  
  309.     int w = getIconWidth();
  310.     int h = getIconHeight();
  311.     int[] pixels = new int[w * h];
  312.  
  313.     try {
  314.         PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
  315.         pg.grabPixels();
  316.         if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
  317.         throw new IOException("failed to load image contents");
  318.         }
  319.     }
  320.     catch (InterruptedException e) {
  321.         throw new IOException("image load interrupted");
  322.     }
  323.     
  324.     s.writeInt(w);
  325.     s.writeInt(h);
  326.     s.writeObject(pixels);
  327.     }
  328. }
  329.  
  330.