|
Breakout Problem Set Javadocs | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbreakout.DefaultActiveBreakoutComponent
abstract class DefaultActiveBreakoutComponent provides most of the
implementation of the ActiveBreakoutComponent interface, leaving the paint, hitBy,
and act methods to the subclasser. It is an active or animate object,
and it creates a thread for itself at construction time. This
thread spends most of its time sleeping. After waking up,
the thread updates the location of the component based on the velocity
and then invokes the act method. The duration of the sleep is
determined by the timeBetweenActs field as specified in the
constructor.
Velocity
The velocity of an active component is specified as a Point, which
should lie within a 200 pixel square centered on the origin (100
in each direction). At each time step, 1/100 of the point's coordinates
are added to the component's location, with the fractional part
remembered but not revealed to someone calling getLocation(). Thus
the component will not move more than 1 pixel per timestep; allowing
component intersections to be detected before the components become
too enmeshed.
Synchronization
With all the independant threads running, some synchronization is
necessary to prevent them from stepping on each other. However,
other a thread in a component will want to modify another component.
This can lead to deadlocks as two components acquire a lock on
themselves and block waiting to acquire a lock on the other component.
Instead, all components acquire a lock on the World in which they
reside before updating their position and invoking act. Similarly,
all World-changing methods on the World synchronize before performing
their operations.
ActiveBreakoutComponent
,
World
Constructor Summary | |
DefaultActiveBreakoutComponent(java.awt.Point loc,
java.awt.Dimension size,
World world,
java.awt.Point direction,
int timeBetweenActs)
|
Method Summary | |
abstract void |
act()
subclasses must implement the act method to have the component interact with the world after moving. |
java.awt.Point |
getCenter()
returns a point that corresponds to the center of the component. |
java.awt.Point |
getDirection()
returns a new Point corresponding to the direction of the component's motion. |
java.awt.Point |
getLocation()
returns a new Point corresponding to the location of the component's upper-left corner. |
java.awt.Shape |
getShape()
returns a Rectangle at location with size. |
java.awt.Dimension |
getSize()
returns the Dimensions of the component. |
int |
getTimeBetweenActs()
returns the number of milliseconds between calls to act |
abstract void |
hitBy(BreakoutComponent striker)
subclasses must implement the hitBy method to have the component react to being struck. |
boolean |
isDead()
returns dead status of component. |
boolean |
isTransient()
returns true if the component is transient. |
void |
kill()
destroys this component |
abstract void |
paint(java.awt.Graphics g)
subclasses must implement the paint method in order to draw their component. |
void |
run()
sleeps timeBetweenActs milliseconds, updates position, then invokes act |
void |
setDirection(java.awt.Point d)
Sets the direction of motion of the component. |
void |
setLocation(java.awt.Point location)
changes the location of the component's upper left corner to the given location. |
void |
setWorld(World world)
sets the world the component lives in |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public DefaultActiveBreakoutComponent(java.awt.Point loc, java.awt.Dimension size, World world, java.awt.Point direction, int timeBetweenActs)
Method Detail |
public void run()
run
in interface java.lang.Runnable
public java.awt.Point getLocation()
getLocation
in interface BreakoutComponent
public void setLocation(java.awt.Point location)
setLocation
in interface BreakoutComponent
location
- Point to locate the component at.public java.awt.Point getCenter()
getCenter
in interface BreakoutComponent
public java.awt.Dimension getSize()
getSize
in interface BreakoutComponent
public java.awt.Shape getShape()
getShape
in interface BreakoutComponent
public void kill()
BreakoutComponent
kill
in interface BreakoutComponent
public boolean isDead()
BreakoutComponent
isDead
in interface BreakoutComponent
public boolean isTransient()
BreakoutComponent
isTransient
in interface BreakoutComponent
public void setWorld(World world)
BreakoutComponent
setWorld
in interface BreakoutComponent
public java.awt.Point getDirection()
getDirection
in interface ActiveBreakoutComponent
public void setDirection(java.awt.Point d)
setDirection
in interface ActiveBreakoutComponent
public int getTimeBetweenActs()
getTimeBetweenActs
in interface ActiveBreakoutComponent
public abstract void paint(java.awt.Graphics g)
paint
in interface BreakoutComponent
g
- Graphics object used to draw with.public abstract void hitBy(BreakoutComponent striker)
hitBy
in interface BreakoutComponent
striker
- BreakoutComponent that struck this componentpublic abstract void act()
act
in interface ActiveBreakoutComponent
|
Breakout Problem Set Javadocs | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |