Class RobotRoom

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Canvas
              |
              +--RobotRoom
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

public class RobotRoom
extends java.awt.Canvas

Represents a room in which Science of Computing robots can move. A room consists of a tiled floor and walls. The floor tiles can be colored. Within the room, tiles provide a coordinate system for describing the positions of robots, walls, etc. The coordinate system's origin is the upper left corner of the room, with horizontal coordinates increasing to the right and vertical coordinates increasing down. Coordinates start at 0, i.e., the left-most side of the room is horizontal coordinate 0, and the top side is row 0. Every room has walls around its edges (so that robots can't fall out of the room), and a room may have other walls (or fragments of wall) in its interior.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
RobotRoom()
          Initialize a robot's room with default size and contents.
RobotRoom(java.lang.String description)
          Initialize a robot's room from its description.
 
Method Summary
 void addRobot(Robot robot, int col, int row)
          Put a robot in a room.
protected  java.awt.Color getColor(int col, int row)
          Find out what color a tile within a room has.
 int getRoomHeight()
          Get a room's height.
 int getRoomWidth()
          Get a room's width.
protected  boolean isObstructed(int col, int row)
          Find out whether a tile within a room is obstructed.
protected  void moveRobot(int oldCol, int oldRow, int newCol, int newRow)
          Move a robot from one tile to another.
 void paint(java.awt.Graphics context)
          Redisplay a robot room.
protected  void setColor(int col, int row, java.awt.Color newColor)
          Change the color of a tile in a room.
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy, update
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RobotRoom

public RobotRoom()
Initialize a robot's room with default size and contents. The default is a square room 10 tiles on a side, with all tiles white and no interior walls. For example

RobotRoom room = new RobotRoom();


RobotRoom

public RobotRoom(java.lang.String description)
Initialize a robot's room from its description. Regardless of what the description calls for, the room will be at least three tiles by three tiles, to leave space for a wall all around the room and at least one tile for a robot inside. The room will be no more than 25 tiles by 25. For example

RobotRoom room = new RobotRoom( "5 5 2 1 Y" );

Parameters:
description - The description of the room. This is a string that must start with two integers, the room's width and height, in tiles. The string may then contain any number of tile specifications, which consist of column and row coordinates (integers) followed by a color or wall designator (R, G, B, Y, or K for red, green, blue, yellow, and black, or * for a wall). All elements of the specification should be separated by white space. For example 9 5 4 2 * 3 2 r specifies a 9-tile wide by 5-tile high room whose middle tile is a wall, the tile to its left is red. The outer-most rows and columns of the room always contain walls, and any tiles not defined by the description are white.
Method Detail

addRobot

public void addRobot(Robot robot,
                     int col,
                     int row)
Put a robot in a room. This will put the robot at the requested position if possible. If not possible, because the requested position is outside the room, because there is a wall at the requested position, or because another robot is already at the requested position, then the new robot is not placed in the room at all. For example

room.addRobot( someRobot, 3, 5 );

Parameters:
robot - The robot that is being put in this room.
col - The horizontal coordinate (tile column) at which to place this robot.
row - The vertical coordinate (tile row) at which to place this robot.

getColor

protected java.awt.Color getColor(int col,
                                  int row)
Find out what color a tile within a room has.

Parameters:
col - The horizontal coordinate (i.e., tile column) of the tile.
row - The vertical coordinate (i.e.,tile row) of the tile.
Returns:
The color of the tile at the specified column and row. If the row or column are out of bounds for this room, then the color is assumed to be white.

getRoomHeight

public int getRoomHeight()
Get a room's height. For example

int h = room.getRoomHeight();

Returns:
The room's height, in tiles.

getRoomWidth

public int getRoomWidth()
Get a room's width. For example

int w = room.getRoomWidth();

Returns:
The width of the room, in tiles.

isObstructed

protected boolean isObstructed(int col,
                               int row)
Find out whether a tile within a room is obstructed. A tile is obstructed if it contains a wall or robot.

Parameters:
col - The horizontal coordinate (tile column) of the tile.
row - The vertical coordinate (tile row) of the tile.
Returns:
True if the specified tile is obstructed, false if it is not.

moveRobot

protected void moveRobot(int oldCol,
                         int oldRow,
                         int newCol,
                         int newRow)
Move a robot from one tile to another.

Parameters:
oldCol - The horizontal coordinate (tile column) of the tile the robot is moving from.
oldRow - The vertical coordinate (tile row) of the tile the robot is moving from.
newCol - The horizontal coordinate of the tile the robot is moving to.
newRow - The vertical coordinate of the tile the robot is moving to.

paint

public void paint(java.awt.Graphics context)
Redisplay a robot room. This method should never be called by client code, it is called automatically by the Java runtime system when the runtime system believes that a robot room needs to be redrawn.

Overrides:
paint in class java.awt.Canvas
Parameters:
context - The graphics context in which to draw the room.

setColor

protected void setColor(int col,
                        int row,
                        java.awt.Color newColor)
Change the color of a tile in a room.

Parameters:
col - The horizontal coordinate (i.e., tile column) of the tile to change.
row - The vertical coordinate (i.e., tile row) of the tile to change.
newColor - The color to make the tile.