ballwar.model
Class Ball

java.lang.Object
  extended by ballwar.model.Ball
All Implemented Interfaces:
Observer

public class Ball
extends Object
implements Observer

An concrete circular ball that moves in a line with its given velocity and bounces off the walls of a rectangularly shaped container. A ball has a location, a radius, a mass, and environment and an update strategy.


Field Summary
private  Color _color
          The color of the ball.
private  IBallEnvironment _env
          The ball's environment
private  Point2DDouble _location
          The present location of the center of the ball.
private  double _mass
          The mass of the ball
private  IBallMoveStrategy _moveStrategy
           
private  IPaintStrategy _paintStrategy
          The paint strategy used by the ball
private  int _radius
          The radius of the ball.
private  IUpdateStrategy _strategy
          The update strategy used by the ball
private  BallCmdSet _updateCmdSet
           
 
Constructor Summary
Ball(IBallEnvironment env, IUpdateStrategy strategy, IPaintStrategy pstrategy)
          Constructor for a ball.
 
Method Summary
 void addUpdateCmd(IBallCmd cmd)
           
 void doKill()
          Kills th ball by removing it from the dispatcher.
 Color getColor()
          Returns the current color of this Ball.
 IBallEnvironment getEnv()
          Get the ball's environment
 Point2DDouble getLocation()
          Returns the current center of this Ball.
 double getMass()
          Get the ball's mass
 IBallMoveStrategy getMoveStrategy()
           
 IPaintStrategy getPaintStrategy()
          Returns the current paint strategy of this Ball
 int getRadius()
          Returns the current radius for this Ball.
 IUpdateStrategy getStrategy()
          Returns the current strategy of this Ball
 Point2DDouble getVelocity()
          Returns the current velocity of this Ball.
 void kill()
          "Kills" this ball by delegating the request to the strategy.
private  void move()
           
 void paint(Graphics g)
          Paints the image of the ball onto a Graphics object using the current paint strategy.
 void performUpdate()
           
 void setColor(Color color)
          Sets the color of this Ball to a given color.
 void setLocation(Point2DDouble location)
          Sets the center of the ball to a new location.
 void setMass(double mass)
          Sets the ball's mass
 void setMoveStrategy(IBallMoveStrategy s)
           
 void setPaintStrategy(IPaintStrategy pstrategy)
          Sets the current paint strategy of this Ball to the given strategy
 void setRadius(int radius)
          Sets the radius of this Ball to a new radius.
 void setStrategy(IUpdateStrategy strategy)
          Sets the current update strategy of this Ball to the given strategy
 void setVelocity(Point2DDouble velocity)
          Sets the velocity of this Ball to a new velocity.
 void update(Observable o, Object cmd)
          The update method called by the main ball Dispatcher to notify all the balls to perform the given command.
 void updateState(Graphics g)
          Updates the state of the ball by calling the strategy's updateState(), moving the ball as per its velocity, bouncing off the walls if necessary, and notifying all other balls that it may have collided with them.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_env

private IBallEnvironment _env
The ball's environment


_location

private Point2DDouble _location
The present location of the center of the ball.


_radius

private int _radius
The radius of the ball.


_moveStrategy

private IBallMoveStrategy _moveStrategy

_color

private Color _color
The color of the ball.


_mass

private double _mass
The mass of the ball


_strategy

private IUpdateStrategy _strategy
The update strategy used by the ball


_paintStrategy

private IPaintStrategy _paintStrategy
The paint strategy used by the ball


_updateCmdSet

private BallCmdSet _updateCmdSet
Constructor Detail

Ball

public Ball(IBallEnvironment env,
            IUpdateStrategy strategy,
            IPaintStrategy pstrategy)
Constructor for a ball. Calls the init method of the given strategy to initialize the ball.

Parameters:
env - The ball's environment
strategy - The update strategy used by the ball.
Method Detail

getMoveStrategy

public IBallMoveStrategy getMoveStrategy()

setMoveStrategy

public void setMoveStrategy(IBallMoveStrategy s)

update

public void update(Observable o,
                   Object cmd)
The update method called by the main ball Dispatcher to notify all the balls to perform the given command. The given command is executed.

Specified by:
update in interface Observer
Parameters:
o - The Dispatcher that set the update request.
cmd - The IBallCmd that will be run.

addUpdateCmd

public void addUpdateCmd(IBallCmd cmd)

updateState

public void updateState(Graphics g)
Updates the state of the ball by calling the strategy's updateState(), moving the ball as per its velocity, bouncing off the walls if necessary, and notifying all other balls that it may have collided with them. Finally, the ball is painted onto the given Graphics object.

Parameters:
g - The Graphics object to paint on.

performUpdate

public void performUpdate()

move

private void move()

setLocation

public void setLocation(Point2DDouble location)
Sets the center of the ball to a new location.

Parameters:
location - the new center.

getLocation

public Point2DDouble getLocation()
Returns the current center of this Ball.

Returns:
a Point2DDouble representing the center of this Ball.

setRadius

public void setRadius(int radius)
Sets the radius of this Ball to a new radius.

Parameters:
radius -

getRadius

public int getRadius()
Returns the current radius for this Ball.

Returns:
the radius of this Ball.

setVelocity

public void setVelocity(Point2DDouble velocity)
Sets the velocity of this Ball to a new velocity.

Parameters:
velocity - the new velocity for this Ball.

getVelocity

public Point2DDouble getVelocity()
Returns the current velocity of this Ball.

Returns:
the current velocity of this Ball.

setColor

public void setColor(Color color)
Sets the color of this Ball to a given color.

Parameters:
color - the new color for this Ball.

getColor

public Color getColor()
Returns the current color of this Ball.

Returns:
the color of this Ball.

getStrategy

public IUpdateStrategy getStrategy()
Returns the current strategy of this Ball

Returns:
the IUpdateStrategy currently in use.

setPaintStrategy

public void setPaintStrategy(IPaintStrategy pstrategy)
Sets the current paint strategy of this Ball to the given strategy

Parameters:
pstrategy - The new IPaintStrategy to use.

getPaintStrategy

public IPaintStrategy getPaintStrategy()
Returns the current paint strategy of this Ball

Returns:
the IPaintStrategy currently in use.

setStrategy

public void setStrategy(IUpdateStrategy strategy)
Sets the current update strategy of this Ball to the given strategy

Parameters:
strategy - The new IUpdateStrategy to use.

paint

public void paint(Graphics g)
Paints the image of the ball onto a Graphics object using the current paint strategy.

Parameters:
g - a Graphics object.

kill

public void kill()
"Kills" this ball by delegating the request to the strategy.


doKill

public void doKill()
Kills th ball by removing it from the dispatcher. This will effectively remove the ball from the system.


getEnv

public IBallEnvironment getEnv()
Get the ball's environment

Returns:
the ball's environment

getMass

public double getMass()
Get the ball's mass

Returns:
The ball's mass

setMass

public void setMass(double mass)
Sets the ball's mass

Parameters:
mass - The mass value. Must be a positive, non-zero number.