ballworld.model
Class Ball

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

public class Ball
extends java.lang.Object
implements java.util.Observer

An abstract ball that moves in a line with its given velocity and bounces off the walls of a rectangularly shaped container. Has a Point representing its center, an int representing its radius, a Point representing its velocity, a Color representing its color.


Field Summary
private  java.awt.Color _color
          The color of the ball.
private  java.awt.Component _container
          The Container that is currently holding this ball and defines where the walls are
private  java.awt.Point _location
          The present location of the center of the ball.
private  IPaintStrategy _paintStrategy
          The strategy used to paint the ball.
private  int _radius
          The radius of the ball.
private  IUpdateStrategy _updateStrategy
          The strategy used to update the state of the ball
private  java.awt.Point _velocity
          The current velocity of the ball.
 
Constructor Summary
Ball(java.awt.Point p, int r, java.awt.Point v, java.awt.Color c, java.awt.Component container, IUpdateStrategy updateStrategy, IPaintStrategy paintStrategy)
          Initializes this Ball to a given center, radius, color, containing walls, and the NullStrategy to move in a straight line.
 
Method Summary
 void bounce()
          Checks if the ball needs to bounce off the wall of its container
 java.awt.Color getColor()
          Returns the current color of this Ball.
 java.awt.Component getContainer()
          Accessor for the ball's container Component
 java.awt.Point getLocation()
          Returns the current center of this Ball.
 IPaintStrategy getPaintStrategy()
          Returns the current paint strategy of this Ball
 int getRadius()
          Returns the current radius for this Ball.
 IUpdateStrategy getUpdateStrategy()
          Returns the current strategy of this Ball
 java.awt.Point getVelocity()
          Returns the current velocity of this Ball.
 void move()
          Moves the ball's location by the X and Y increments specified by the velocity property.
 void paint(java.awt.Graphics g)
          Paints the image of the ball onto a Graphics object.
 void setColor(java.awt.Color color)
          Sets the color of this Ball to a given color.
 void setLocation(java.awt.Point location)
          Sets the center to a new location.
 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 setUpdateStrategy(IUpdateStrategy strategy)
          Sets the current strategy of this Ball to the given strategy
 void setVelocity(java.awt.Point velocity)
          Sets the velocity of this Ball to a new velocity.
 void update(java.util.Observable o, java.lang.Object g)
          Moves to the new position, updates the attributes of this Ball and paints it.
 void updateState()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_location

private java.awt.Point _location
The present location of the center of the ball.


_radius

private int _radius
The radius of the ball.


_velocity

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


_color

private java.awt.Color _color
The color of the ball.


_container

private java.awt.Component _container
The Container that is currently holding this ball and defines where the walls are


_updateStrategy

private IUpdateStrategy _updateStrategy
The strategy used to update the state of the ball


_paintStrategy

private IPaintStrategy _paintStrategy
The strategy used to paint the ball.

Constructor Detail

Ball

public Ball(java.awt.Point p,
            int r,
            java.awt.Point v,
            java.awt.Color c,
            java.awt.Component container,
            IUpdateStrategy updateStrategy,
            IPaintStrategy paintStrategy)
Initializes this Ball to a given center, radius, color, containing walls, and the NullStrategy to move in a straight line.

Parameters:
r - the initial radius of this Ball.
v - the initial velocity of this Ball.
c - The initial color of the ball.
p - the initial center of this Ball.
container - the rectangular Container area where this Ball is drawn.
Method Detail

update

public void update(java.util.Observable o,
                   java.lang.Object g)
Moves to the new position, updates the attributes of this Ball and paints it.

Specified by:
update in interface java.util.Observer

updateState

public void updateState()

move

public void move()
Moves the ball's location by the X and Y increments specified by the velocity property.


bounce

public void bounce()
Checks if the ball needs to bounce off the wall of its container


setLocation

public void setLocation(java.awt.Point location)
Sets the center to a new location.

Parameters:
location - the new center.

getLocation

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

Parameters:
velocity - the new velocity for this Ball.

getVelocity

public java.awt.Point getVelocity()
Returns the current velocity of this Ball.

Returns:
the current velocity of this Ball.

setColor

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

Parameters:
color - the new color for this Ball.

getColor

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

Returns:
the color of this Ball.

getUpdateStrategy

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

Returns:
the IUpdateStrategy currently in use.

setUpdateStrategy

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

Parameters:
strategy - The new IUpdateStrategy to use.

paint

public void paint(java.awt.Graphics g)
Paints the image of the ball onto a Graphics object.

Parameters:
g - a Graphics object.

setPaintStrategy

public void setPaintStrategy(IPaintStrategy pstrategy)
Sets the current 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.

getContainer

public java.awt.Component getContainer()
Accessor for the ball's container Component

Returns:
the ball's container