import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class SimApplet extends JApplet {
	public static Hunter hu1 = new Hunter(Color.red);
	public static Hunter hu2 = new Hunter(Color.magenta);
    final static int GRID_ROWS = 50;
    final static int GRID_COLUMNS = 50;
    private static final int FRAME_WIDTH = 520;
    private static final int FRAME_HEIGHT = 590;

    public SimApplet () {
	JRootPane pane = getRootPane ();
	//@assume pane != null
	pane.putClientProperty ("defaultSystemEventQueueCheck", Boolean.TRUE);
    }

    public void init() {

	// Add support for keyboard input using a KeyListener

	addKeyListener(new KeyAdapter () {
                    public void keyPressed (KeyEvent e) {
                      if (e.getKeyCode() == 87) {
                        System.err.println("up");
                        hu1.dir = new Direction (-1,0);
                        hu1.started = true;
                      }
                      if (e.getKeyCode() == 65) {
                        System.err.println("left");
                        hu1.dir = new Direction (0, -1);
                        hu1.started = true;
                      }
                      if (e.getKeyCode() == 83) {
                        System.err.println("down");
                        hu1.dir = new Direction (1, 0);
                        hu1.started = true;
                      }
                      if (e.getKeyCode() == 68) {
                        System.err.println("right");
                        hu1.dir = new Direction (0,1);
                        hu1.started = true;
                      }
                      if (e.getKeyCode() == 73) {
                        System.err.println("up");
                        hu2.dir = new Direction (-1,0);
                        hu2.started = true;
                      }
                      if (e.getKeyCode() == 74) {
                        System.err.println("left");
                        hu2.dir = new Direction (0, -1);
                        hu2.started = true;
                      }
                      if (e.getKeyCode() == 75) {
                        System.err.println("down");
                        hu2.dir = new Direction (1, 0);
                        hu2.started = true;
                      }
                      if (e.getKeyCode() == 76) {
                        System.err.println("right");
                        hu2.dir = new Direction (0,1);
                        hu2.started = true;
                      }
                      }
                    
                  }
                );

	Grid grid = new Grid (50, 50);
	Simulator ca = new Simulator (FRAME_HEIGHT, FRAME_WIDTH, grid);

	Container content = getContentPane ();

	// Add objects to the simulation.

	grid.setObjectAt(0,0,hu1);
	grid.setObjectAt(49,49,hu2);
	Random ra = new Random();
	for (int i = 0; i < 10; i++) {
	    grid.setObjectAt(ra.nextInt(50), ra.nextInt(50), new Food());
	}
	for (int i = 0; i < 5; i++) {
	    grid.setObjectAt(ra.nextInt(50), ra.nextInt(50), new Stalker());
	}
	
	if (content != null) {
	    content.add (ca, BorderLayout.CENTER);
	} else {
	    System.err.println ("ERROR: No content pane");
	}

	ca.start ();
    }
}