import java.awt.*;
import java.awt.image.*;
import java.lang.Math;

class Sprite extends Raster {
  int x, y;  //current position of sprite (upper left-hand corner)
  int width, height;  //size of sprite's frame

  public Sprite () {
  } //empty constructor allows for future extension

  public Sprite (Image image) {
    //initialize sprite with an image
    super(image);
  }

  boolean notTransparent(int pixel) {
    /**
     *PURPOSE:this procedure is used to determine if a pixel is transparent or not
     *it was made a procedure to make the Draw code more readable
     *FUNCTION: Each pixel has 32 relevant bits (4 bytes)
     *The first byte represents the opacity of the pixel;
     *an opacity of 0 is transparent.
     *To check if the the first byte (bits 24-31) of the pixel is transparent,
     *right shift by 24 and then see if this value is transparent
     */
    return ((pixel>>>24) != 0);
  }
  
  public void Draw(Raster bgnd) { 
    /**Draws the sprite on the background Raster bgnd
     *In the simplest case the Sprite is contained completely on the background
     *and all we need to do is copy the Sprite bits onto to the background 
     *(except for transparent bits, of course).  However, if part of the sprite
     *is out of the field of view (the background), then we don't want to try
     *to draw these pixels.  The limits in the for loops make sure that we
     *don't draw the pixels that are out of bound
     */
    for (int j= Math.max(y,0);   //the minimum y pixel to draw
	 j<= Math.min((y+this.height), bgnd.getHeight());  //the max y pixel to draw
	 j++) {
      for (int i= Math.max(x,0);   //the min x pixel to draw
	   i<= Math.min((x+this.width), bgnd.getWidth());  //the max x pixel to draw
	   i++) {
	if (notTransparent(this.getPixel(i-x,j-y)))  
	  //only draw the nontransparent pixels
	  bgnd.setPixel(this.getPixel(i-x,j-y), i, j);
      }
    }
  }
  
}

