Class Grid

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

public class Grid
extends java.awt.Panel

Class for displaying the map and handling the Player Movements. Fires Events for battles and redirects

See Also:
Player, People, MapObject, Redirect, RedirectEvent, Game, BattleEvent, Serialized Form

Field Summary
static int DEFAULTNUM
          The default number of Rows/COlumns
static int DOWN
          Down direction
static int LEFT
          Left direction
protected  int numi
          The number of rows
protected  int numj
          The number of columns
protected  int pi
          The position of the player, the row/i
protected  int pj
          The position of the player, the column/j
static int RIGHT
          Right direction
static int UP
          Up direction
 
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
Grid()
          Construct a new Grid of size Grid.DEFAULTNUM
 
Method Summary
 void addBattleListener(BattleListener bl)
          Add a class to be notified of Battles
 void addRedirectListener(RedirectListener r)
          Add a class to be notified of Battles
 void allowMonsterOnMap(int m)
          Allow wild Monsters of type m on this.
 void beginBattleWithPlayer(MapObject opp)
          Call to begin a battle with th Player, fires the events.
 boolean canMove(int i, int j)
          Determines if the Location specified can be moved into
 void destroy()
          Destroy all threads tied to this by setting their Grid Objects to null.
 void fillGrid()
          Fills the Grid in a standard manner, useful for debugging.
 void fillMonsters()
          Removes the older wild Monsters, and add new ones randomly from th selection allowed.
 int getNumColumns()
          Gets the number of columns
 int getNumRows()
          Gets the number of rows.
 java.awt.Dimension getPerferredSize()
          Returns the preferred size of this.
 Player getPlayer()
          Gets the Player of this.
 void init()
          Initialize the Grid, ie start the Peopl Moving, and fill this with wild Monsters
 MapObject isPeopleInDirection(int i, int j, int dir, int distance)
          Returns the MapObject in the direction specified within the distance specified from the i, j specified.
 boolean isValidLocation(int i, int j)
          Determines if the Location specified is a valid on this.
 Location locationOf(MapObject mp)
          Gets the Location of the specified MapObject, uses equals(Object o).
 MapObject mapObjectAt(int i, int j)
          Gets the MapObject at i, j.
 void moveMapObjectTo(int i, int j, int ni, int nj)
          Moves the MapObject at i, j to ni, nj.
 void movePlayerTo(int direction)
          Moves the Player in the direction specified
 void paint(java.awt.Graphics g)
          Draws the components of this
 void processEvent(java.awt.AWTEvent e)
          Handle the Events.
 void removeAllObjects()
          Remove All Objects from the Grid.
 void removeBattleListener(BattleListener bl)
          Removes a class that was being notified.
 MapObject removeObject(int i, int j)
          Removes and returns the MapObject at the specified Location
 void removeRedirectListener(RedirectListener r)
          Removes a class that was being notified.
 void repaint(int i, int j)
          Repaint the Location specified.
 void setDefault(MapObject m)
          Sets the default backdrop to display.
 void setMonsterLevel(int l)
          Sets the wild Monsters level for this
 void setObjectAt(MapObject mp, int i, int j)
          Sets the object at the Location specified
 void setPlayer(Player p)
          Sets the Player of this.
 void update(java.awt.Graphics g)
          Overriden to rmove the flickering effect
 
Methods inherited from class java.awt.Panel
addNotify, getAccessibleContext
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFont, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULTNUM

public static final int DEFAULTNUM
The default number of Rows/COlumns

See Also:
Constant Field Values

LEFT

public static final int LEFT
Left direction

See Also:
Constant Field Values

RIGHT

public static final int RIGHT
Right direction

See Also:
Constant Field Values

UP

public static final int UP
Up direction

See Also:
Constant Field Values

DOWN

public static final int DOWN
Down direction

See Also:
Constant Field Values

numi

protected int numi
The number of rows


numj

protected int numj
The number of columns


pi

protected int pi
The position of the player, the row/i


pj

protected int pj
The position of the player, the column/j

Constructor Detail

Grid

public Grid()
Construct a new Grid of size Grid.DEFAULTNUM

Method Detail

getNumRows

public int getNumRows()
Gets the number of rows.

Returns:
the Number of rows

getNumColumns

public int getNumColumns()
Gets the number of columns

Returns:
the number of columns

isValidLocation

public boolean isValidLocation(int i,
                               int j)
Determines if the Location specified is a valid on this.

Parameters:
i - the row
j - the column
Returns:
true iff i & j are in this ie less than Grid.DEFAULTNUM, and greater than 0

canMove

public boolean canMove(int i,
                       int j)
Determines if the Location specified can be moved into

Parameters:
i - the row
j - the column
Returns:
truee iff can be moved into, ie if isValidLocation, and canBeEntered
See Also:
MapObject, Grass, Floor

setDefault

public void setDefault(MapObject m)
Sets the default backdrop to display.

Parameters:
m - the MapObject to have as the backdrop

getPerferredSize

public java.awt.Dimension getPerferredSize()
Returns the preferred size of this. Needs be larger or equal to the size of the Applet

Returns:
the Dimension of this
See Also:
Game

setPlayer

public void setPlayer(Player p)
Sets the Player of this.

Parameters:
p - the new Player of this

setObjectAt

public void setObjectAt(MapObject mp,
                        int i,
                        int j)
                 throws InvalidLocationException,
                        NonEmptyException
Sets the object at the Location specified

Parameters:
mp - the MapObject to set at i, j
i - the row
j - the column
Throws:
InvalidLocationException - iff !isValidException(i, j)
NonEmptyException - iff !canMove(i, j)

removeObject

public MapObject removeObject(int i,
                              int j)
                       throws InvalidLocationException
Removes and returns the MapObject at the specified Location

Parameters:
i - the row
j - the column
Throws:
InvalidLocationException - iff !isValidException(i, j)

removeAllObjects

public void removeAllObjects()
Remove All Objects from the Grid.


mapObjectAt

public MapObject mapObjectAt(int i,
                             int j)
                      throws EmptyLocationException,
                             InvalidLocationException
Gets the MapObject at i, j.

Parameters:
i - the row
j - the columne
Throws:
InvalidLocationException - iff !isValidException(i, j)
EmptyLocationException - if the Location only contains the default MapObject

locationOf

public Location locationOf(MapObject mp)
                    throws InvalidLocationException
Gets the Location of the specified MapObject, uses equals(Object o). Has a O(n*n) run time.

Parameters:
mp - the MapObject to search for.
InvalidLocationException

isPeopleInDirection

public MapObject isPeopleInDirection(int i,
                                     int j,
                                     int dir,
                                     int distance)
                              throws InvalidLocationException
Returns the MapObject in the direction specified within the distance specified from the i, j specified.

Parameters:
i - the row
j - the column
dir - the direction as specified above
distance - the distance to look
Throws:
InvalidLocationException - iff !isValidException(i, j)

getPlayer

public Player getPlayer()
Gets the Player of this.

Returns:
the Player of this

movePlayerTo

public void movePlayerTo(int direction)
                  throws NonEmptyException,
                         InvalidLocationException
Moves the Player in the direction specified

Parameters:
direction - the direction to move the player, as specified above
NonEmptyException
InvalidLocationException

moveMapObjectTo

public void moveMapObjectTo(int i,
                            int j,
                            int ni,
                            int nj)
                     throws NonEmptyException,
                            InvalidLocationException,
                            EmptyLocationException
Moves the MapObject at i, j to ni, nj.

Parameters:
i - the row originally at
j - the column origanally at
ni - the new row
nj - the new column
Throws:
InvalidLocationException - iff !isValidException(i, j)
NonEmptyException - iff !canMove(i, j)
EmptyLocationException - Will Not Be Thrown.

processEvent

public void processEvent(java.awt.AWTEvent e)
Handle the Events. Overriden to handle the KeyEvents.

Overrides:
processEvent in class java.awt.Container
Parameters:
e - the AWTEvent to process

allowMonsterOnMap

public void allowMonsterOnMap(int m)
Allow wild Monsters of type m on this.

Parameters:
m - the Monster. to allow

setMonsterLevel

public void setMonsterLevel(int l)
Sets the wild Monsters level for this

Parameters:
l - the level of the wild Monsters

fillGrid

public void fillGrid()
Fills the Grid in a standard manner, useful for debugging.


init

public void init()
Initialize the Grid, ie start the Peopl Moving, and fill this with wild Monsters


fillMonsters

public void fillMonsters()
Removes the older wild Monsters, and add new ones randomly from th selection allowed.


destroy

public void destroy()
Destroy all threads tied to this by setting their Grid Objects to null.

See Also:
PersonAI, Trainer

paint

public void paint(java.awt.Graphics g)
Draws the components of this

Overrides:
paint in class java.awt.Container
Parameters:
g - the Graphics

update

public void update(java.awt.Graphics g)
Overriden to rmove the flickering effect

Overrides:
update in class java.awt.Container
Parameters:
g - the Graphics

repaint

public void repaint(int i,
                    int j)
Repaint the Location specified.

Parameters:
i - the row
j - the column

addBattleListener

public void addBattleListener(BattleListener bl)
Add a class to be notified of Battles


removeBattleListener

public void removeBattleListener(BattleListener bl)
Removes a class that was being notified.


addRedirectListener

public void addRedirectListener(RedirectListener r)
Add a class to be notified of Battles


removeRedirectListener

public void removeRedirectListener(RedirectListener r)
Removes a class that was being notified.


beginBattleWithPlayer

public void beginBattleWithPlayer(MapObject opp)
Call to begin a battle with th Player, fires the events.

Parameters:
opp - the Opponent of the Player.