import java.awt.*;

public class BouncingBallCanvas 
    extends Canvas implements DoubleBufferedComponent {

  public BouncingBallCanvas() {
    System.out.println("BouncingBallCanvas constructor - starting");
    dbhandler = new DoubleBufferHandler(this); 
  }

  public void initCanvas() {
    System.out.println("BouncingBallCanvas.initCanvas - getting size");
    // hack to get around a bug?? in jdk1.1.8
    setSize(300,148);
    System.out.println("BouncingBallCanvas.initCanvas - prefered size of canvas = " + getPreferredSize());
    System.out.println("BouncingBallCanvas.initCanvas - min size of canvas = " + getMinimumSize());
        System.out.println("BouncingBallCanvas.initCanvas - max size of canvas = " + getMaximumSize());
    d = getSize(); 
    System.out.println("BouncingBallCanvas.initCanvas - size = " + d.toString());
    x = d.width * 2 / 3 ;
    y = d.height - radius;
    System.out.println("BouncingBallCanvas.initCanvas - starting location = " + x + " " + y);
  }

  public void update(Graphics g) {
    System.out.println("BouncingBallCanvas.update - calling dbhandler.update()");
    dbhandler.update(g); 
  } 

  public void paint(Graphics g) {
    System.out.println("BouncingBallCanvas.paint - starting");
    update(g); 
  } 

  public void paintFrame(Graphics g) {
    System.out.println("BouncingBallCanvas.paintframe - starting");
    g.setColor(Color.white);
    g.fillRect(0, 0, d.width, d.height); 
    if (x < radius || x > d.width - radius)
      dx = -dx; 
    if (y < radius || y > d.height - radius)
      dy = -dy; 
    x += dx; y += dy; 
    g.setColor(ballcolor);
    g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
  } 

  public void setBallColor(Color c) {
    System.out.println("BouncingBallCanvas.setBallColor - starting");
    ballcolor = c; 
  }

  public void setBallPosition(int x, int y) {
    System.out.println("BouncingBallCanvas.setBallPosition - starting");
    this.x = x; this.y = y; 
  }

  protected int x, y, dx = -2, dy = -4, radius = 20; 
  protected Color ballcolor = Color.red;
  protected Dimension d;  
  protected DoubleBufferHandler dbhandler; 
}
