home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / sun / awt / image / Image.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  5.8 KB  |  291 lines

  1. package sun.awt.image;
  2.  
  3. import java.awt.image.ImageObserver;
  4. import java.awt.image.ImageProducer;
  5. import java.util.Enumeration;
  6. import java.util.Hashtable;
  7.  
  8. public abstract class Image extends java.awt.Image {
  9.    ImageProducer source;
  10.    InputStreamImageSource src;
  11.    ImageInfoGrabber info;
  12.    private int width = -1;
  13.    private int height = -1;
  14.    private Hashtable properties;
  15.    private int availinfo;
  16.    Hashtable representations = new Hashtable();
  17.  
  18.    protected Image() {
  19.    }
  20.  
  21.    protected Image(int w, int h) {
  22.       OffScreenImageSource osis = new OffScreenImageSource(w, h);
  23.       this.source = osis;
  24.       this.width = w;
  25.       this.height = h;
  26.       this.availinfo |= 3;
  27.       ImageRepresentation ir = this.getImageRep(-1, -1);
  28.       this.representations.put(w + "x" + h, ir);
  29.       this.representations.put("-1x-1", ir);
  30.       ir.setDimensions(w, h);
  31.       ir.offscreenInit();
  32.       osis.setImageRep(ir);
  33.    }
  34.  
  35.    protected Image(ImageProducer is) {
  36.       this.source = is;
  37.       if (is instanceof InputStreamImageSource) {
  38.          this.src = (InputStreamImageSource)is;
  39.       }
  40.  
  41.       this.info = new ImageInfoGrabber(this);
  42.       this.info.setupConsumer();
  43.    }
  44.  
  45.    public ImageProducer getSource() {
  46.       if (this.src != null) {
  47.          this.src.checkSecurity((Object)null, false);
  48.       }
  49.  
  50.       return this.source;
  51.    }
  52.  
  53.    public int getWidth() {
  54.       if (this.src != null) {
  55.          this.src.checkSecurity((Object)null, false);
  56.       }
  57.  
  58.       if ((this.availinfo & 1) == 0) {
  59.          this.reconstruct(1);
  60.       }
  61.  
  62.       return this.width;
  63.    }
  64.  
  65.    public synchronized int getWidth(ImageObserver iw) {
  66.       if (this.src != null) {
  67.          this.src.checkSecurity((Object)null, false);
  68.       }
  69.  
  70.       if ((this.availinfo & 1) == 0) {
  71.          this.addWatcher(iw, true);
  72.          return -1;
  73.       } else {
  74.          return this.width;
  75.       }
  76.    }
  77.  
  78.    public int getHeight() {
  79.       if (this.src != null) {
  80.          this.src.checkSecurity((Object)null, false);
  81.       }
  82.  
  83.       if ((this.availinfo & 2) == 0) {
  84.          this.reconstruct(2);
  85.       }
  86.  
  87.       return this.height;
  88.    }
  89.  
  90.    public synchronized int getHeight(ImageObserver iw) {
  91.       if (this.src != null) {
  92.          this.src.checkSecurity((Object)null, false);
  93.       }
  94.  
  95.       if ((this.availinfo & 2) == 0) {
  96.          this.addWatcher(iw, true);
  97.          return -1;
  98.       } else {
  99.          return this.height;
  100.       }
  101.    }
  102.  
  103.    public Object getProperty(String name, ImageObserver observer) {
  104.       if (this.src != null) {
  105.          this.src.checkSecurity((Object)null, false);
  106.       }
  107.  
  108.       if (this.properties == null) {
  109.          this.addWatcher(observer, true);
  110.          return null;
  111.       } else {
  112.          Object o = this.properties.get(name);
  113.          if (o == null) {
  114.             o = java.awt.Image.UndefinedProperty;
  115.          }
  116.  
  117.          return o;
  118.       }
  119.    }
  120.  
  121.    public boolean hasError() {
  122.       if (this.src != null) {
  123.          this.src.checkSecurity((Object)null, false);
  124.       }
  125.  
  126.       return (this.availinfo & 64) != 0;
  127.    }
  128.  
  129.    public int check(ImageObserver iw) {
  130.       if (this.src != null) {
  131.          this.src.checkSecurity((Object)null, false);
  132.       }
  133.  
  134.       if ((this.availinfo & 64) == 0 && (~this.availinfo & 7) != 0) {
  135.          this.addWatcher(iw, false);
  136.       }
  137.  
  138.       return this.availinfo;
  139.    }
  140.  
  141.    public void preload(ImageObserver iw) {
  142.       if (this.src != null) {
  143.          this.src.checkSecurity((Object)null, false);
  144.       }
  145.  
  146.       if ((this.availinfo & 32) == 0) {
  147.          this.addWatcher(iw, true);
  148.       }
  149.  
  150.    }
  151.  
  152.    private synchronized void addWatcher(ImageObserver iw, boolean load) {
  153.       if ((this.availinfo & 64) != 0) {
  154.          if (iw != null) {
  155.             iw.imageUpdate(this, 192, -1, -1, -1, -1);
  156.          }
  157.  
  158.       } else {
  159.          if (this.info == null) {
  160.             this.info = new ImageInfoGrabber(this);
  161.             this.info.setupConsumer();
  162.          }
  163.  
  164.          this.info.addWatcher(iw);
  165.          if (load) {
  166.             this.info.getInfo();
  167.          }
  168.  
  169.       }
  170.    }
  171.  
  172.    private synchronized void reconstruct(int flags) {
  173.       if ((flags & ~this.availinfo) != 0) {
  174.          if ((this.availinfo & 64) != 0) {
  175.             return;
  176.          }
  177.  
  178.          if (this.info == null) {
  179.             this.info = new ImageInfoGrabber(this);
  180.          }
  181.  
  182.          this.info.getInfo();
  183.  
  184.          while((flags & ~this.availinfo) != 0) {
  185.             try {
  186.                this.wait();
  187.             } catch (InterruptedException var2) {
  188.                Thread.currentThread().interrupt();
  189.                return;
  190.             }
  191.  
  192.             if ((this.availinfo & 64) != 0) {
  193.                return;
  194.             }
  195.          }
  196.       }
  197.  
  198.    }
  199.  
  200.    synchronized void addInfo(int newinfo) {
  201.       this.availinfo |= newinfo;
  202.       this.notifyAll();
  203.       if ((~this.availinfo & 7) == 0) {
  204.          this.info.stopInfo();
  205.       }
  206.  
  207.    }
  208.  
  209.    void setDimensions(int w, int h) {
  210.       this.width = w;
  211.       this.height = h;
  212.       this.addInfo(3);
  213.    }
  214.  
  215.    void setProperties(Hashtable props) {
  216.       if (props == null) {
  217.          props = new Hashtable();
  218.       }
  219.  
  220.       this.properties = props;
  221.       this.addInfo(4);
  222.    }
  223.  
  224.    synchronized void infoDone(int status) {
  225.       if (status != 1 && (~this.availinfo & 3) == 0) {
  226.          if ((this.availinfo & 4) == 0) {
  227.             this.setProperties((Hashtable)null);
  228.          }
  229.  
  230.       } else {
  231.          this.addInfo(64);
  232.       }
  233.    }
  234.  
  235.    public void flush() {
  236.       if (this.src != null) {
  237.          this.src.checkSecurity((Object)null, false);
  238.       }
  239.  
  240.       if (!(this.source instanceof OffScreenImageSource)) {
  241.          synchronized(this){}
  242.  
  243.          Enumeration enum;
  244.          try {
  245.             this.availinfo &= -65;
  246.             enum = this.representations.elements();
  247.             this.representations = new Hashtable();
  248.             if (this.source instanceof InputStreamImageSource) {
  249.                ((InputStreamImageSource)this.source).flush();
  250.             }
  251.          } catch (Throwable var4) {
  252.             throw var4;
  253.          }
  254.  
  255.          while(enum.hasMoreElements()) {
  256.             ImageRepresentation ir = (ImageRepresentation)enum.nextElement();
  257.             ir.abort();
  258.          }
  259.       }
  260.  
  261.    }
  262.  
  263.    protected ImageRepresentation getImageRep(int w, int h) {
  264.       if (this.src != null) {
  265.          this.src.checkSecurity((Object)null, false);
  266.       }
  267.  
  268.       String key = w + "x" + h;
  269.       ImageRepresentation ir = (ImageRepresentation)this.representations.get(key);
  270.       if (ir == null) {
  271.          synchronized(this){}
  272.  
  273.          try {
  274.             ir = (ImageRepresentation)this.representations.get(key);
  275.             if (ir == null) {
  276.                ir = new ImageRepresentation(this, w, h, 0);
  277.                if (this.source instanceof OffScreenImageSource) {
  278.                   ir.offscreen = true;
  279.                } else {
  280.                   this.representations.put(key, ir);
  281.                }
  282.             }
  283.          } catch (Throwable var7) {
  284.             throw var7;
  285.          }
  286.       }
  287.  
  288.       return ir;
  289.    }
  290. }
  291.