
import java.awt.*;

import javax.swing.*;

public class BouncingBallCanvas 
  extends JPanel { 

  public BouncingBallCanvas() {
    super(true); // double-buffered 
  }

  public void initCanvas() {
	  d = getSize(); 
	    x = d.width * 2 / 3 ;
	    y = d.height - radius;
  System.err.println(Integer.toString(d.width) + " " + Integer.toString(d.height));
  }
 
  public void paint(Graphics g) {
	  if (sizeNotSet){
		  d = getSize(); 
		    x = d.width * 2 / 3 ;
		    y = d.height - radius;
	    System.err.println(Integer.toString(d.width) + " " + Integer.toString(d.height));
	    sizeNotSet = false;
	  }
	System.err.println("painting");
    g.setColor(Color.black);
    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);
    if (BouncingBall3.gfxImage != null) {
    	Image is = BouncingBall3.gfxImage.getScaledInstance(2*radius, 2*radius, Image.SCALE_FAST);
    	g.drawImage(is, x-radius, y-radius, x+radius, y+radius,
    			0, 0, 2*radius, 2*radius, null);
    	//g.drawRect(x-radius, y-radius, 2*radius, 2*radius);
    } else {
    	g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
    }
  } 

  public void setBallColor(Color c) {
    ballcolor = c; 
  }

  public void setBallPosition(int x, int y) {
    this.x = x; this.y = y; 
  }

  public void changeRadius (int amount) {
	  radius = radius + amount;
	  if (radius <= 0)
		  radius = 5;
  }
  
  protected int x, y, dx = -2, dy = -4, radius = 20; 
  protected Color ballcolor = Color.red;
  protected Dimension d;  
  protected Boolean sizeNotSet = true;

}
