package Life;

import java.awt.*;
import java.awt.event.*;
/**
 * This type was created by Jens Elbæk
 */
public class ArtificialLife extends java.applet.Applet 
{
	private ArtificialLife ai = this;
	private Thread life;
	private EcologicalSystem eath;
	private Thread thread;
	private Button StepLife = null;
	private Button StartLife = null;
	private Button StopLife = null;
	private static String GLIDER = "Glider"; 
	private static String GLIDERKANON = "Gliderkanon"; 
	private static String EDENSHAVE = "Edens have"; 
	private static String TOM = "En tom verden"; 
	static int screenwidth=800, screenheight=500;
private class LifeformChoiceListener implements ItemListener
{  
	public void itemStateChanged (ItemEvent e) 
	{
		String s = (String)e.getItem();
		if (s.equals(TOM))
		{
			eath=getNewLifeSpace();
		}
		if (s.equals(GLIDER))
		{
			eath=getNewLifeSpace();
			eath.startGlider();
			eath.repaint();
		}
		if (s.equals(GLIDERKANON))
		{
			eath=getNewLifeSpace();
			eath.startGliderkanon();
			eath.repaint();
		}
		if (s.equals(EDENSHAVE))
		{	
			eath=getNewLifeSpace();
			eath.startParadis();
			eath.repaint();
		}
	}
}

private class StepButtonListener implements ActionListener
{  
	public void actionPerformed (ActionEvent e) 
	{
		eath.lifecicle();

		System.out.println("Step knap trykket");
	}
}
private class StartButtonListener implements ActionListener
{  
	public void actionPerformed (ActionEvent e) 
	{
		System.out.println("Start knap trykket");
		if (life == null)
		{
			life = new Thread(eath);
			eath.thisthread = life;
			life.start();
		}
		else
			System.out.println("life allerede startet");
	
	}
}
private class StopButtonListener implements ActionListener
{  
	public void actionPerformed (ActionEvent e) 
	{
		if (life != null)
		{
			life.interrupt();
			life = null;
		}
		System.out.println("Stop knap trykket");
	}
}
private class MouseClickListener extends MouseAdapter
{  
	public void mouseClicked (MouseEvent e) 
	{
		int x = e.getX()/eath.pointsize;
		int y = e.getY()/eath.pointsize;
		System.out.println("Musen trykket på X=" + e.getX() + " og Y=" + e.getY() );
		System.out.println("Start liv på X=" + x + " og Y=" + y );
		if (eath.cells[x][y].alive)
			eath.cells[x][y].kill();
		else
			eath.cells[x][y].setAlive();
		eath.changestate(x, y);
		eath.cells[x][y].draw(eath.pixels);
		eath.imgsrc.newPixels(0, 0, eath.bredde*eath.pointsize, eath.højde*eath.pointsize);

		eath.repaint();
	}
}
/**
 * ArtificialLife constructor comment.
 */
public ArtificialLife() 
{
	super();
}
/**
 * Insert the method's description here.
 * Creation date: (27-04-01 11:28:46)
 * @return int
 */
public int getheight()
{
	return screenheight;
}
/**
 * Return the LifeSpace property value.
 * @return java.awt.Panel
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private EcologicalSystem getNewLifeSpace()
{
	if (eath != null) 
	{
		remove(eath);
	}
	try {
			eath = new EcologicalSystem(this);
			eath.setName("LifeSpace");
			eath.setLayout(null);
//			eath.setBounds(4, 4, eath.bredde*eath.pointsize, eath.højde*eath.pointsize);
			eath.addMouseListener(new MouseClickListener());
			// user code begin {1}
			remove(eath);
			add(eath, BorderLayout.CENTER);
		
			// user code end
		}
	catch (java.lang.Throwable ivjExc) 
		{
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}

	return eath;
}
/**
 * Insert the method's description here.
 * Creation date: (27-04-01 11:28:46)
 * @return int
 */
public int getwidth()
{
	return screenwidth;
}
/**
 * Called whenever the part throws an exception.
 * @param exception java.lang.Throwable
 */
private void handleException(Throwable exception) {

	/* Uncomment the following lines to print uncaught exceptions to stdout */
	// System.out.println("--------- UNCAUGHT EXCEPTION ---------");
	// exception.printStackTrace(System.out);
}
/**
 * Handle the Applet init method.
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
public void init() {
	super.init();
	try {
		setName("ArtificialLife");
		setLayout(new BorderLayout());
		setSize(screenwidth, screenheight);
		Panel knapper = new Panel();
		knapper.setBackground(null);
		knapper.setLayout(new FlowLayout(FlowLayout.CENTER));
		add(knapper, BorderLayout.SOUTH);
		Button step = new Button("Step");
		step.addActionListener(new StepButtonListener());
		knapper.add (step);				 				
		Button start = new Button("Start");
		start.addActionListener(new StartButtonListener());
		knapper.add (start);				 				
		Button stop = new Button("Stop");
		stop.addActionListener(new StopButtonListener());
		knapper.add (stop);	
		
		Choice valg = new Choice();
		valg.addItem(TOM);
		valg.addItem(GLIDER);
		valg.addItem(GLIDERKANON);
		valg.addItem(EDENSHAVE);
		valg.addItemListener( new LifeformChoiceListener() );
		knapper.add(valg);
		
			
		eath = getNewLifeSpace();
//		add(eath, BorderLayout.CENTER);

//		eath.startParadis();
//		eath.repaint();
	} catch (java.lang.Throwable ivjExc) {
		// user code begin {2}
		// user code end
		handleException(ivjExc);
	}
}
}

