Class RobotlApplet

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----java.awt.Panel
                           |
                           +----java.applet.Applet
                                   |
                                   +----RobotlApplet

public class RobotlApplet
extends Applet
implements ElJefe
The main applet that the user interacts with. This applet creates a single parser to parse programs, a single RobotWorld to run the programs and a new Thread for each execution. It also keeps references to all of the interface elements in the main window including the menu bar.

Author:
Peter Sagerson, petersag@wpi.edu

Variable Index

 o BLUES
A constant defining a color set.
 o BUTTONS
The number of buttons lined up below the code box.
 o canvas
The component that draws the RoBOTL grid.
 o clearButton
 o code
The text box into which the user enters code.
 o codeLabel
 o colors
A list of color sets for the grid display.
 o COMPONENTS
The total number of components.
 o defaultInfo
Text displayed in the gridInfo box when the mouse is not on the grid.
 o EXAMPLES_FILE
The name of the example programs index file on the server.
 o examplesEntries
The actual example programs (elements of type IndexEntry).
 o examplesList
The List for the examples Browse window.
 o examplesMenu
The Examples menu.
 o executing
True when a program is executing.
 o exprogFrame
The window used for the example programs.
 o FROG_LAND
A constant defining a color set.
 o gridInfo
The textbox in which information about the grid or individual robots is displayed.
 o gridLabel
 o INDEX_FILE
 o indexEntries
The actual modules in the tutorial index.
 o indexFrame
The window used for the tutorial index.
 o indexList
The List for the tutorial Browse window.
 o infoLabel
 o infoLocked
True when the gridInfo box is locked onto an InfoTarget.
 o initButton
The button seen in the browser that will make the RoBOTL window visible.
 o keepMessages
When true, the messages text box isn't cleared between runs.
 o killButton
 o lastColorItem
A reference to the last item in the Color menu to be selected.
 o lastDelayItem
A reference to the last item in the Delay menu to be selected.
 o lastInfoTarget
A reference to the last Cell or Robot to be set for feedback.
 o menubar
The main menu bar for the window.
 o messages
The text box in which warnings, errors and other messages are displayed.
 o msgLabel
 o parseButton
 o parser
An instance of the parser generated by JavaCC.
 o PEACHES
A constant defining a color set.
 o prettyPrint
When true, DebugBox.format() is called before parsing.
 o PURPLES
A constant defining a color set.
 o REFERENCE_URL
The URL for the reference guide.
 o robotlFrame
The window that contains all of the interface components.
 o runButton
 o SEAFOAM
A constant defining a color set.
 o soundOn
True if the user wants to hear all of the sounds.
 o stepButton
 o stepField
The Label that displays the next instruction to be executed.
 o steppingFont
The default font for the applet.
 o thread
The Thread that executes a RoBOTL program.
 o TUTORIAL_FILE
The name of the tutorial index file on the server.
 o tutorialEntries
The actual modules in the tutorial.
 o tutorialFrame
The window used for the tutorial.
 o tutorialList
The List for the tutorial Browse window.
 o tutorialMenu
The Tutorial menu.
 o useRobotInfo
True when the gridInfo box displays robot information instead of cell information.
 o world
An instance of RobotWorld to be used for all simulations.

Constructor Index

 o RobotlApplet()

Method Index

 o action(Event, Object)
Responds to the buttons and menus.
 o activateTutorial(String)
Creates the tutorial window, if it doesn't exist.
 o buildInfoString(InfoTarget, boolean)
Returns the info string for the given target.
 o coordString(int, int)
Converts x and y coodinates into a printable string.
 o formatCode()
If prettyPrint is true, tells the code box to format the code.
 o getCode()
Returns the contents of the code-entry text box.
 o getLockedObject()
If the gridInfo window is locked, returns the cell that it's locked on.
 o getParser()
Returns a reference to the parser object.
 o getWorld()
Returns a reference to the world field.
 o haltExecution()
Stops the execution thread, clears the "next instruction" field and calls setExecuting(false, false).
 o handleRunButton()
Calls spawnExecution if we're not currently executing.
 o handleStepButton()
Starts executing if we're not already and calls RobotWorld.step().
 o init()
Initializes the interface components and menus and calls initFrame().
 o initFrame()
Creates the RoBOTL window and adds all components.
 o killFrame(RobotlFrame)
This is called by RobotlFrame.cleanup().
 o lockGridFeedback(int, int)
Locks the info box into displaying the given cell's information regardless of the mouse position.
 o lockRobotFeedback(int, int)
Locks the robot at the given location into the info box.
 o paintCell(int, int)
Tells the RobotlCanvas to paint the cell and updates the grid feedback.
 o playSound(String, boolean)
Plays a sound with the given name, if soundOn is true or conditional is false.
 o postMessage(String)
Adds the given String to the end of the message box.
 o resetGridInfo()
Called right after the array of cells has been destroyed and recreated in RobotWorld.execute().
 o retrieveCode(String)
Retrieves the given text file from applet's directory on the server and reads its contents into a string, which is returned.
 o robotlInFront()
Calls robotlFrame.toFront().
 o selectCodeLine(int)
Selects the given line of code in the code entry box.
 o setColors(int)
Sets the background color of gridInfo and calls canvas.setColors()
 o setExecuting(boolean, boolean)
Called once when a new execution thread starts and again just before it dies.
 o setGridFeedback(int, int, boolean)
Tells the applet to display information on the given square in the grid info box.
 o setNextInstruction(String, int)
Displays the given string in the stepField Label and highlights the given line.
 o setupDisplay(RobotWorld)
Wrapper for canvas.reInit.
 o setupSupportData(Menu, List, Hashtable, String)
The file filename contains the information that is displayed in a SupportFrame.
 o spawnExecution(boolean, boolean)
Clears the message box, sets the delay time and starts a new execution thread.
 o updateFeedback()
Updates the info box if lastInfoTarget is not null.
 o updateGridFeedback(int, int)
Called by paintCell whenever a grid cell changes.
 o updateRobotFeedback(Robot)
Updates the info box if the given robot is the current info target.
 o useCode(String)
Gets the contents of file filename and puts them into the code text box.

Variables

 o COMPONENTS
 public static final int COMPONENTS
The total number of components. Used by RobotlLayout.

 o BUTTONS
 public static final int BUTTONS
The number of buttons lined up below the code box. RobotlLayout.

 o TUTORIAL_FILE
 final String TUTORIAL_FILE
The name of the tutorial index file on the server.

 o EXAMPLES_FILE
 final String EXAMPLES_FILE
The name of the example programs index file on the server.

 o INDEX_FILE
 final String INDEX_FILE
 o REFERENCE_URL
 final String REFERENCE_URL
The URL for the reference guide.

 o executing
 boolean executing
True when a program is executing.

 o soundOn
 boolean soundOn
True if the user wants to hear all of the sounds.

 o prettyPrint
 boolean prettyPrint
When true, DebugBox.format() is called before parsing.

 o keepMessages
 boolean keepMessages
When true, the messages text box isn't cleared between runs. This feature was used for usability testing.

 o infoLocked
 boolean infoLocked
True when the gridInfo box is locked onto an InfoTarget.

See Also:
InfoTarget
 o useRobotInfo
 boolean useRobotInfo
True when the gridInfo box displays robot information instead of cell information.

 o lastInfoTarget
 InfoTarget lastInfoTarget
A reference to the last Cell or Robot to be set for feedback.

 o lastDelayItem
 CheckboxMenuItem lastDelayItem
A reference to the last item in the Delay menu to be selected. This is needed because when a new item is selected, this one must be unmarked.

 o lastColorItem
 CheckboxMenuItem lastColorItem
A reference to the last item in the Color menu to be selected. This is needed because when a new item is selected, this one must be unmarked.

 o colors
 Color colors[][]
A list of color sets for the grid display. In order, the colors are for wall, beepers, world, robot, text box.

 o BLUES
 public static final int BLUES
A constant defining a color set.

 o FROG_LAND
 public static final int FROG_LAND
A constant defining a color set.

 o PURPLES
 public static final int PURPLES
A constant defining a color set.

 o SEAFOAM
 public static final int SEAFOAM
A constant defining a color set.

 o PEACHES
 public static final int PEACHES
A constant defining a color set.

 o parser
 RoBOTL parser
An instance of the parser generated by JavaCC.

 o world
 RobotWorld world
An instance of RobotWorld to be used for all simulations.

 o thread
 Thread thread
The Thread that executes a RoBOTL program.

 o robotlFrame
 RobotlFrame robotlFrame
The window that contains all of the interface components.

 o menubar
 MenuBar menubar
The main menu bar for the window.

 o tutorialFrame
 TutorialFrame tutorialFrame
The window used for the tutorial.

 o tutorialMenu
 Menu tutorialMenu
The Tutorial menu. Used by action to identify items in this menu.

 o tutorialList
 List tutorialList
The List for the tutorial Browse window.

 o tutorialEntries
 Hashtable tutorialEntries
The actual modules in the tutorial.

 o indexFrame
 IndexFrame indexFrame
The window used for the tutorial index.

 o indexList
 List indexList
The List for the tutorial Browse window.

 o indexEntries
 Hashtable indexEntries
The actual modules in the tutorial index.

 o exprogFrame
 ExamplesFrame exprogFrame
The window used for the example programs.

 o examplesMenu
 Menu examplesMenu
The Examples menu. Used by action to identify items in this menu.

 o examplesList
 List examplesList
The List for the examples Browse window.

 o examplesEntries
 Hashtable examplesEntries
The actual example programs (elements of type IndexEntry).

 o canvas
 RobotlCanvas canvas
The component that draws the RoBOTL grid.

 o code
 DebugBox code
The text box into which the user enters code.

 o messages
 TextArea messages
The text box in which warnings, errors and other messages are displayed.

 o steppingFont
 static Font steppingFont
The default font for the applet.

 o defaultInfo
 final String defaultInfo
Text displayed in the gridInfo box when the mouse is not on the grid.

 o gridInfo
 TextArea gridInfo
The textbox in which information about the grid or individual robots is displayed.

 o initButton
 Button initButton
The button seen in the browser that will make the RoBOTL window visible.

 o clearButton
 Button clearButton
 o runButton
 Button runButton
 o killButton
 Button killButton
 o parseButton
 Button parseButton
 o stepButton
 Button stepButton
 o stepField
 Label stepField
The Label that displays the next instruction to be executed.

 o gridLabel
 Label gridLabel
 o codeLabel
 Label codeLabel
 o msgLabel
 Label msgLabel
 o infoLabel
 Label infoLabel

Constructors

 o RobotlApplet
 public RobotlApplet()

Methods

 o init
 public void init()
Initializes the interface components and menus and calls initFrame().

Overrides:
init in class Applet
 o setupSupportData
 void setupSupportData(Menu menu,
                       List list,
                       Hashtable table,
                       String filename)
The file filename contains the information that is displayed in a SupportFrame. The titles are also displayed the given menu. The arguments menu, list and tabe are initialized here. The file format is as follows:

{Title}\n{codeFile}[,{textFile}]\n{Summary}\n\n (repeat)

{Title} is displayed in the Examples or Tutorial and the list box in the associated SupportFrame. {codeFile} is the name of the file containing the code for this entry. {textFile} is optional and refers to the file containing the text that the tutorial displays for that module. {Summary} is displayed in the right-hand text box of the SupportFrame when {Title} is selected in the list.

See Also:
SupportFrame, IndexEntry
 o initFrame
 void initFrame()
Creates the RoBOTL window and adds all components.

 o coordString
 public static String coordString(int x,
                                  int y)
Converts x and y coodinates into a printable string. This is in RobotlApplet simply because there's no better place for it.

 o getParser
 public RoBOTL getParser()
Returns a reference to the parser object. Called by RobotWorld.

 o getWorld
 public RobotWorld getWorld()
Returns a reference to the world field.

See Also:
world
 o getCode
 public String getCode()
Returns the contents of the code-entry text box.

 o getLockedObject
 public InfoTarget getLockedObject()
If the gridInfo window is locked, returns the cell that it's locked on. Returns null otherwise.

 o formatCode
 public String formatCode()
If prettyPrint is true, tells the code box to format the code.

 o retrieveCode
 public String retrieveCode(String name)
Retrieves the given text file from applet's directory on the server and reads its contents into a string, which is returned.

 o useCode
 public void useCode(String filename)
Gets the contents of file filename and puts them into the code text box. The main interface window is then moved to the foreground.

 o activateTutorial
 public void activateTutorial(String module)
Creates the tutorial window, if it doesn't exist. if module is not the empty string, it tells the tutorial window to open the given module window. If the tutorial window is created to launch a specific module, then the tutorial window is told to remain hidden upon creation.

 o postMessage
 public void postMessage(String str)
Adds the given String to the end of the message box.

Parameters:
str - the message to add to the message box
 o playSound
 public void playSound(String name,
                       boolean conditional)
Plays a sound with the given name, if soundOn is true or conditional is false. The sounds are located in the applet's directory on the server.

Parameters:
name - the name of the sound to play
conditional - if true, the sound will only be played if the user has sounds turned on. If false, the sound will be played regardless.
 o setNextInstruction
 public void setNextInstruction(String str,
                                int line)
Displays the given string in the stepField Label and highlights the given line. This is used to indicate the next instruction to be executed while stepping through a program.

 o setupDisplay
 public void setupDisplay(RobotWorld inWorld)
Wrapper for canvas.reInit.

Parameters:
inWorld - the world to display
See Also:
reInit
 o setColors
 public void setColors(int set)
Sets the background color of gridInfo and calls canvas.setColors()

 o paintCell
 public void paintCell(int x,
                       int y)
Tells the RobotlCanvas to paint the cell and updates the grid feedback.

See Also:
paintCell, updateGridFeedback
 o selectCodeLine
 public void selectCodeLine(int line)
Selects the given line of code in the code entry box.

 o resetGridInfo
 public void resetGridInfo()
Called right after the array of cells has been destroyed and recreated in RobotWorld.execute(). If we were locked on a cell, the new cell in the old location is locked into the info box. Otherwise, lastInfoTarget is set to null and the info box is cleared.

 o buildInfoString
 String buildInfoString(InfoTarget inTarget,
                        boolean robotInfo)
Returns the info string for the given target.

Parameters:
target - the Cell or Robot to get the info string from.
robotInfo - true if target is a Cell and we're actually intersted in the info for the robot in the cell.
 o setGridFeedback
 public void setGridFeedback(int x,
                             int y,
                             boolean robotInfo)
Tells the applet to display information on the given square in the grid info box.

 o updateGridFeedback
 void updateGridFeedback(int x,
                         int y)
Called by paintCell whenever a grid cell changes. If the given cell is currently being displayed in the info box, the info box is updated.

 o lockGridFeedback
 public void lockGridFeedback(int x,
                              int y)
Locks the info box into displaying the given cell's information regardless of the mouse position. lastInfoTarget will never be null because we would either have to get a mouseMove event in a given cell before getting a mouseDown event in it or we'd have a cell already locked.

 o updateRobotFeedback
 void updateRobotFeedback(Robot r)
Updates the info box if the given robot is the current info target.

Parameters:
r - the robot that may need to be updated.
 o lockRobotFeedback
 public void lockRobotFeedback(int x,
                               int y)
Locks the robot at the given location into the info box. If the robot is already locked, it is unlocked and the info box returns to tracking the mouse position.

 o updateFeedback
 public void updateFeedback()
Updates the info box if lastInfoTarget is not null.

 o setExecuting
 public void setExecuting(boolean b,
                          boolean inStepping)
Called once when a new execution thread starts and again just before it dies. This function disables, enables and sets labels of the buttons and sets the executing field.

See Also:
executing
 o spawnExecution
 void spawnExecution(boolean execute,
                     boolean stepping)
Clears the message box, sets the delay time and starts a new execution thread.

Parameters:
execute - false if the thread should just parse the code and return.
stepping - true if the thread should pause execution immediately after starting.
 o haltExecution
 public void haltExecution()
Stops the execution thread, clears the "next instruction" field and calls setExecuting(false, false).

See Also:
setExecuting
 o killFrame
 public void killFrame(RobotlFrame frame)
This is called by RobotlFrame.cleanup(). It tells the applet that the given window is going away and we should drop our reference to it.

See Also:
cleanup
 o robotlInFront
 public void robotlInFront()
Calls robotlFrame.toFront().

 o handleRunButton
 synchronized void handleRunButton()
Calls spawnExecution if we're not currently executing. If we are executing, iterrupts the execution, pausing or resuming, depending on the current state (Calls RobotWorld.interrupt()).

See Also:
interrupt
 o handleStepButton
 synchronized void handleStepButton()
Starts executing if we're not already and calls RobotWorld.step().

See Also:
step
 o action
 public boolean action(Event evt,
                       Object what)
Responds to the buttons and menus.

Overrides:
action in class Component