ballWorlds.framework
Class VisibleWorld

java.lang.Object
  extended by ballWorlds.framework.BallManagerWorld
      extended by ballWorlds.framework.VisibleWorld
All Implemented Interfaces:
BallManager, Drawable, World, WorldDescriber
Direct Known Subclasses:
WorldWithManyThreads, WorldWithTwoOrThreeThreads

public class VisibleWorld
extends BallManagerWorld
implements World

A World manages the Balls that it contains; a VisibleWorld is a World that just displays the balls, without asking them to act.

A VisibleWorld constructs its visual elements:

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

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

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

Constructor Summary
VisibleWorld(List<World> universe, Dimension size, Color color, JFrame ballWorldFrame)
          Sets the World's size, color, and home frame to the given values.
 
Method Summary
 void drawBalls(Graphics2D graphics, Ball selectedBall)
          Draws the Balls in this World.
 List<World> getAllWorlds()
          Returns a List that contains all the Worlds in the Universe.
 World getAnotherWorld()
          Returns a random other World (or this World if it is the only one).
 Color getColor()
          Returns the Color of this World.
 Shape getShape()
          Returns the Shape (which contains position and size) of this World.
protected  WorldView getView()
          TODO Put here a description of what this method does.
 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.
 Point2D randomPointInWorld()
          Returns a new Point2D that is a random point in this World.
 
Methods inherited from class ballWorlds.framework.BallManagerWorld
addBall, allBallsInWorld, getBallsInWorld, intersectingBall, intersectingBalls, nearestBall, removeBall
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface ballWorlds.BallManager
addBall, allBallsInWorld, intersectingBall, intersectingBalls, nearestBall, removeBall
 

Constructor Detail

VisibleWorld

public VisibleWorld(List<World> universe,
                    Dimension size,
                    Color color,
                    JFrame ballWorldFrame)
Sets the World's size, color, and home frame to the given values.

Constructs the visual elements of this World:

Adds these visual elements to the given frame.

Starts a Thread that repeatedly asks each Ball to act.

Parameters:
universe - The Universe of Worlds
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

isInsideWorldX

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

Specified by:
isInsideWorldX in interface WorldDescriber
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 WorldDescriber
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 WorldDescriber
Returns:
a new Point2D that is at the middle of this World.

randomPointInWorld

public Point2D randomPointInWorld()
Returns a new Point2D that is a random point in this World.

Specified by:
randomPointInWorld in interface WorldDescriber
Returns:
a new Point2D that is random point in this World

drawBalls

public void drawBalls(Graphics2D graphics,
                      Ball selectedBall)
Draws the Balls in this World. This method is synchronized so that when the WorldView triggers a graphics update it doesn't collide with an update to the list of balls triggered by the thread in this class.

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.

getView

protected final WorldView getView()
TODO Put here a description of what this method does.

Returns:

getAllWorlds

public List<World> getAllWorlds()
Description copied from interface: WorldDescriber
Returns a List that contains all the Worlds in the Universe. Each call to this method returns a fresh List object that contains all the World objects known to this World independently from any other List objects in use at the same time. This allows the caller to safely iterate through this List without fear of it being concurrently modified by other Threads (and possibly generating a ConcurrentModification exception).

Specified by:
getAllWorlds in interface WorldDescriber
Returns:
a List that contains all the Worlds in this Universe.

getAnotherWorld

public World getAnotherWorld()
Description copied from interface: WorldDescriber
Returns a random other World (or this World if it is the only one).

Specified by:
getAnotherWorld in interface WorldDescriber
Returns:
a random other World (or this World if it is the only one).