Breakout Problem Set Javadocs

breakout
Class DefaultActiveBreakoutComponent

java.lang.Object
  extended bybreakout.DefaultActiveBreakoutComponent
All Implemented Interfaces:
ActiveBreakoutComponent, BreakoutComponent, java.lang.Runnable
Direct Known Subclasses:
SimpleBall

public abstract class DefaultActiveBreakoutComponent
extends java.lang.Object
implements ActiveBreakoutComponent, java.lang.Runnable

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.

Version:
$Id: DefaultActiveBreakoutComponent.java,v 1.2 2004/03/26 20:39:33 gus Exp $
Author:
benmv@olin.edu
See Also:
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

DefaultActiveBreakoutComponent

public DefaultActiveBreakoutComponent(java.awt.Point loc,
                                      java.awt.Dimension size,
                                      World world,
                                      java.awt.Point direction,
                                      int timeBetweenActs)
Method Detail

run

public void run()
sleeps timeBetweenActs milliseconds, updates position, then invokes act

Specified by:
run in interface java.lang.Runnable

getLocation

public java.awt.Point getLocation()
returns a new Point corresponding to the location of the component's upper-left corner. Changing the point will NOT result in the component's location changing.

Specified by:
getLocation in interface BreakoutComponent
Returns:
New Point corresponding to location of component.

setLocation

public void setLocation(java.awt.Point location)
changes the location of the component's upper left corner to the given location. Copies the given point, so subsequent changes to the argument will not affect the location of the compenent.

Specified by:
setLocation in interface BreakoutComponent
Parameters:
location - Point to locate the component at.

getCenter

public java.awt.Point getCenter()
returns a point that corresponds to the center of the component. getLocation() returns upper-left corner.

Specified by:
getCenter in interface BreakoutComponent
Returns:
Point located at the center of the component.

getSize

public java.awt.Dimension getSize()
returns the Dimensions of the component. Mutating the returned value will NOT change the size of the component.

Specified by:
getSize in interface BreakoutComponent
Returns:
Dimension of the component.

getShape

public java.awt.Shape getShape()
returns a Rectangle at location with size.

Specified by:
getShape in interface BreakoutComponent
Returns:
Shape Rectangle according to components location and size.

kill

public void kill()
Description copied from interface: BreakoutComponent
destroys this component

Specified by:
kill in interface BreakoutComponent

isDead

public boolean isDead()
Description copied from interface: BreakoutComponent
returns dead status of component.

Specified by:
isDead in interface BreakoutComponent

isTransient

public boolean isTransient()
Description copied from interface: BreakoutComponent
returns true if the component is transient.

Specified by:
isTransient in interface BreakoutComponent
Returns:
boolean true if component is transient

setWorld

public void setWorld(World world)
Description copied from interface: BreakoutComponent
sets the world the component lives in

Specified by:
setWorld in interface BreakoutComponent

getDirection

public java.awt.Point getDirection()
returns a new Point corresponding to the direction of the component's motion. The x and y values range from -100 to 100. These values represent change in the x or y value of the location by 1/100 of their value per activation of the component. Changing the point will NOT result in the component's direction changing.

Specified by:
getDirection in interface ActiveBreakoutComponent
Returns:
New Point corresponding to direction of component.

setDirection

public void setDirection(java.awt.Point d)
Sets the direction of motion of the component. See getter or class description for details. Copies the given point, so subsequent changes to the argument will not affect the direction of the compenent.

Specified by:
setDirection in interface ActiveBreakoutComponent

getTimeBetweenActs

public int getTimeBetweenActs()
returns the number of milliseconds between calls to act

Specified by:
getTimeBetweenActs in interface ActiveBreakoutComponent
Returns:
int milliseconds between calls to the act method.

paint

public abstract void paint(java.awt.Graphics g)
subclasses must implement the paint method in order to draw their component.

Specified by:
paint in interface BreakoutComponent
Parameters:
g - Graphics object used to draw with.

hitBy

public abstract void hitBy(BreakoutComponent striker)
subclasses must implement the hitBy method to have the component react to being struck.

Specified by:
hitBy in interface BreakoutComponent
Parameters:
striker - BreakoutComponent that struck this component

act

public abstract void act()
subclasses must implement the act method to have the component interact with the world after moving.

Specified by:
act in interface ActiveBreakoutComponent

Breakout Problem Set Javadocs