import java.awt.*;
import java.awt.event.*;
public class BouncingBall8b extends java.applet.Applet 
     implements Runnable 
{
  protected BouncingBallCanvasa canvas; 
  protected Panel controlPanel; 
  
  protected static final int START = 1;
  protected static final int STOP = 2;
  protected static final int FAST = 3;
  protected static final int SLOW = 4;

  Thread bouncingThread;
  int delay = 400;
  protected class StartStopButtonHandler implements ActionListener 
  {
    private int cmd = 0; 
    public StartStopButtonHandler(int cmd)
    {
      this.cmd = cmd;
    }
    public void actionPerformed(ActionEvent event) 
    {
      switch (cmd) 
      {     
        case START: start();
        break;
        case STOP: stop();
        break; 
        case FAST: fast();
        break;
        case SLOW: slow();
        break; 
      }   
    }
  }
  protected class ObjectChoiceHandler implements ItemListener 
  {
    public void itemStateChanged(ItemEvent event)
    {    
      Choice choice = (Choice) event.getSource();  
      if (choice != null) {
       if ("Background".equals(event.getItem())) { 
         canvas.setObject(2);   
       } 
       else if ("Ball".equals(event.getItem())) 
       {
          canvas.setObject(0); 
       } 
       else if ("Rectangle".equals(event.getItem()))
       {
          canvas.setObject(1); 
       } 
      }
    }
  }

 protected class ColorChoiceHandler implements ItemListener 
  {
    public void itemStateChanged(ItemEvent event)
    {    
      Choice choice1 = (Choice) event.getSource();  
      if (choice1 != null) {
       if ("red".equals(event.getItem())) { 
         canvas.setColor(Color.red);   
       } 
       else if ("green".equals(event.getItem())) 
       {
          canvas.setColor(Color.green); 
       } 
       else if ("blue".equals(event.getItem()))
       {
          canvas.setColor(Color.blue); 
       } 
       else if ("cyan".equals(event.getItem())) 
       {
          canvas.setColor(Color.cyan); 
       } 
       else if ("orange".equals(event.getItem()))
       {
          canvas.setColor(Color.orange); 
       } 
       
      }
    }
  }
  public BouncingBall8b() 
  {
    setLayout(new BorderLayout());
    canvas = new BouncingBallCanvasa(); 
    add("Center", canvas); 
    controlPanel = new Panel(); 
    controlPanel.setLayout(new GridLayout(1,0));     
    Button startButton = new Button("start");
    controlPanel.add(startButton);
    Button stopButton = new Button("stop");
    controlPanel.add(stopButton);
    Button fastButton = new Button("fast");
    controlPanel.add(fastButton);
    Button slowButton = new Button("slow");
    controlPanel.add(slowButton);
    Choice choice = new Choice();
    choice.addItem("Background");
    choice.addItem("Ball");
    choice.addItem("Rectangle");
    controlPanel.add(choice);
    Choice choice1 = new Choice();
    choice1.addItem("red");
    choice1.addItem("green");
    choice1.addItem("blue");
    choice1.addItem("cyan");
    choice1.addItem("orange");
    controlPanel.add(choice1); 
    add("South", controlPanel); 
    startButton.addActionListener(new StartStopButtonHandler(START)); 
    stopButton.addActionListener(new StartStopButtonHandler(STOP)); 
    fastButton.addActionListener(new StartStopButtonHandler(FAST)); 
    slowButton.addActionListener(new StartStopButtonHandler(SLOW)); 
    choice.addItemListener(new ObjectChoiceHandler()); 
    choice1.addItemListener(new ColorChoiceHandler()); 
  }
  public void init() {
    String att = getParameter("delay");
    if (att != null) 
      delay = Integer.parseInt(att);
    doLayout();
    canvas.init();
  }
  public void start() {
    if (bouncingThread == null) {
      bouncingThread = new Thread(this);
      bouncingThread.start();
    }
  }
  
  public void stop() {
    if (bouncingThread != null) {
      bouncingThread = null; 
    }
  }
  public void fast()
  {
     if (delay > 20)
     {
        delay = delay - 5;
      }
   }

   public void slow()
   { 
     if (delay  < 200)
     {
        delay = delay + 5;
     }
   }
  
  public void run() {
    while (Thread.currentThread() == bouncingThread) {
      try { 
        Thread.currentThread().sleep(delay); 
      }                                            
      catch (InterruptedException e){}
      canvas.repaint();
    }
  }
      
}








