public class Ball
extends java.lang.Object
implements java.util.Observer
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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 cmd)
Run the given command.
|
void |
updateState(Dispatcher disp)
Update the state of the ball.
|
private java.awt.Point _location
private int _radius
private java.awt.Point _velocity
private java.awt.Color _color
private java.awt.Component _container
private IUpdateStrategy _updateStrategy
private IPaintStrategy _paintStrategy
public Ball(java.awt.Point p, int r, java.awt.Point v, java.awt.Color c, java.awt.Component container, IUpdateStrategy updateStrategy, IPaintStrategy paintStrategy)
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.updateStrategy
- the ball's update strategypaintStrategy
- the ball's paint strategypublic void update(java.util.Observable o, java.lang.Object cmd)
update
in interface java.util.Observer
cmd
- The command to run in this context.public void updateState(Dispatcher disp)
disp
- The dispatcher to use if needed.public void move()
public void bounce()
public void setLocation(java.awt.Point location)
location
- the new center.public java.awt.Point getLocation()
public void setRadius(int radius)
radius
- the new radius valuepublic int getRadius()
public void setVelocity(java.awt.Point velocity)
velocity
- the new velocity for this Ball.public java.awt.Point getVelocity()
public void setColor(java.awt.Color color)
color
- the new color for this Ball.public java.awt.Color getColor()
public IUpdateStrategy getUpdateStrategy()
public void setUpdateStrategy(IUpdateStrategy strategy)
strategy
- The new IUpdateStrategy to use.public void paint(java.awt.Graphics g)
g
- a Graphics object.public void setPaintStrategy(IPaintStrategy pstrategy)
pstrategy
- The new IPaintStrategy to use.public IPaintStrategy getPaintStrategy()
public java.awt.Component getContainer()