ballworlds.framework
Class World

java.lang.Object
  extended by ballworlds.framework.World
All Implemented Interfaces:
BallEnvironment, BumperEnvironment, CollectionOfBalls, Drawable, Runnable

public class World
extends Object
implements BallEnvironment, BumperEnvironment, CollectionOfBalls, Drawable, Runnable

A World simulates a "world" that contain various kinds of "balls" (and possibly other objects).

A World constructs its visual elements:

A World adds those visual elements to the frame that the World is given.

A World manages its Balls, including animating them by calling each Ball's act method repeatedly.

A World manages its Bumpers and other objects that the World creates.

Author:
David Mutchler, Salman Azhar and others, January 2005. Modified September, 2008.

Constructor Summary
World(Dimension size, Color color, JFrame ballWorldFrame)
          Sets the World's Shape (and related data) and Color to the given values.
 
Method Summary
 void addBall(Ball ballToAdd)
          Adds the given Ball to this World.
 void drawBalls(Graphics2D graphics, Ball selectedBall)
          Draws the Balls in this World.
 Color getColor()
          Returns the Color of this World.
 Shape getShape()
          Returns the Shape (which contains position and size) of this World.
 boolean isInsideWorldX(Point2D p)
          Returns true if the given point's x-coordinate is inside this World.
 boolean isInsideWorldY(Point2D p)
          Returns true if the given point's y-coordinate is inside this World.
 Point2D middleOfWorld()
          Returns a new Point2D that is at the middle of this World.
 Ball nearestBall(Point2D p)
          Returns the Ball in this World that is nearest the given point.
 void removeBall(Ball ballToRemove)
          Removes the given Ball from this World.
 void run()
          Repeatedly asks each Ball to act.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

World

public World(Dimension size,
             Color color,
             JFrame ballWorldFrame)
Sets the World's Shape (and related data) and Color to the given values.

Constructs the visual elements of this World:

Adds those visual elements to the frame that it is given.

Starts a Thread that repeatedly asks each Ball to act.

Parameters:
size - Size of this World (including its ButtonPanel).
color - Color of this World.
ballWorldFrame - JFrame to which the visual elements of this World are to be added.
Method Detail

addBall

public void addBall(Ball ballToAdd)
Adds the given Ball to this World.

Specified by:
addBall in interface BallEnvironment
Parameters:
ballToAdd - the Ball to add to the World.

removeBall

public void removeBall(Ball ballToRemove)
Removes the given Ball from this World.

Specified by:
removeBall in interface BallEnvironment
Parameters:
ballToRemove - the Ball to remove from the World.

isInsideWorldX

public boolean isInsideWorldX(Point2D p)
Returns true if the given point's x-coordinate is inside this World.

Specified by:
isInsideWorldX in interface BallEnvironment
Parameters:
p - the point whose x-coordinate to check
Returns:
true if the given point's x-coordinate is inside this World.

isInsideWorldY

public boolean isInsideWorldY(Point2D p)
Returns true if the given point's y-coordinate is inside this World.

Specified by:
isInsideWorldY in interface BallEnvironment
Parameters:
p - the point whose y-coordinate to check
Returns:
true if the given point's y-coordinate is inside this World.

middleOfWorld

public Point2D middleOfWorld()
Returns a new Point2D that is at the middle of this World.

Specified by:
middleOfWorld in interface BallEnvironment
Returns:
a new Point2D that is at the middle of this World.

nearestBall

public Ball nearestBall(Point2D p)
Returns the Ball in this World that is nearest the given point. Returns null if there are no Balls in this World.

Specified by:
nearestBall in interface CollectionOfBalls
Parameters:
p - the point for which to find the nearest Ball.
Returns:
the Ball in this World that is nearest the given point.

drawBalls

public void drawBalls(Graphics2D graphics,
                      Ball selectedBall)
Draws the Balls in this World.

Specified by:
drawBalls in interface CollectionOfBalls
Parameters:
graphics - the object onto which to draw.
selectedBall - the ball which has been selected (null if none).

getShape

public Shape getShape()
Returns the Shape (which contains position and size) of this World.

Specified by:
getShape in interface Drawable
Returns:
the Shape of this World.

getColor

public Color getColor()
Returns the Color of this World.

Specified by:
getColor in interface Drawable
Returns:
the Color of this World.

run

public void run()
Repeatedly asks each Ball to act.

Specified by:
run in interface Runnable