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

  1. package java.awt.image;
  2.  
  3. import java.util.Hashtable;
  4.  
  5. public class MemoryImageSource implements ImageProducer {
  6.    int width;
  7.    int height;
  8.    ColorModel model;
  9.    Object pixels;
  10.    int pixeloffset;
  11.    int pixelscan;
  12.    Hashtable properties;
  13.    private ImageConsumer theConsumer;
  14.  
  15.    public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, int scan) {
  16.       this.initialize(w, h, cm, pix, off, scan, (Hashtable)null);
  17.    }
  18.  
  19.    public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, int scan, Hashtable props) {
  20.       this.initialize(w, h, cm, pix, off, scan, props);
  21.    }
  22.  
  23.    public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, int scan) {
  24.       this.initialize(w, h, cm, pix, off, scan, (Hashtable)null);
  25.    }
  26.  
  27.    public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, int scan, Hashtable props) {
  28.       this.initialize(w, h, cm, pix, off, scan, props);
  29.    }
  30.  
  31.    private void initialize(int w, int h, ColorModel cm, Object pix, int off, int scan, Hashtable props) {
  32.       this.width = w;
  33.       this.height = h;
  34.       this.model = cm;
  35.       this.pixels = pix;
  36.       this.pixeloffset = off;
  37.       this.pixelscan = scan;
  38.       if (props == null) {
  39.          props = new Hashtable();
  40.       }
  41.  
  42.       this.properties = props;
  43.    }
  44.  
  45.    public MemoryImageSource(int w, int h, int[] pix, int off, int scan) {
  46.       this.initialize(w, h, ColorModel.getRGBdefault(), pix, off, scan, (Hashtable)null);
  47.    }
  48.  
  49.    public MemoryImageSource(int w, int h, int[] pix, int off, int scan, Hashtable props) {
  50.       this.initialize(w, h, ColorModel.getRGBdefault(), pix, off, scan, props);
  51.    }
  52.  
  53.    public synchronized void addConsumer(ImageConsumer ic) {
  54.       this.theConsumer = ic;
  55.  
  56.       try {
  57.          this.produce();
  58.       } catch (Exception var2) {
  59.          if (this.theConsumer != null) {
  60.             this.theConsumer.imageComplete(1);
  61.          }
  62.       }
  63.  
  64.       this.theConsumer = null;
  65.    }
  66.  
  67.    public synchronized boolean isConsumer(ImageConsumer ic) {
  68.       return ic == this.theConsumer;
  69.    }
  70.  
  71.    public synchronized void removeConsumer(ImageConsumer ic) {
  72.       if (this.theConsumer == ic) {
  73.          this.theConsumer = null;
  74.       }
  75.  
  76.    }
  77.  
  78.    public void startProduction(ImageConsumer ic) {
  79.       this.addConsumer(ic);
  80.    }
  81.  
  82.    public void requestTopDownLeftRightResend(ImageConsumer ic) {
  83.    }
  84.  
  85.    private void produce() {
  86.       if (this.theConsumer != null) {
  87.          this.theConsumer.setDimensions(this.width, this.height);
  88.       }
  89.  
  90.       if (this.theConsumer != null) {
  91.          this.theConsumer.setProperties(this.properties);
  92.       }
  93.  
  94.       if (this.theConsumer != null) {
  95.          this.theConsumer.setColorModel(this.model);
  96.       }
  97.  
  98.       if (this.theConsumer != null) {
  99.          this.theConsumer.setHints(30);
  100.       }
  101.  
  102.       if (this.theConsumer != null) {
  103.          if (this.pixels instanceof byte[]) {
  104.             this.theConsumer.setPixels(0, 0, this.width, this.height, this.model, (byte[])this.pixels, this.pixeloffset, this.pixelscan);
  105.          } else {
  106.             this.theConsumer.setPixels(0, 0, this.width, this.height, this.model, (int[])this.pixels, this.pixeloffset, this.pixelscan);
  107.          }
  108.       }
  109.  
  110.       if (this.theConsumer != null) {
  111.          this.theConsumer.imageComplete(3);
  112.       }
  113.  
  114.    }
  115. }
  116.