import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;

import java.awt.image.ImageObserver;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;

class   Sprite 
extends Raster
{
    public int x; // Position of the sprite in the PlayField
    public int y; // Position of the sprite in the PlayField

    /**
     *  This constructor which takes no arguments
     *  allows for future extension.
     */
    public Sprite() 
    {
    }


    /**
     *  This constructor creates an uninitialized
     *  Raster Object of a given size (w x h).
     */
    public Sprite( int w, int h )
    {
        super( w, h );
    }


    /**
     *  This constructor creates an Raster initialized
     *  with the contents of an image.
     */
    public Sprite( Image img )
    {
        super( img );
    }


    /**
     *  Draw the sprite on the input raster.
     */
    public Rectangle[]
    draw( Raster raster )
    {
        for ( int i=0; i<height; i++ ) {
            for( int j=0; j<width; j++ ) {
                if ( !isTransparent( pixel[i*width+j] ) ) {
                    raster.setPixel( pixel[i*width+j], x+j, y+i );
                }
            }
        }
        
        Rectangle result[] = new Rectangle[1];
        
        result[0] = new Rectangle( x, y, width, height );
        
        return result;
    }


    /**
     *  Test if the pixel is transparent. White pixels are 
     *  assumed to be transparent.
     */
    public boolean
    isTransparent( int pixel )
    {
        if ( (pixel & 0x00FFFFFF) == 0x00FFFFFF ) {
            return true;
        } else {
            return false;
        }
    }


    /**
     *  Check if the pixel at position p on the playfield is 
     *  a part of this sprite or not.
     */
    public boolean
    isInside( Point p )
    {
        int a = p.x-x;
        int b = p.y-y;
        
        System.out.println( "Inside got relative point " + a + ", " + b );

        return ( a >=0 ) && ( a < width ) && ( b >=0 ) && ( b < height ) &&
               !isTransparent( getPixel( p.x-x, p.y-y ) );
    }
}
