Class Marble

java.lang.Object
  |
  +--Marble
Direct Known Subclasses:
OtherMarble, ShooterMarble

public abstract class Marble
extends java.lang.Object

The abstract class for the Marble object. Based on the Ball class implemented by Steve Revilak

Version:
1.0
Author:
Andi Sutedja

Field Summary
protected  java.awt.Color color
          The color of the marble.
protected static double FRICTION
          A change to velocity; applied each time the marble moves.
protected static int RADIUS
          Radius for each Marble is a fixed 6 pixels.
protected static int TERMINAL_VELOCITY
          The fastest that the marble is allowed to move (in pixels/move).
protected  int x
          The x coordinate of the vertex of the marble.
protected  int y
          The y coordinate of the vertex of the marble.
 
Constructor Summary
Marble(java.awt.Color c, int ringCenterX, int ringCenterY, int ringRadius)
          Construct a new marble with the specified color, ringCenterX, ringCenterY, and ringRadius.
 
Method Summary
 void applyForce(double magnitude, double direction)
          Apply a force to this marble (causing it to move).
abstract  void draw(java.awt.Graphics g)
          Ask this marble to draw itself on the given Graphics object.
 java.awt.Color getColor()
          Get the color of this marble.
 double getDirection()
          Get this marble's direction of travel (in radians).
 java.util.Enumeration getPathEnumerator()
          Return an object that will enumerate the Points through which this marble will pass during its next move.
 double getVelocity()
          Get this marble's current velocity (the number of pixels it moves between redraws).
 java.awt.Point getVertex()
          Get the location (in Point) of this marble.
 boolean isMoving()
          Check if this marble is moving.
 boolean isOutsideRing()
          Check if this marble is outside the boundary of the ring.
 boolean isTouching(Marble other)
          Check if this marble is touching the other marble.
 boolean isTouching(java.awt.Point p)
          Check if this marble is touching the given point.
 boolean isTouching(java.awt.Point p, double distance)
          Check if this marble is within the distance distance of the given point.
 boolean move()
          Move this marble, by having it recompute its coordinate, adjusting speed and velocity as necessary.
 void setVertex(double x, double y)
          Set the location of this marble, after rounding x and y to integers.
 void setVertex(int x, int y)
          Set the location of this marble.
 java.lang.String toString()
          Get the string representation of this marble.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

RADIUS

protected static final int RADIUS
Radius for each Marble is a fixed 6 pixels. All marbles have the same weight.

x

protected int x
The x coordinate of the vertex of the marble.

y

protected int y
The y coordinate of the vertex of the marble.

color

protected java.awt.Color color
The color of the marble.

TERMINAL_VELOCITY

protected static final int TERMINAL_VELOCITY
The fastest that the marble is allowed to move (in pixels/move).

FRICTION

protected static final double FRICTION
A change to velocity; applied each time the marble moves.
Constructor Detail

Marble

public Marble(java.awt.Color c,
              int ringCenterX,
              int ringCenterY,
              int ringRadius)
Construct a new marble with the specified color, ringCenterX, ringCenterY, and ringRadius. The marble will limit its movement to the area whose dimensions are given by ringCenterX, ringCenterY, and ringRadius. The marble is considered to be within the ring as long as the distance between the marble and the center of the ring is less than or equal to the (radius of the ring - radius of the marble).
Parameters:
c - the color of this Marble
ringCenterX - the x coordinate of the center of the ring
ringCenterY - the y coordinate of the center of the ring
ringRadius - the radius of the ring
Method Detail

getVertex

public final java.awt.Point getVertex()
Get the location (in Point) of this marble.
Returns:
A point representing the location of this marble.

getColor

public final java.awt.Color getColor()
Get the color of this marble.
Returns:
the Color of this marble.

setVertex

public final void setVertex(int x,
                            int y)
Set the location of this marble.
Parameters:
x - the x coordinate of this marble
y - the y coordinate of this marble

setVertex

public final void setVertex(double x,
                            double y)
Set the location of this marble, after rounding x and y to integers.
Parameters:
x - the new x coordinate of this marble
y - the new y coordinate of this marble

getDirection

public final double getDirection()
Get this marble's direction of travel (in radians).
Returns:
the direction in the range (0, 2pi), or -1 if the ball is not moving
See Also:
MotionUtils.java

isMoving

public final boolean isMoving()
Check if this marble is moving.
Returns:
true if this marble is in motion (has velocity > 0), false otherwise

getVelocity

public final double getVelocity()
Get this marble's current velocity (the number of pixels it moves between redraws).
Returns:
the current velocity of this marble

isTouching

public final boolean isTouching(Marble other)
Check if this marble is touching the other marble.
Parameters:
other - the other marble
Returns:
true if this marble is touching the other marble, false otherwise

isTouching

public final boolean isTouching(java.awt.Point p,
                                double distance)
Check if this marble is within the distance distance of the given point.
Parameters:
p - the point to test
distance - the distance around the point
Returns:
true if this marble is within the distance of the given point, false otherwise

isTouching

public final boolean isTouching(java.awt.Point p)
Check if this marble is touching the given point.
Parameters:
p - the point to test
Returns:
true if this marble is touching the given point, false otherwise

isOutsideRing

public final boolean isOutsideRing()
Check if this marble is outside the boundary of the ring. The marble is considered to be outside of the ring if the distance between the marble's vertex and the center point of the ring is greater than the ring's radius.
Returns:
true if this marble is outside the ring, false otherwise

toString

public java.lang.String toString()
Get the string representation of this marble.
Overrides:
toString in class java.lang.Object
Returns:
the string representation of this marble, describing its color, position, velocity, and direction

move

public boolean move()
Move this marble, by having it recompute its coordinate, adjusting speed and velocity as necessary.
Returns:
true if this marble's position changed, false otherwise.

applyForce

public void applyForce(double magnitude,
                       double direction)
Apply a force to this marble (causing it to move). Note that a marble has a "TERMINAL_VELOCITY", which it will not exceed, regardless of the amount of force applied.
Parameters:
magnitude - the amount of force to apply
direction - the direction in which to apply the force, (0 <= direction <= 2pi).

draw

public abstract void draw(java.awt.Graphics g)
Ask this marble to draw itself on the given Graphics object. Deriving classes should provide their own implementations, suitable for how the marble should look.
Parameters:
g - the graphics object onto which the marble should draw itself

getPathEnumerator

public final java.util.Enumeration getPathEnumerator()
Return an object that will enumerate the Points through which this marble will pass during its next move. Points will be given at intervals equal to the marble's radius, up to and including the distance that will be covered by move(). The purpose of the enumerator is to provide a finer degree of detail about the path taken by a marble. Namely, if the marble is traveling at a very high velocity, the distance covered by move() might be great enough that a marble would "pass through" something directly in its path.
Returns:
an object that implements the Enumeration interface