import java.awt.*;
import java.awt.event.*;

public class BouncingBall3 extends java.applet.Applet {
  public BouncingBall3() {
    setLayout(new BorderLayout());
    myCanvas = new BouncingBallCanvas();
    System.out.println("BouncingBall3 - prefered size of canvas before adding to layout = " + myCanvas.getPreferredSize());
    System.out.println("BouncingBall3 - min size of canvas before adding to layout = " + myCanvas.getMinimumSize());
    System.out.println("BouncingBall3 - max size of canvas before adding to layout = " + myCanvas.getMaximumSize());
    add("Center", myCanvas); 
    animator = new Animator(myCanvas); 
    Dimension d; 
    
    controlPanel = new Panel(); 
    titlePanel = new Panel();
    titleLabel = new Label("XXXXX");
    titlePanel.add(titleLabel);
    controlPanel.setLayout(new GridLayout(1,0));     
    Button startButton = new Button("start");
    controlPanel.add(startButton);
    Button stopButton = new Button("stop");
    controlPanel.add(stopButton);
    Choice choice = new Choice();
    choice.addItem("red");
    choice.addItem("green");
    choice.addItem("blue");
    controlPanel.add(choice); 
    add("South", controlPanel);
    
    add("North", titlePanel); 
    
    startButton.addActionListener(new ButtonHandler(new StartCommand())); 
    stopButton.addActionListener(new ButtonHandler(new StopCommand())); 
    choice.addItemListener(new ColorChoiceHandler());
     
  }
   
  public void init() {
    
    // get the parameters
    System.out.println("Hw3.init - getting HTML paramaters");
    String newSpeed = getParameter("speed");
	String newTitle = getParameter("title");

	// set the parameter values
	if (newTitle == null) {
		// parameter not provided
		System.out.println("Hw3.init - setting default title");
	  	titleLabel.setText(DEFAULT_TITLE);
	} else {
	    System.out.println("Hw3.init - setting title = " + newTitle);
		titleLabel.setText(newTitle);
	}
	//titleLabel.repaint();

	if (newSpeed == null){
		// parameter not provided
		System.out.println("Hw3.init - settting default speed");
		animator.setDelay(DEFAULT_SPEED);
	} else {
		try {
		    System.out.println("Hw3.init - setting speed");
			animator.setDelay(Integer.parseInt(newSpeed));
		} catch (NumberFormatException e){
		    System.out.println("Hw3.init - invalid speed");
		    System.out.println("Hw3.init - settting default speed");
		    animator.setDelay(DEFAULT_SPEED);
		}
	}
    myCanvas.initCanvas(); 
  }
  public void start() {
    animator.start(); 
  }
  public void stop() {
    animator.stop(); 
  }
   
  protected BouncingBallCanvas myCanvas;
  protected Animator animator; 
  protected Panel controlPanel; 
  protected Panel titlePanel;
  protected Label titleLabel;
  private static final int DEFAULT_SPEED = 100;
  private static final String DEFAULT_TITLE = "No Title Provided!!";
  
  protected class ButtonHandler implements ActionListener {  
    private Command cmd; 
      
    public ButtonHandler(Command cmd) {
      this.cmd = cmd;     
    }
       
    public void actionPerformed(ActionEvent event) { 
      if (cmd != null) 
        cmd.execute(); 
    }
  }
  protected class StartCommand implements Command {
    public void execute() {
      start(); 
    }
  }
  protected class StopCommand implements Command {
    public void execute() {
      stop(); 
    }
  }
  protected class ColorChoiceHandler implements ItemListener {
    public void itemStateChanged(ItemEvent event)  {    
      Choice choice = (Choice) event.getSource();  
      if (choice != null) {
        if ("red".equals(event.getItem()))
          myCanvas.setBallColor(Color.red); 
        else if ("green".equals(event.getItem()))
          myCanvas.setBallColor(Color.green); 
        else if ("blue".equals(event.getItem()))
          myCanvas.setBallColor(Color.blue); 
        myCanvas.repaint();
      }
    }
  }
   
}
