import java.awt.*; import java.awt.image.*; import java.util.*; class Raster implements ImageConsumer { public int width, height; public int pixel[]; private ImageProducer producer; ///////////////////////// Constructors ////////////////////// /** * This constructor which takes no arguments * allows for future extension. */ public Raster() { } /** * This constructor creates an uninitialized * Raster Object of a given size (w x h). */ public Raster(int w, int h) { width = w; height = h; pixel = new int[w*h]; } /** * This constructor creates an Raster initialized * with the contents of an image. */ public Raster(Image img) { producer = img.getSource(); producer.addConsumer(this); producer.startProduction(this); waitForImage(); } public synchronized void waitForImage() { try { wait( ); } catch (Exception e) { }; } public synchronized void imageComplete(int status) { producer.removeConsumer(this); notifyAll(); } public void setProperties(Hashtable props) { } public void setColorModel(ColorModel model) { } public void setHints(int hintflags) { } public void setDimensions(int w, int h) { width = w; height = h; pixel = new int[w*h]; } public void setPixels(int x, int y, int w, int h, ColorModel cm, byte p[], int offset, int stride) { int i, j, delta; i = y*width+x; delta = width - w; while (h > 0) { for (j = 0; j < w; j++) { pixel[i++] = cm.getRGB(p[offset+j] & 255); } offset += stride; i += delta; h -= 1; } } public void setPixels(int x, int y, int w, int h, ColorModel cm, int p[], int offset, int stride) { int i, j, delta; i = y*width+x; delta = width - w; while (h > 0) { for (j = 0; j < w; j++) { pixel[i++] = cm.getRGB(p[offset+j]); } offset += stride; i += delta; h -= 1; } } ////////////////////////// Methods ////////////////////////// /** * Returns the number of pixels in the Raster */ public final int size( ) { return width*height; } /** * Fills a Raster with a solid color * 9/24/96 : removed final from method definiton */ public void fill(Color c) { int s = pixel.length; int rgb = c.getRGB(); for (int i = 0; i < s; i++) pixel[i] = rgb; } /** * Converts Rasters to Images * 9/2/96 : removed final from method definiton */ public Image toImage(Component root) { return root.createImage(new MemoryImageSource(width, height, pixel, 0, width)); } /** * Gets a pixel from a Raster * 9/24/96 : removed final from method definiton */ public int getPixel(int x, int y) { return pixel[y*width+x]; } /** * Gets a color from a Raster * 9/24/96 : removed final from method definiton */ public Color getColor(int x, int y) { return new Color(pixel[y*width+x]); } /** * Sets a pixel to a given value * 9/24/96 : removed final from method definiton */ public boolean setPixel(int pix, int x, int y) { pixel[y*width+x] = pix; return true; } /** * Sets a pixel to a given color * 9/24/96 : removed final from method definiton */ public boolean setColor(Color c, int x, int y) { pixel[y*width+x] = c.getRGB(); return true; } }