import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

class MouseHandler implements MouseListener {
    /*@non_null@*/ private GridDisplay grid;

    public MouseHandler (/*@non_null@*/ GridDisplay display) {
        super ();
        grid = display;
    }

    public void mousePressed(MouseEvent e) { ; }
    public void mouseReleased(MouseEvent e) { ; }
    public void mouseEntered(MouseEvent e) { ; }
    public void mouseExited(MouseEvent e) { ; }

    public void mouseClicked(MouseEvent e) {
	//@assume e != null;
        grid.setObjectAt (e.getX (), e.getY ());
    }
}

public class Simulator extends JPanel implements ActionListener {
    /*@non_null@*/ GridDisplay display;
    /*@non_null@*/ SimObjectChooser simObjChooser;
    /*@non_null@*/ TeamChooser teamChooser;
    /*@non_null@*/ GameSpeedChooser gamespeedchooser;
    /*@non_null@*/ private JButton startButton, stopButton,quitButton;
   
   
    private int width;
    private int height;
    
    //@requires width > 0
    //@requires height > 0
    public Simulator (int width, int height, /*@non_null@*/ Grid grid) {
	this.width = width;
	this.height = height;
	
        setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));

	simObjChooser = new SimObjectChooser ();
        teamChooser = new TeamChooser ();
        gamespeedchooser = new GameSpeedChooser ();

	display = new GridDisplay (grid, simObjChooser,teamChooser,gamespeedchooser);
        display.setPreferredSize (new Dimension (width, height));
        display.setMinimumSize (new Dimension (width, height));
        display.addMouseListener (new MouseHandler (display));
       add (display);

        JPanel panel2 = new JPanel();
        
        
        panel2.add (new JLabel ("Army: "));
        panel2.add (teamChooser);
        
	panel2.add (new JLabel ("Place Object: "));
	panel2.add (simObjChooser);

       
        JPanel panel1 = new JPanel();
	panel1.add (new JLabel ("GameSpeed: "));
        panel1.add (gamespeedchooser);
        
        startButton = new JButton ("Fight");
        // place button at center, left position
        startButton.setVerticalTextPosition (AbstractButton.CENTER);
        startButton.setHorizontalTextPosition (AbstractButton.LEFT);
        startButton.setMnemonic (KeyEvent.VK_D);
        startButton.setActionCommand ("start");

        stopButton = new JButton ("Stop");
        // Use the default button position of CENTER, RIGHT.
        stopButton.setMnemonic (KeyEvent.VK_E);
        stopButton.setActionCommand ("stop");
        stopButton.setEnabled (false);

        quitButton = new JButton ("Quit");
        // Use the default button position of CENTER, RIGHT.
        quitButton.setMnemonic (KeyEvent.VK_F);
        quitButton.setActionCommand ("quit");
        quitButton.setEnabled (true);
        
        
        
        // Listen for actions on the Start and Stop buttons.
        startButton.addActionListener (this);
        stopButton.addActionListener (this);
        quitButton.addActionListener (this);
        
        // Add the start and stop buttons, and the table, to the
        // container, using the default layout.
	panel2.add (simObjChooser);

        panel1.add (startButton);
        panel1.add (stopButton);
        panel1.add (quitButton);
        
        add (panel2);
      //  add(display);
        add (panel1);
        
    }

    public void addClass (String s) {
	try {
	    simObjChooser.addClass (s);
	} catch (ClassNotFoundException e) {
	    System.err.println ("Error loading simulated object class " + s + ": " + e);
	}
    }

    public void addTeam (String s) {
	try {
	    teamChooser.addClass (s);
	} catch (ClassNotFoundException e) {
	    System.err.println ("Error loading simulated object class " + s + ": " + e);
	}
    }
    
    public void addSpeed (String s) {
	try {
	    gamespeedchooser.addClass (s);
	} catch (ClassNotFoundException e) {
	    System.err.println ("Error loading simulated object class " + s + ": " + e);
	}
    }
    
    
    public void actionPerformed (ActionEvent e) 
    {
	//@assume e != null;
	String command = e.getActionCommand ();

	if (command != null) {
	    if (command.equals ("start")) {
		startButton.setEnabled (false);
		stopButton.setEnabled (true);
		display.startObjects();
	    } else if (command.equals ("stop")) { 
		startButton.setEnabled (true);
		stopButton.setEnabled (false);
		display.pauseObjects();
	    } else if (command.equals ("quit")) { 
		System.exit (0);
               
	    }
	} else {
	    System.err.println ("Null command: " + e);
	}
    }
}