import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

/*Requires GameOfLife to function - that file has the game panel

Took me one hour to code
Involves: threads, comboboxes, objects
Should involve: (hence window problem) frames
*/
public class GofLInterface extends Applet implements ActionListener
{
    //applet member data
    GameOfLife game;
    JButton go,stop;
    JLabel title;

    public void init ()
    {
	setLayout (new BorderLayout ());

	go = new JButton ("Go");
	go.addActionListener (this);
	go.setActionCommand ("-1");
	stop = new JButton ("Stop");
	stop.addActionListener (this);
	stop.setActionCommand ("-3");
	
	title = new JLabel ("The Game of Life");

	String [] patStrings = {"Blinker", "Pi", "R-Pentamino", "Beacon"};

	//Create the combo box, select the item at index 4.
	//Indices start at 0, so 4 specifies the pig.
	JComboBox patList = new JComboBox (patStrings);
	patList.setSelectedIndex (1);
	patList.setActionCommand ("-2");
	patList.addActionListener (this);

	JPanel p = new JPanel (new FlowLayout (FlowLayout.CENTER, 20, 20));
	p.add (title);
	p.add (go);
	p.add (stop);
	p.add (patList);
	add (p, "North");

	game = new GameOfLife ();
	add (game, "Center");

	resize (400, 400);
	//close the darn window
	//addWindowListener (new WinderCloser ());
    }


    public void actionPerformed (ActionEvent e)
    { //To handle the button array - invoked when button is clicked

	//get the number of the button from getActionCommand
	//convert it to an int and store it in i
	if (e.getActionCommand ().equals ("-1"))
	{
	    Thread gameThread = new Thread (game, "Gamethread");
	    gameThread.start ();
	}
	else if (e.getActionCommand ().equals ("-2"))
	{
	    game.stopit ();
	    JComboBox cb = (JComboBox) e.getSource ();
	    String Name = (String) cb.getSelectedItem ();
	    game.updateScreen (Name);
	}
	else if (e.getActionCommand ().equals ("-3"))
	{
	    game.stopit ();
	    
	}


    }


    private class WindowCloser extends WindowAdapter
    {
	public void windowClosing (WindowEvent event)
	{
	    System.exit (0);
	}
    }
}
