import java.awt.*;
import java.awt.image.*;
//
//
// Raster
//
//
class Raster {
    public int width, height;
    public int pixel[], originalpixel[];
	DirectColorModel dcm;
	Image img;

    ///////////////////////// 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)
    {
        try {
            PixelGrabber grabber = new PixelGrabber(img, 0, 0, -1, -1, true);
            if (grabber.grabPixels()) {
                width = grabber.getWidth();
                height = grabber.getHeight();
                pixel = (int []) grabber.getPixels();
				//originalpixel = (int []) grabber.getPixels();
				
				originalpixel = new int[pixel.length];

				copyArray(pixel, originalpixel);
            }

			dcm = (DirectColorModel)grabber.getColorModel(); 
        } catch (InterruptedException e) {
        }
    }

	void copyArray(int[] p1, int[] p2){
		for(int i = 0; i<p1.length;i++){
			p2[i] = p1[i];
		}

	}

	void renderImage(Component root){
		img = toImage(root);
	}

	Image getRenderedImage(){
		return img;
	}

	public Raster(int pixels[], DirectColorModel dcm, int width, int height){
		pixel = pixels;
		originalpixel = pixels;
		this.dcm = dcm;
		this.width = width;

		System.out.println("width Raster: " + Integer.toString(width));
        this.height = height;

	}


	void resetImage(){
		for(int i = 0; i<pixel.length;i++){
			//if(pixel[i] != originalpixel[i]) System.out.println("Pixels not are equal");
			pixel[i] = originalpixel[i];

		}
		//System.out.println("resetImage()");
	}


    ////////////////////////// Methods //////////////////////////

    /**
     *  Returns the number of pixels in the Raster
     */
    public final int size( )
    {
        return pixel.length;
    }

    /**
     *  Fills a Raster with a solid color
     */
    public final void fill(Color c)
    {
        int s = size();
        int rgb = c.getRGB();
        for (int i = 0; i < s; i++)
            pixel[i] = rgb;
    }

	//sets current image, needed for animated sprite

    /**
     *  Converts Rasters to Images
     */
    public final Image toImage(Component root)
    {
        return root.createImage(new MemoryImageSource(width, height, pixel, 0, width));
    }

    /**
     *  Gets a pixel from a Raster
     */
    public final int getPixel(int x, int y)
    {
		if(x >= width || y>= height || x < 0 || y < 0) {
			//System.out.println("Clipping");
			return -1;	//to avoid clipping issues

		}
        return pixel[y*width+x];
    }

    /**
     *  Gets a color from a Raster
     */
    public final Color getColor(int x, int y)
    {
        return new Color(pixel[y*width+x]);
    }

    /**
     *  Sets a pixel to a given value
     */
    public final boolean setPixel(int pix, int x, int y)
    {
		if(x >= width || y>=height || x < 0 || y < 0) {
			//System.out.println("Clipping");
			return true;	//to avoid clipping issues

		}

		if(y*width+x > pixel.length) System.out.println("*"+Integer.toString(x)+","+Integer.toString(y));
        pixel[y*width+x] = pix;
		//if(pixel[y*width+x] == originalpixel[y*width+x]) System.out.println("Matching Prob");
        return true;
    }

    /**
     *  Sets a pixel to a given color
     */
    public final boolean setColor(Color c, int x, int y)
    {	
		if(x >= width || y>=height || x < 0 || y < 0) {
			//System.out.println("Clipping");
			return true;	//to avoid clipping issues

		}
		if(y*width+x > pixel.length){ 
			try{
			System.out.println("*"+Integer.toString(x)+","+Integer.toString(y));
			Thread.sleep(5000);
			}catch(Exception e){
				System.out.println(e);
			}
		}
      
        pixel[y*width+x] = c.getRGB();
        return true;
    }
}