import java.awt.*;
import java.applet.*;
//NOTE: in the robot set the delayTime instance variable to be 0,
//  this program doesn't need a delay

public class Run_Robot extends Applet
{
    RobotWorld dirt; //The World Object
    Robot Karl; //The Robot Object
    Button move, east, west, north, south, fake, fake1, fake2, fake3;
    Panel grid, space;

    public void init ()
    { //set up the buttons
	setLayout (new BorderLayout ());
	grid = new Panel (new GridLayout (3, 3));
	move = new Button ("Move");
	east = new Button ("East");
	west = new Button ("West");
	north = new Button ("North");
	south = new Button ("South");
	
	//set up the fake buttons to make the grid layout work
	fake = new Button ();
	fake.setVisible (false);
	fake1 = new Button ();
	fake1.setVisible (false);
	fake2 = new Button ();
	fake2.setVisible (false);
	fake3 = new Button ();
	fake3.setVisible (false);
	
	//add all of the buttons to the grid
	grid.add (fake);
	grid.add (north);
	grid.add (fake1);
	grid.add (west);
	grid.add (move);
	grid.add (east);
	grid.add (fake2);
	grid.add (south);
	grid.add (fake3);
	
	//space is to keep the grid from filling up the screen
	space = new Panel (new FlowLayout (FlowLayout.CENTER, 0, 3));
	space.add (grid);
	add (space, "North");

	//Add the World, add some walls
	dirt = new RobotWorld ();

	for (int i = 0 ; i < 8 ; i++)
	{
	    dirt.placeWall (i, 1);
	    dirt.placeWall (i + 2, 3);
	    dirt.placeWall (i, 5);
	    dirt.placeWall (i + 2, 7);
	}
	dirt.placeDot (9, 9);
	//Add Karl, top left
	Karl = new Robot ();
    }


    public boolean action (Event e, Object o)
    {
	Graphics g = getGraphics ();
	if (e.target == move)
	{ //move Karl forward
	    dirt.drawWorld (g);
	    Karl.move (g, dirt);
	}
	else if (e.target == east)
	{ //change Karl's direction to east
	    dirt.drawWorld (g);
	    Karl.turnEast (g, dirt);
	}
	else if (e.target == west)
	{ //change Karl's direction to west
	    dirt.drawWorld (g);
	    Karl.turnWest (g, dirt);
	}
	else if (e.target == north)
	{ //change Karl's direction to north
	    dirt.drawWorld (g);
	    Karl.turnNorth (g, dirt);
	}
	else if (e.target == south)
	{ //change Karl's direction to south
	    dirt.drawWorld (g);
	    Karl.turnSouth (g, dirt);
	}
	return true;
    }


    public void paint (Graphics g)
    { //Draw Karl and the World
	dirt.drawWorld (g);
	Karl.drawRobot (g);

    }
}

