info.gridworld.world
Class World<T>

java.lang.Object
  extended by info.gridworld.world.World<T>
Direct Known Subclasses:
ActorWorld

public class World<T>
extends Object

A World is the mediator between a grid and the GridWorld GUI.
This class is not tested on the AP CS A and AB exams.


Constructor Summary
World()
           
World(Grid<T> g)
           
 
Method Summary
 void add(Location loc, T occupant)
          Adds an occupant at a given location.
 void addGridClass(String className)
          Adds a class to be shown in the "Set grid" menu.
 void addOccupantClass(String className)
          Adds a class to be shown when clicking on an empty location.
 Grid<T> getGrid()
          Gets the grid managed by this world.
 Set<String> getGridClasses()
          Gets a set of grid classes that should be used by the world frame for this world.
 String getMessage()
          Gets the message to be displayed in the world frame above the grid.
 Set<String> getOccupantClasses()
          Gets a set of occupant classes that should be used by the world frame for this world.
 Location getRandomEmptyLocation()
          Gets a random empty location in this world.
 boolean keyPressed(String description, Location loc)
          This method is called when a key was pressed.
 boolean locationClicked(Location loc)
          This method is called when the user clicks on a location in the WorldFrame.
 T remove(Location loc)
          Removes an occupant from a given location.
 void setGrid(Grid<T> newGrid)
          Sets the grid managed by this world.
 void setMessage(String newMessage)
          Sets the message to be displayed in the world frame above the grid.
 void show()
          Constructs and shows a frame for this world.
 void step()
          This method is called when the user clicks on the step button, or when run mode has been activated by clicking the run button.
 String toString()
          Returns a string that shows the positions of the grid occupants.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

World

public World()

World

public World(Grid<T> g)
Method Detail

show

public void show()
Constructs and shows a frame for this world.


getGrid

public Grid<T> getGrid()
Gets the grid managed by this world.

Returns:
the grid

setGrid

public void setGrid(Grid<T> newGrid)
Sets the grid managed by this world.

Parameters:
newGrid - the new grid

setMessage

public void setMessage(String newMessage)
Sets the message to be displayed in the world frame above the grid.

Parameters:
newMessage - the new message

getMessage

public String getMessage()
Gets the message to be displayed in the world frame above the grid.

Returns:
the message

step

public void step()
This method is called when the user clicks on the step button, or when run mode has been activated by clicking the run button.


locationClicked

public boolean locationClicked(Location loc)
This method is called when the user clicks on a location in the WorldFrame.

Parameters:
loc - the grid location that the user selected
Returns:
true if the world consumes the click, or false if the GUI should invoke the Location->Edit menu action

keyPressed

public boolean keyPressed(String description,
                          Location loc)
This method is called when a key was pressed. Override it if your world wants to consume some keys (e.g. "1"-"9" for Sudoku). Don't consume plain arrow keys, or the user loses the ability to move the selection square with the keyboard.

Parameters:
description - the string describing the key, in this format.
loc - the selected location in the grid at the time the key was pressed
Returns:
true if the world consumes the key press, false if the GUI should consume it.

getRandomEmptyLocation

public Location getRandomEmptyLocation()
Gets a random empty location in this world.

Returns:
a random empty location

add

public void add(Location loc,
                T occupant)
Adds an occupant at a given location.

Parameters:
loc - the location
occupant - the occupant to add

remove

public T remove(Location loc)
Removes an occupant from a given location.

Parameters:
loc - the location
Returns:
the removed occupant, or null if the location was empty

addGridClass

public void addGridClass(String className)
Adds a class to be shown in the "Set grid" menu.

Parameters:
className - the name of the grid class

addOccupantClass

public void addOccupantClass(String className)
Adds a class to be shown when clicking on an empty location.

Parameters:
className - the name of the occupant class

getGridClasses

public Set<String> getGridClasses()
Gets a set of grid classes that should be used by the world frame for this world.

Returns:
the set of grid class names

getOccupantClasses

public Set<String> getOccupantClasses()
Gets a set of occupant classes that should be used by the world frame for this world.

Returns:
the set of occupant class names

toString

public String toString()
Returns a string that shows the positions of the grid occupants.

Overrides:
toString in class Object