kareltherobot
Class World

java.lang.Object
  extended bykareltherobot.World
All Implemented Interfaces:
Directions

public class World
extends java.lang.Object
implements Directions

Build and maintain the world in which Robots work and play. The normal operation is to show the world in its own frame, which is initially not visible. You can either call World.setVisible(true) to show this frame or extract its Canvas with World.worldCanvas() if you would rather show the world in another context. You can save worlds in files and restore them. You can even create the worlds in a text editor. For information on the external form of worlds see http://csis.pace.edu/~bergin/KarelJava2ed/karelexperimental.html , This format is also assumed by getWorld and is produced by asText when the separator is a newline. Note that the features here are not intended for normal robot programming, but for world initialization and maintenance.


Nested Class Summary
 
Nested classes inherited from class kareltherobot.Directions
Directions.Direction
 
Field Summary
static World asObject
           
 
Fields inherited from interface kareltherobot.Directions
East, infinity, North, South, West
 
Method Summary
static World asObject()
          Obtain a singleton object representing the world
static java.lang.String asText(java.lang.String sep)
          Return a String version of the world's contents suitable for saving and restoring.
static void clearBeepers(int Street, int Avenue)
          Clear all beepers, if any, from the given corner.
static int delay()
          Return the current delay value.
static void getWorld(java.lang.String commands)
          Read a world from a string.
static void makeView()
          This is normally not called, but is left public for the rare situations (like embedding this system in another) in which you need to manually create the view system.
static void placeBeepers(int Street, int Avenue, int howMany)
          Put some (additional) beepers on a corner.
static void placeEWWall(int NorthOfStreet, int atAvenue, int lengthTowardEast)
          Place a chain of one or more east west walls
static void placeNSWall(int atStreet, int EastOfAvenue, int lengthTowardNorth)
          Place a chain of one or more north south walls
static void readWorld(java.lang.String filename)
          Read a world from a file in the current directory.
static void readWorld(java.lang.String directoryPath, java.lang.String filename)
          Read a world from a file in the given directory path.
static void removeEWWall(int NorthOfStreet, int atAvenue)
          Remove a single east-west wall segment.
static void removeNSWall(int atStreet, int EastOfAvenue)
          Remove a signle north-south wall segment
static void repaint()
          Repaint the world.
static void replaceCloser(java.awt.event.WindowListener w)
          Replace the standard window closer (which calls exit) of the frame with one of your choice.
static void reset()
          Remove everything from this world.
static void resume()
          Resume all the threads after stopping them.
static void saveWorld(java.lang.String filename)
          Save the world in a file in the current directory.
static void saveWorld(java.lang.String directoryPath, java.lang.String filename)
          Save the world in a file in the given directory path.
static void saveXMLWorld(java.lang.String filename)
          Save a representation of th eworld in XML format.
static void saveXMLWorld(java.lang.String directoryPath, java.lang.String filename)
          Save a world in XML format
static void setBeeperColor(java.awt.Color color)
          Change the color of beepers from the standard black to any color you choose.
static void setDelay(int d)
          Slow down the operation of robots in the world.
static void setNeutroniumColor(java.awt.Color color)
          Change the color of walls from the standard black to any color you choose.
static void setSize(int numberOfStreets, int numberOfAvenues)
          Set the size of the world.
static void setStreetColor(java.awt.Color color)
          Change the color of streets and avenues from the standard dark red to any color you choose.
static void setTrace(boolean t)
          Turn tracing on or off.
static void setupThread(java.lang.Runnable r)
          Let a robot run in its own thread.
static void setVisible()
          Toggle the visibility state.
static void setVisible(boolean show)
          Make the frame visible or not.
static void setVisible(boolean show, int width, int height)
          Set the world visible or invisible and simultaneously set its pixel size.
static void setWorldColor(java.awt.Color color)
          Change the color of the background of the world from the standard white to any color you choose.
static void showBeepers()
          Show a report about all the beepers in the world on System.out
static void showSpeedControl(boolean show)
          Make the speed dialog visible or not.
static void showWorld()
          Print a representation of the world's contents on System.out.
static void startThreads()
          Start all the threads initially.
static void stop()
          Suspend all the threads in the world.
static java.awt.Canvas worldCanvas()
          Return the canvas that normally shows in the world's frame.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

asObject

public static final World asObject
Method Detail

asObject

public static final World asObject()
Obtain a singleton object representing the world

Returns:
an object that behaves like the world

placeBeepers

public static void placeBeepers(int Street,
                                int Avenue,
                                int howMany)
Put some (additional) beepers on a corner.

Parameters:
Street - the street of the required corner
Avenue - the avenue of the required corner
howMany - the number of beepers. An integer or "infinity"

clearBeepers

public static void clearBeepers(int Street,
                                int Avenue)
Clear all beepers, if any, from the given corner.

Parameters:
Street - the street of the required corner
Avenue - the avenue of the required corner

worldCanvas

public static java.awt.Canvas worldCanvas()
Return the canvas that normally shows in the world's frame. Use this if you don't want a separate frame, but want the world to appear in some other panel. Useful only on the rare occasion when you need to embed this in another system.


placeEWWall

public static final void placeEWWall(int NorthOfStreet,
                                     int atAvenue,
                                     int lengthTowardEast)
Place a chain of one or more east west walls

Parameters:
NorthOfStreet - the chain of walls will be north of this street
atAvenue - the first segment will cross this avenue
lengthTowardEast - how many avenues to cross toward the east

placeNSWall

public static final void placeNSWall(int atStreet,
                                     int EastOfAvenue,
                                     int lengthTowardNorth)
Place a chain of one or more north south walls

Parameters:
atStreet - the first segment will cross this street
EastOfAvenue - the chain of walls will be east of this avenue
lengthTowardNorth - how many strets to cross toward the north

removeEWWall

public static final void removeEWWall(int NorthOfStreet,
                                      int atAvenue)
Remove a single east-west wall segment.

Parameters:
NorthOfStreet - the wall north of this street
atAvenue - the segment that crosses this avenue

removeNSWall

public static final void removeNSWall(int atStreet,
                                      int EastOfAvenue)
Remove a signle north-south wall segment

Parameters:
atStreet - the segment that crosses this street
EastOfAvenue - the wall east of this avenue

saveWorld

public static final void saveWorld(java.lang.String filename)
Save the world in a file in the current directory.

Parameters:
filename - the name of the file to be saved

saveXMLWorld

public static final void saveXMLWorld(java.lang.String filename)
Save a representation of th eworld in XML format. Currently there is no way to read this back.

Parameters:
filename - the name of the file to save

asText

public static final java.lang.String asText(java.lang.String sep)
Return a String version of the world's contents suitable for saving and restoring.

Parameters:
sep - the separator to use between world commands, generally newline

saveWorld

public static final void saveWorld(java.lang.String directoryPath,
                                   java.lang.String filename)
Save the world in a file in the given directory path.

Parameters:
directoryPath - a full path to the directory in which to save the file
filename - the name of the file to be saved

saveXMLWorld

public static void saveXMLWorld(java.lang.String directoryPath,
                                java.lang.String filename)
Save a world in XML format

Parameters:
directoryPath - a full path to the directory in which to save the file
filename - the name of the file to be saved

showBeepers

public static final void showBeepers()
Show a report about all the beepers in the world on System.out


showWorld

public static final void showWorld()
Print a representation of the world's contents on System.out.


readWorld

public static final void readWorld(java.lang.String filename)
Read a world from a file in the current directory. It must be a legal world file

Parameters:
filename - the name of the file in the current directory to read

readWorld

public static final void readWorld(java.lang.String directoryPath,
                                   java.lang.String filename)
Read a world from a file in the given directory path.

Parameters:
directoryPath - a full path to the file containing the world
filename - the nameof the file to read

getWorld

public static void getWorld(java.lang.String commands)
Read a world from a string.

Parameters:
commands - a string in the form of a world file. Tokens are separated by the usual white space

reset

public static final void reset()
Remove everything from this world.


setDelay

public static final void setDelay(int d)
Slow down the operation of robots in the world. A delay of 0 causes poor performance on some systems.

Parameters:
d - the delay. The delay is the opposite of speed.

delay

public static final int delay()
Return the current delay value.


resume

public static final void resume()
Resume all the threads after stopping them.


stop

public static final void stop()
Suspend all the threads in the world.


setSize

public static final void setSize(int numberOfStreets,
                                 int numberOfAvenues)
Set the size of the world.

Parameters:
numberOfStreets - the number of streets to show. This sets the vertical size of the world's window.
numberOfAvenues - the number of avenues to show. If they won't fit in the visible window, a scroller will be created.

setTrace

public static final void setTrace(boolean t)
Turn tracing on or off. When on (default) robots show their state after actions.

Parameters:
t - turn tracing on if true. It is on by default.

setupThread

public static final void setupThread(java.lang.Runnable r)
Let a robot run in its own thread. Put its "task" into the run method and pass the robot to this method. Runnable objects added like this can be started and stopped with the speed dialog. Robots can have their speed controlled by it also.

Parameters:
r - any runnable object, not just a robot

showSpeedControl

public static final void showSpeedControl(boolean show)
Make the speed dialog visible or not.

Parameters:
show - show the speed dialog if true

setVisible

public static final void setVisible(boolean show)
Make the frame visible or not. Note that it is invisible by default.

Parameters:
show - show the world if true

setVisible

public static final void setVisible(boolean show,
                                    int width,
                                    int height)
Set the world visible or invisible and simultaneously set its pixel size. The default is not to show the world and the default size is about 570 by 490 pixels

Parameters:
show - true to show the world, false to hide it
width - the number of pixels of width in the window
height - the number of pixels of height in the window

setVisible

public static final void setVisible()
Toggle the visibility state. If the world is visible make it invisible. If it is invisible, make it visible


replaceCloser

public static final void replaceCloser(java.awt.event.WindowListener w)
Replace the standard window closer (which calls exit) of the frame with one of your choice. Use null to get one that does nothing at all.

Parameters:
w - a new WindowListener or null

setStreetColor

public static final void setStreetColor(java.awt.Color color)
Change the color of streets and avenues from the standard dark red to any color you choose.

Note that the colors set in the world are NOT saved in the world files. They are just a feature of the current run of the program.

Parameters:
color - the color for streets and avenues

setNeutroniumColor

public static final void setNeutroniumColor(java.awt.Color color)
Change the color of walls from the standard black to any color you choose.

Note that the colors set in the world are NOT saved in the world files. They are just a feature of the current run of the program.

Parameters:
color - the color for walls, which are made of Neutronium, an impenetrable substance

setBeeperColor

public static final void setBeeperColor(java.awt.Color color)
Change the color of beepers from the standard black to any color you choose.

Note that the colors set in the world are NOT saved in the world files. They are just a feature of the current run of the program.

Parameters:
color - the color for beepers.

setWorldColor

public static final void setWorldColor(java.awt.Color color)
Change the color of the background of the world from the standard white to any color you choose.

Note that the colors set in the world are NOT saved in the world files. They are just a feature of the current run of the program.

Parameters:
color - the color for the background of the world.

repaint

public static final void repaint()
Repaint the world. Useful after reset() for example.


startThreads

public static final void startThreads()
Start all the threads initially. Normally called from the speed dialog only.


makeView

public static final void makeView()
This is normally not called, but is left public for the rare situations (like embedding this system in another) in which you need to manually create the view system.