import java.awt.*;
import java.awt.image.*;
import Raster;

/**
 * This is the Sprite class derived from Raster.
 * (c)Hongsong Chou for 6.837(F98) project 1
 * It can be compiled by javac on athena.
 */

class Sprite extends Raster {
  int x, y;//upper-left corner of Sprite frame

  boolean isSpriteTransp(int xx, int yy) {
    //This private method seraches transparent pixels in Sprite 
    if((pixel[yy*width+xx] & 0xff000000 ) == 0)
      return true;
    else
      return false;
  }
  
  public Sprite(Image image) {
    super(image);
  }

  public void setXY(int x, int y) {
    this.x = x;
    this.y = y - height/2;//reset upper-left corner from mouse position
  }
  
  public void Draw(Raster bgnd) {
    //where to put the sprite
    int bgnd_w = bgnd.getWidth();
    int bgnd_h = bgnd.getHeight();
    for(int j = 0; j < bgnd_h; j++)
      for(int i = 0; i < bgnd_w; i++)
	{
	  if( i < x + width && i >= x && j < y + height && j >= y )
	    //then, overlaps
	    {
	      if( !isSpriteTransp(i-x , j-y) )
		bgnd.setPixel(pixel[(j-y)*width+(i-x)],i,j);
	    }
	}
  }  

  public int getWidth() {
    return width;
  }

  public int getHeight() {
    return height;
  }
}    
    
    
    
