import java.awt.*;


public class MyBouncingBall
    extends java.applet.Applet implements Runnable {

  // Set four objects of graphics: an Oval, a Rect, a RoundRectRing and a circle
  protected Color colorF, // drawing area frame color
				  color1 = Color.red,
				  color2 = Color.blue,
				  color3 = Color.green,
				  color4 = Color.orange;
  protected int radius1X = 30, radius1Y = 20,
				radius2X = 15, radius2Y = 15,
				radius3X = 20, radius3Y = 35,
				radius4  = 10;
  protected int x1, y1, x2, y2, x3, y3, x4, y4;
  protected int dx1 = 5, dy1 = 15,
				dx2 = -20, dy2 = -10,
				dx3 = -4,  dy3 = -4,
				dx4 = 15, dy4 = 0;
  protected Image image;
  protected Graphics offscreen;
  protected Dimension d;

  public void init() {
    String att = getParameter("delay");
    if (att != null) {
      delay = Integer.parseInt(att);
    }
    d = getSize();
    x1 = d.width * 2 / 3;
    y1 = d.height - radius1Y;
	x2 = d.width / 2;
	y2 = d.height * 3 / 4;
	x3 = d.width - radius3X * 2;
	y3 = d.height -radius3Y * 2;
	x4 = radius4 +3;
	y4 = radius4 +3;
		

	//get the frame color from PARAM
	String param = getParameter("color");
	if ("cyan".equals(param)) 
		colorF = Color.cyan;
	else if ("magenta".equals(param)) 
		colorF = Color.magenta;
	else if ("orange".equals(param)) 
		colorF = Color.orange;
	else if ("pink".equals(param)) 
		colorF = Color.pink;
	else
		colorF = Color.yellow;
  }


  public void update(Graphics g) {
    // create the off-screen image buffer
    // if it is invoked the first time
    if (image == null) {
      image = createImage(d.width, d.height);
      offscreen = image.getGraphics();
    }

    // draw the background	
    offscreen.setColor(Color.black);
    offscreen.fillRect(0,0,d.width,d.height);
    offscreen.setColor(colorF);
    offscreen.drawRect(0,0,d.width-1,d.height-1);

    // adjust the position of the ball
    // reverse the direction if it touches
    // any of the four sides
    if (x1 < radius1X+3 || x1 > d.width-3 - radius1X) {
      dx1  =  -dx1;
    }
    if (y1 < radius1Y+3 || y1 > d.height-3 - radius1Y) {
      dy1  =  -dy1;
    }
	if (x2 < radius2X+3 || x2 > d.width-3 - radius2X) {
      dx2  =  -dx2;
    }
    if (y2 < radius2Y+3 || y2 > d.height-3 - radius2Y) {
      dy2  =  -dy2;
    }
	if (x3 < radius3X+3 || x3 > d.width-3 - radius3X) {
      dx3  =  -dx3;
    }
    if (y3 < radius3Y+3 || y3 > d.height-3 - radius3Y) {
      dy3  =  -dy3;
    }

    x1 += dx1;
    y1 += dy1;
	x2 += dx2;
    y2 += dy2;
	x3 += dx3;
    y3 += dy3;
	x4 += dx4;
	y4 += dy4;

	if (x4 < radius4+3 ) {
	  x4  =  radius4+3;
	  dx4 =  0;
	  dy4 =  -10;
    }
	if (x4 > d.width-3 - radius4) {
	  x4  =  d.width-3 - radius4;
	  dx4 =  0;
	  dy4 =  10;
    }
    if (y4 < radius4+3 ) {
	  y4  =  radius4+3;
	  dx4 =  15;
	  dy4 =  0;
    }
	if (y4 > d.height-3 - radius4) {
      y4  =  d.height-3 - radius4;
	  dx4 =  -15;
	  dy4 =  0;
    }
    // draw the oval
    offscreen.setColor(color1);
    offscreen.fillOval(x1 - radius1X, y1 - radius1Y,
                           radius1X * 2, radius1Y * 2);
	 // draw the rectangle
    offscreen.setColor(color2);
	if ((dx2<0)&&(dy2<0)||(dx2>0)&&(dy2>0)) //draw the box
		offscreen.fillRect(x2 - radius2X, y2 - radius2Y,
                           radius2X * 2, radius2Y * 2);
	else
		offscreen.fillOval(x2 - radius2X, y2 - radius2Y,
                           radius2X * 2, radius2Y * 2);
	 // draw the roundrectangle ring
    offscreen.setColor(color3);
    offscreen.drawRoundRect(x3 - radius3X, y3 - radius3Y,
                           radius3X * 2, radius3Y * 2,
						   20, 20);
	 // draw the circle
    offscreen.setColor(color4);
    offscreen.drawOval(x4 - radius4, y4 - radius4,
                           radius4 * 2, radius4 * 2);

    // copy the off-screen image to the screen
    g.drawImage(image, 0, 0, this);
  }

  public void paint(Graphics g) {
    update(g);
  }

  // The animation applet idiom
  protected Thread bouncingThread;
  protected int delay = 100;

  public void start() {
    bouncingThread = new Thread(this);
    bouncingThread.start();
  }

  public void stop() {
    bouncingThread = null;
  }

  public void run() {
    while (Thread.currentThread() == bouncingThread) {
      try {
        Thread.currentThread().sleep(delay);
      } catch  (InterruptedException  e) {}
        repaint();
      }
    }
}
