import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*
    Name: Vincent Ng
    Date: June 4, 2004
*/
//requires the horserace (the thread) and horse (the horses)
public class HorseRun2 extends Applet implements ActionListener
{
    HorseRace hr; //The two horse race
    JButton cmdstart;
    JButton cmdreset;
    
    
    public void init ()
    {

	setLayout (new BorderLayout ());
	resize (700, 400);
	//Set up the start and reset buttons
	JPanel p = new JPanel (new FlowLayout ());
	JLabel title = new JLabel ("Horse Race!");
	JPanel p2 = new JPanel (new BorderLayout());
	Font titleFont = new Font ("Arial", Font.BOLD, 24);
	p2.setBackground (Color.white);
	title.setFont(titleFont);
	cmdstart = new JButton ("Start Race");
	cmdstart.setActionCommand ("Go");
	cmdstart.addActionListener (this);
	p.add (cmdstart);
	cmdreset = new JButton ("Reset Race");
	cmdreset.setActionCommand ("Reset");
	cmdreset.addActionListener (this);
	cmdreset.setEnabled(false);
	cmdstart.requestFocus();
	p.add (cmdreset);
	p2.add (p, "North");
	//Construct the horserace and add it
	hr = new HorseRace (13, this);
	p2.add (hr, "Center");
	add (title, "North");
	add (p2, "Center");
    }


    public void actionPerformed (ActionEvent e)
    {
	if (e.getActionCommand ().equals ("Go"))
	{ //Start the new horse race thread
	    Thread hrthread = new Thread (hr, "Thread1");
	    hrthread.start ();
	    cmdreset.setEnabled(true);
	    cmdstart.setEnabled(false);
	    cmdreset.requestFocus();
	}
	else if (e.getActionCommand ().equals ("Reset"))
	{ //Reset the horserace, it stops the thread internally
	    hr.reset ();
	    cmdreset.setEnabled(false);
	    cmdstart.setEnabled(true);
	    cmdstart.requestFocus();
	}
    }
}
