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  IBallCmd _collisionCmd
          The command sent to the dispatcher to notify the other balls that they may have collided with this ball.
private  Color _color
          The color of the ball.
private  IBallEnvironment _env
          The ball's environment
private  Point _location
          The present location of the center of the ball.
private  double _mass
          The mass of the ball
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  Point _velocity
          The current velocity of the ball.
 
Constructor Summary
Ball(IBallEnvironment env, IUpdateStrategy strategy, IPaintStrategy pstrategy)
          Constructor for a ball.
 
Method Summary
 void bounce()
          Checks if the ball needs to bounce off the wall of its container.
 void collide(Ball target)
           
 Color getColor()
          Returns the current color of this Ball.
 IBallEnvironment getEnv()
          Get the ball's environment
 Point getLocation()
          Returns the current center of this Ball.
 double getMass()
          Get the ball's mass
 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
 Point getVelocity()
          Returns the current velocity of this Ball.
 void kill()
          "Kills" this ball by delegating the request to the strategy.
 void killSelf()
          Kills th ball by removing it from the dispatcher.
 void paint(Graphics g)
          Paints the image of the ball onto a Graphics object using the current paint strategy.
 void setColor(Color color)
          Sets the color of this Ball to a given color.
 void setLocation(Point location)
          Sets the center of the ball to a new location.
 void setMass(double mass)
          Sets the ball's mass
 void setPaintStrategy(IPaintStrategy pstrategy)
          Sets the current 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(Point 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 Point _location
The present location of the center of the ball.


_radius

private int _radius
The radius of the ball.


_velocity

private Point _velocity
The current velocity of the ball. Given in terms of an X and Y increment per update interval.


_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


_collisionCmd

private IBallCmd _collisionCmd
The command sent to the dispatcher to notify the other balls that they may have collided with this ball. Students to add initialization here using anonymous inner class. Should conform to these two specifications: 1. No action taken if the target is the same ball (same instance) as this ball. 2. If not the same ball, then this ball should checkCollide with the target ball, NOT the other way around. This is because the algorithms assume that the ball doing the checking is the one that just moved.

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

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.

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.

bounce

public void bounce()
Checks if the ball needs to bounce off the wall of its container. Performs a perfectly elastic bounce, moving the ball back completely onto the drawing area if necessary.


setLocation

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

Parameters:
location - the new center.

getLocation

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

Returns:
a Point 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(Point velocity)
Sets the velocity of this Ball to a new velocity.

Parameters:
velocity - the new velocity for this Ball.

getVelocity

public Point 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 strategy of this Ball to the given strategy

Parameters:
strategy - 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.

collide

public void collide(Ball target)

kill

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


killSelf

public void killSelf()
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.