import java.awt.*;

public class BouncingBall2 extends DBAnimationApplet {

  public BouncingBall2() {
    super(true);  // double buffering
  }

  protected void initAnimator() {
    String att = getParameter("delay");
    if (att != null)
      setDelay(Integer.parseInt(att));
    x1 = d.width * 2 / 3 ;
    y1 = d.height - radius;
    x2 = x1 - 110;
    y2 = d.height/2;
  }


  protected void paintFrame(Graphics g) {
    g.setColor(Color.white);
    g.fillRect(0,0,d.width,d.height);
    if (Math.sqrt(Math.pow((x1-x2),2) + Math.pow((y1-y2),2))<=radius * 2) {
		int temp;
		temp = dx1;
		dx1 = dx2; dx2 = temp;
		temp = dy1;
		dy1 = dy2; dy2 = temp;
    }
    if (x1 < radius || x1 > d.width - radius) {
      dx1 = -dx1;
    }

    if (y1 < radius || y1 > d.height - radius) {
      dy1  =  -dy1;
    }
    x1 += dx1; y1 += dy1;
    if (x2 < radius || x2 > d.width - radius) {
	      dx2 = -dx2;
	}
    if (y2 < radius || y2 > d.height - radius) {
	      dy2  =  -dy2;
	}
	x2 += dx2; y2 += dy2;

    g.setColor(color1);
    g.fillOval(x1 - radius, y1 - radius, radius * 2, radius * 2);
    g.setColor(color2);
	g.fillOval(x2 - radius, y2 - radius, radius * 2, radius * 2);

  }

  protected int x1, y1, x2, y2;
  protected int dx1 = -2, dy1 = -4;
  protected int dx2 = 4, dy2 = -8;
  protected int radius = 20;
  protected Color color1 = Color.green;
  protected Color color2 = Color.red;
}
