home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1LZIHNE (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  6.2 KB  |  213 lines

  1. package com.sun.java.swing;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.MediaTracker;
  7. import java.awt.Toolkit;
  8. import java.awt.image.ColorModel;
  9. import java.awt.image.ImageObserver;
  10. import java.awt.image.MemoryImageSource;
  11. import java.awt.image.PixelGrabber;
  12. import java.io.IOException;
  13. import java.io.ObjectInputStream;
  14. import java.io.ObjectOutputStream;
  15. import java.io.Serializable;
  16. import java.net.URL;
  17.  
  18. public class ImageIcon implements Icon, Serializable {
  19.    transient Image image;
  20.    transient int loadStatus;
  21.    ImageObserver imageObserver;
  22.    String description;
  23.    protected static final Component component = new 1();
  24.    protected static final MediaTracker tracker;
  25.    int width;
  26.    int height;
  27.  
  28.    static {
  29.       tracker = new MediaTracker(component);
  30.    }
  31.  
  32.    public ImageIcon() {
  33.       this.loadStatus = 0;
  34.       this.description = null;
  35.       this.width = -1;
  36.       this.height = -1;
  37.    }
  38.  
  39.    public ImageIcon(byte[] imageData) {
  40.       this.loadStatus = 0;
  41.       this.description = null;
  42.       this.width = -1;
  43.       this.height = -1;
  44.       this.image = Toolkit.getDefaultToolkit().createImage(imageData);
  45.       if (this.image != null) {
  46.          Object o = this.image.getProperty("comment", this.imageObserver);
  47.          if (o instanceof String) {
  48.             this.description = (String)o;
  49.          }
  50.  
  51.          this.loadImage(this.image);
  52.       }
  53.    }
  54.  
  55.    public ImageIcon(byte[] imageData, String description) {
  56.       this.loadStatus = 0;
  57.       this.description = null;
  58.       this.width = -1;
  59.       this.height = -1;
  60.       this.image = Toolkit.getDefaultToolkit().createImage(imageData);
  61.       if (this.image != null) {
  62.          this.description = description;
  63.          this.loadImage(this.image);
  64.       }
  65.    }
  66.  
  67.    public ImageIcon(Image image) {
  68.       this.loadStatus = 0;
  69.       this.description = null;
  70.       this.width = -1;
  71.       this.height = -1;
  72.       this.image = image;
  73.       Object o = image.getProperty("comment", this.imageObserver);
  74.       if (o instanceof String) {
  75.          this.description = (String)o;
  76.       }
  77.  
  78.       this.loadImage(image);
  79.    }
  80.  
  81.    public ImageIcon(Image image, String description) {
  82.       this(image);
  83.       this.description = description;
  84.    }
  85.  
  86.    public ImageIcon(String filename) {
  87.       this(filename, filename);
  88.    }
  89.  
  90.    public ImageIcon(String filename, String description) {
  91.       this.loadStatus = 0;
  92.       this.description = null;
  93.       this.width = -1;
  94.       this.height = -1;
  95.       this.image = Toolkit.getDefaultToolkit().getImage(filename);
  96.       this.description = description;
  97.       this.loadImage(this.image);
  98.    }
  99.  
  100.    public ImageIcon(URL location) {
  101.       this(location, location.toExternalForm());
  102.    }
  103.  
  104.    public ImageIcon(URL location, String description) {
  105.       this.loadStatus = 0;
  106.       this.description = null;
  107.       this.width = -1;
  108.       this.height = -1;
  109.       this.image = Toolkit.getDefaultToolkit().getImage(location);
  110.       this.description = description;
  111.       this.loadImage(this.image);
  112.    }
  113.  
  114.    public String getDescription() {
  115.       return this.description;
  116.    }
  117.  
  118.    public int getIconHeight() {
  119.       return this.height;
  120.    }
  121.  
  122.    public int getIconWidth() {
  123.       return this.width;
  124.    }
  125.  
  126.    public Image getImage() {
  127.       return this.image;
  128.    }
  129.  
  130.    public int getImageLoadStatus() {
  131.       return this.loadStatus;
  132.    }
  133.  
  134.    public ImageObserver getImageObserver() {
  135.       return this.imageObserver;
  136.    }
  137.  
  138.    protected void loadImage(Image image) {
  139.       synchronized(tracker){}
  140.  
  141.       try {
  142.          tracker.addImage(image, 0);
  143.  
  144.          try {
  145.             tracker.waitForID(0, 5000L);
  146.          } catch (InterruptedException var5) {
  147.             System.out.println("INTERRUPTED while loading Image");
  148.          }
  149.  
  150.          this.loadStatus = tracker.statusID(0, false);
  151.          tracker.removeImage(image, 0);
  152.          this.width = image.getWidth(this.imageObserver);
  153.          this.height = image.getHeight(this.imageObserver);
  154.       } catch (Throwable var6) {
  155.          throw var6;
  156.       }
  157.  
  158.    }
  159.  
  160.    public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
  161.       if (this.imageObserver == null) {
  162.          g.drawImage(this.image, x, y, c);
  163.       } else {
  164.          g.drawImage(this.image, x, y, this.imageObserver);
  165.       }
  166.  
  167.    }
  168.  
  169.    private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  170.       s.defaultReadObject();
  171.       int w = s.readInt();
  172.       int h = s.readInt();
  173.       int[] pixels = (int[])s.readObject();
  174.       Toolkit tk = Toolkit.getDefaultToolkit();
  175.       ColorModel cm = ColorModel.getRGBdefault();
  176.       this.image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w));
  177.    }
  178.  
  179.    public void setDescription(String description) {
  180.       this.description = description;
  181.    }
  182.  
  183.    public void setImage(Image image) {
  184.       this.image = image;
  185.       this.loadImage(image);
  186.    }
  187.  
  188.    public void setImageObserver(ImageObserver observer) {
  189.       this.imageObserver = observer;
  190.    }
  191.  
  192.    private void writeObject(ObjectOutputStream s) throws IOException {
  193.       s.defaultWriteObject();
  194.       int w = this.getIconWidth();
  195.       int h = this.getIconHeight();
  196.       int[] pixels = new int[w * h];
  197.  
  198.       try {
  199.          PixelGrabber pg = new PixelGrabber(this.image, 0, 0, w, h, pixels, 0, w);
  200.          pg.grabPixels();
  201.          if ((pg.getStatus() & 128) != 0) {
  202.             throw new IOException("failed to load image contents");
  203.          }
  204.       } catch (InterruptedException var6) {
  205.          throw new IOException("image load interrupted");
  206.       }
  207.  
  208.       s.writeInt(w);
  209.       s.writeInt(h);
  210.       s.writeObject(pixels);
  211.    }
  212. }
  213.