ballwar.model
Class BallModel

java.lang.Object
  extended by ballwar.model.BallModel
All Implemented Interfaces:
IBallEnvironment

public class BallModel
extends Object
implements IBallEnvironment

Represents the main "model" of the system that coordinates the non-GUI processing.


Field Summary
private  Dispatcher _dispatcher
          The dispatcher used to communicate with all the balls.
private  ILambda _displayCmd
          The current command used to generate the display string.
private  ILambda _displayCmdOrig
          The initial, default command used to generate the initial display string.
private  Graphics _graphics
          Variable used to hold the current Graphics object upon which the balls will next paint.
private  SwitcherStrategy _switcher
          The SwitcherStrategy object to use in switcher balls.
private  Timer _timer
          A timer object that is the main clock that drives the simulation.
private  int _timeSlice
          The time in milliseconds between updates to the balls.
private  IBallCmd _updateStateCmd
          The command used when the balls are told to update their states and paint.
private  IViewCtrlAdapter _vCtrlA
          The adapter to the view.
 
Fields inherited from interface ballwar.model.IBallEnvironment
NullEnv
 
Constructor Summary
BallModel(IViewCtrlAdapter vCtrlA)
          Constructor for the BallControl class Instantiates the BallGUI frame with the required ILambdas for making a ball, clearing the balls from the screen and for painting the balls onto the Container Starts the timer up.
 
Method Summary
 void addDisplayCmd(ILambda cmd)
          Adds a display command (ILambda) used to display messages onto the view.
 void addKeyCmd(String keyName, ILambda cmd)
          Associates a key with a behavior (command).
 void clearBalls()
          Clears all the balls from the dispatcher and resets the display string to its original state.
 Component getCanvas()
          Gets the Component that the Balls will be painted on.
 Dispatcher getDispatcher()
          Get the dispatcher used to notify all the balls to execute a supplied command.
 String getDisplayStr(String origStr)
          Called by the view to get the current display string.
 int getHeight()
          Gets the height of the ball's environment
 IPlayer getSelectedPlayer()
          Get the currently selected player from the view.
 int getWidth()
          Get's the width of the ball's environment.
 Ball loadBall(IUpdateStrategy strategy, IPaintStrategy pstrategy)
           
 Object loadStrategy(String className, Object defaultObject)
          Uses dynamic class loading to load and instantiate a specified class, given its full class name.
 void loadSwitcherBall(IPaintStrategy pstrategy)
          Loads a ball with a switcher strategy into the system.
 IPlayer makePlayer(String name, IMovementKeys keys)
          Makes a play8er object given a name and a set of movement keys.
 void paint(Graphics g)
          Tells the model to have all the balls to update their states and paint themselves onto the supplied Graphics object.
 void registerMovementKeys(IMovementKeys keys, IMoveable m)
          Registers a set of movement keys, as defined by an IMovementKeys object, with the view.
 void setSwitcherStrategy(IUpdateStrategy strategy)
          Sets the strategy used by any switcher balls.
 void start()
          Starts the model running by starting the internal timer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_vCtrlA

private IViewCtrlAdapter _vCtrlA
The adapter to the view.


_dispatcher

private Dispatcher _dispatcher
The dispatcher used to communicate with all the balls.


_switcher

private SwitcherStrategy _switcher
The SwitcherStrategy object to use in switcher balls.


_updateStateCmd

private IBallCmd _updateStateCmd
The command used when the balls are told to update their states and paint.


_graphics

private Graphics _graphics
Variable used to hold the current Graphics object upon which the balls will next paint.


_timeSlice

private int _timeSlice
The time in milliseconds between updates to the balls.


_timer

private Timer _timer
A timer object that is the main clock that drives the simulation. Every _timeSlice interval the view is asked to update, which in turn will ask us to do the same.


_displayCmdOrig

private ILambda _displayCmdOrig
The initial, default command used to generate the initial display string.


_displayCmd

private ILambda _displayCmd
The current command used to generate the display string.

Constructor Detail

BallModel

public BallModel(IViewCtrlAdapter vCtrlA)
Constructor for the BallControl class Instantiates the BallGUI frame with the required ILambdas for making a ball, clearing the balls from the screen and for painting the balls onto the Container Starts the timer up.

Method Detail

getDispatcher

public Dispatcher getDispatcher()
Get the dispatcher used to notify all the balls to execute a supplied command.

Specified by:
getDispatcher in interface IBallEnvironment
Returns:
The system's dispatcher.

getCanvas

public Component getCanvas()
Description copied from interface: IBallEnvironment
Gets the Component that the Balls will be painted on. Note that a Component class is also implements the ImageObserver interface.

Specified by:
getCanvas in interface IBallEnvironment
Returns:
The Component object upon which the Balls will be painted.

getWidth

public int getWidth()
Get's the width of the ball's environment.

Specified by:
getWidth in interface IBallEnvironment
Returns:
The width of the environment in pixels

getHeight

public int getHeight()
Gets the height of the ball's environment

Specified by:
getHeight in interface IBallEnvironment
Returns:
The height of the ball's environment in pixels.

addDisplayCmd

public void addDisplayCmd(ILambda cmd)
Adds a display command (ILambda) used to display messages onto the view. The display command is assumed to take a default string and return a string. The ILambda's apply() output is concatenated onto any previously existing command's output separated by a space. The command is called whenever the view requests that the display string be updated (ie. when getDisplayStr() is called). It is not required that the command's apply method use its input parameter.

Specified by:
addDisplayCmd in interface IBallEnvironment
Parameters:
cmd - The ILambda used to generate the display string.

getDisplayStr

public String getDisplayStr(String origStr)
Called by the view to get the current display string. Calls the concatenated display commands added with the addDisplayCmd method.

Parameters:
origStr - The string handed to the display commands as their input parameter.
Returns:
The complete display string.

addKeyCmd

public void addKeyCmd(String keyName,
                      ILambda cmd)
Description copied from interface: IBallEnvironment
Associates a key with a behavior (command). When the user presses the given key, the ILambda command will be run. The parameter passed to the command will be the key name. The return value will be discarded.

Specified by:
addKeyCmd in interface IBallEnvironment
Parameters:
keyName -
cmd -

loadBall

public Ball loadBall(IUpdateStrategy strategy,
                     IPaintStrategy pstrategy)
Parameters:
strategy -
Returns:

loadStrategy

public Object loadStrategy(String className,
                           Object defaultObject)
Uses dynamic class loading to load and instantiate a specified class, given its full class name.

Parameters:
classname - full classname of the strategy
defaultObject - The default object instance to return if the given class cannot be instantiated. Usually specified to be an instance of the class's null object value.
Returns:
An instance of the given class, typed as Object

paint

public void paint(Graphics g)
Tells the model to have all the balls to update their states and paint themselves onto the supplied Graphics object.

Parameters:
g - The Graphics object to paint on.

clearBalls

public void clearBalls()
Clears all the balls from the dispatcher and resets the display string to its original state.


setSwitcherStrategy

public void setSwitcherStrategy(IUpdateStrategy strategy)
Sets the strategy used by any switcher balls.

Parameters:
strategy - The new strategy to use.

loadSwitcherBall

public void loadSwitcherBall(IPaintStrategy pstrategy)
Loads a ball with a switcher strategy into the system.


start

public void start()
Starts the model running by starting the internal timer.


makePlayer

public IPlayer makePlayer(String name,
                          IMovementKeys keys)
Makes a play8er object given a name and a set of movement keys. If the name is an empty string, the NullPlayer is returned. Also adds the player's score to display string.

Parameters:
name - The name of the new player
keys - The set of movement keys associated with this player.
Returns:
An IPlayer instance.

registerMovementKeys

public void registerMovementKeys(IMovementKeys keys,
                                 IMoveable m)
Registers a set of movement keys, as defined by an IMovementKeys object, with the view. Each key is associated with its corresponding action in the IMoveable object.

Specified by:
registerMovementKeys in interface IBallEnvironment
Parameters:
keys - The set of movement keys to register
m - The IMoveable object to associate with those keys.

getSelectedPlayer

public IPlayer getSelectedPlayer()
Get the currently selected player from the view.

Specified by:
getSelectedPlayer in interface IBallEnvironment
Returns:
The currently selectd player