|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectballwar.model.Ball
public class Ball
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 |
---|
private IBallEnvironment _env
private Point _location
private int _radius
private Point _velocity
private Color _color
private double _mass
private IUpdateStrategy _strategy
private IPaintStrategy _paintStrategy
private IBallCmd _collisionCmd
Constructor Detail |
---|
public Ball(IBallEnvironment env, IUpdateStrategy strategy, IPaintStrategy pstrategy)
env
- The ball's environmentstrategy
- The update strategy used by the ball.Method Detail |
---|
public void update(Observable o, Object cmd)
update
in interface Observer
o
- The Dispatcher that set the update request.cmd
- The IBallCmd that will be run.public void updateState(Graphics g)
g
- The Graphics object to paint on.public void bounce()
public void setLocation(Point location)
location
- the new center.public Point getLocation()
public void setRadius(int radius)
radius
- public int getRadius()
public void setVelocity(Point velocity)
velocity
- the new velocity for this Ball.public Point getVelocity()
public void setColor(Color color)
color
- the new color for this Ball.public Color getColor()
public IUpdateStrategy getStrategy()
public void setPaintStrategy(IPaintStrategy pstrategy)
strategy
- The new IPaintStrategy to use.public IPaintStrategy getPaintStrategy()
public void setStrategy(IUpdateStrategy strategy)
strategy
- The new IUpdateStrategy to use.public void paint(Graphics g)
g
- a Graphics object.public void collide(Ball target)
public void kill()
public void killSelf()
public IBallEnvironment getEnv()
public double getMass()
public void setMass(double mass)
mass
- The mass value. Must be a positive, non-zero number.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |