import java.applet.*;
import java.awt.*;
public class BouncingBitmap extends BitmapSprite implements Moveable {

  // the coords at which the bitmap bounces
  protected int max_width;
  protected int max_height;

  // sprite velocity. used to implement Moveable interface
  protected int vx;
  protected int vy;

  public BouncingBitmap(int x,int y,Image i,Applet a,int max_w,int max_h) {
    super(x,y,i,a);
    max_width = max_w;
    max_height = max_h;
  }

  public void setPosition(int x,int y) {
   locx = x;
   locy = y;
  }

  public void setVelocity(int x,int y) {
    vx = x;
    vy = y;
  }

  // update position according to velocity
  public void updatePosition() {
    locx += vx;
    locy += vy;
  }

  // move and bounce bitmap if it hits borders
  public void update() {

    // flip x velocity if it hits left or right bound
    if ((locx + width > max_width) || locx < 0) {
      vx = -vx;
    }

    // flip y velocity if it hits top or bottom bound
    if ((locy + height > max_height) || locy < 0) {
      vy = -vy;
    }
    updatePosition();
  }

}
