greenfoot
Class World

java.lang.Object
  extended by greenfoot.World

public abstract class World
extends java.lang.Object

World is the world that Actors live in. It is a two-dimensional grid of cells.
All Actor are associated with a World and can get access to the world object. The size of cells can be specified at world creation time, and is constant after creation. Simple scenarios may use large cells that entirely contain the representations of objects in a single cell. More elaborate scenarios may use smaller cells (down to single pixel size) to achieve fine-grained placement and smoother animation. The world background can be decorated with drawings or images.

Version:
1.0
Author:
Poul Henriksen, Michael Kolling
See Also:
Actor

Field Summary
(package private) static greenfoot.util.Version VERSION
          Version number of the Greenfoot API
 
Constructor Summary
World(int worldWidth, int worldHeight, int cellSize)
          Construct a new world.
 
Method Summary
 void addObject(greenfoot.Actor object, int x, int y)
          Add an Actor to the world (at the object's specified location).
 greenfoot.GreenfootImage getBackground()
          Return the world's background image.
(package private)  double getCellCenter(int l)
          Returns the center of the cell.
 int getCellSize()
          Return the size of a cell (in pixels).
 java.awt.Color getColorAt(int x, int y)
          Return the color at the center of the cell.
 int getHeight()
          Return the height of the world (in number of cells).
(package private)  int getHeightInPixels()
          Get the height of the world in pixels.
(package private)  java.util.List getIntersectingObjects(greenfoot.Actor actor, java.lang.Class cls)
          Return all the objects that intersect the given object.
(package private)  java.util.List getNeighbours(int x, int y, int distance, boolean diag, java.lang.Class cls)
          Returns the neighbours to the given location.
 java.util.List getObjects(java.lang.Class cls)
          Get all the objects in the world.
If iterating through these objects, you should synchronize on this world to avoid ConcurrentModificationException.
 java.util.List getObjectsAt(int x, int y, java.lang.Class cls)
          Return all objects at a given cell.
(package private)  java.util.Collection getObjectsAtPixel(int x, int y)
           
(package private)  java.util.List getObjectsInDirection(int x0, int y0, int angle, int length, java.lang.Class cls)
          Return all objects that intersect a straight line from the location at a specified angle.
(package private)  java.util.List getObjectsInRange(int x, int y, int r, java.lang.Class cls)
          Returns all objects with the logical location within the specified circle.
(package private)  java.util.List<greenfoot.Actor> getObjectsList()
          Get the list of all objects in the world.
(package private)  greenfoot.Actor getOneIntersectingObject(greenfoot.Actor object, java.lang.Class cls)
           
(package private)  greenfoot.Actor getOneObjectAt(greenfoot.Actor object, int dx, int dy, java.lang.Class cls)
           
(package private) static rmiextension.wrappers.RObject getRObject(java.lang.Object obj)
          Gets the remote reference to the obj.
 int getWidth()
          Return the width of the world (in number of cells).
(package private)  int getWidthInPixels()
          Get the width of the world in pixels.
 boolean isTiled()
          Returns true if the world is tiled.
(package private)  void paintDebug(java.awt.Graphics g)
           
 void removeObject(greenfoot.Actor object)
          Remove an object from the world.
 void removeObjects(java.util.Collection objects)
          Remove a list of objects from the world.
 void setBackground(greenfoot.GreenfootImage image)
          Set a background image for the world.
 void setBackground(java.lang.String filename)
          Set a background image for the world from an image file.
 void setTiled(boolean tiled)
          If set to true, the background image will be tiled to fill out the entire background of the world.
(package private)  void startSequence()
          Used to indicate the start of an animation sequence.
(package private)  int toCellCeil(int i)
           
(package private)  int toCellFloor(int i)
           
(package private)  void updateObjectLocation(greenfoot.Actor object, int oldX, int oldY)
           
(package private)  void updateObjectSize(greenfoot.Actor object)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

static final greenfoot.util.Version VERSION
Version number of the Greenfoot API

Constructor Detail

World

public World(int worldWidth,
             int worldHeight,
             int cellSize)
Construct a new world. The size of the world (in number of cells) and the size of each cell (in pixels) must be specified.

Parameters:
worldWidth - The width of the world (in cells).
worldHeight - The height of the world (in cells).
cellSize - Size of a cell in pixels.
Method Detail

addObject

public void addObject(greenfoot.Actor object,
                      int x,
                      int y)
               throws java.lang.IndexOutOfBoundsException
Add an Actor to the world (at the object's specified location).

Parameters:
object - The new object to add.
Throws:
java.lang.IndexOutOfBoundsException - If the coordinates are outside the bounds of the world.

getBackground

public greenfoot.GreenfootImage getBackground()
Return the world's background image. The image may be used to draw onto the world's background.

Returns:
The background image

getCellCenter

double getCellCenter(int l)
Returns the center of the cell. It should be rounded down with Math.floor() if the integer version is needed.

Parameters:
l - Cell location.
Returns:
Absolute location of the cell center in pixels.

getCellSize

public int getCellSize()
Return the size of a cell (in pixels).


getColorAt

public java.awt.Color getColorAt(int x,
                                 int y)
Return the color at the center of the cell.

Throws:
java.lang.IndexOutOfBoundsException - If the pixel location is not within the world bounds. If there is no background image at the location it will return Color.WHITE.

getHeight

public int getHeight()
Return the height of the world (in number of cells).


getHeightInPixels

int getHeightInPixels()
Get the height of the world in pixels.


getIntersectingObjects

java.util.List getIntersectingObjects(greenfoot.Actor actor,
                                      java.lang.Class cls)
Return all the objects that intersect the given object. This takes the graphical extent of objects into consideration.

Parameters:
actor - An Actor in the world
cls - Class of objects to look for (null or Object.class will find all classes)

getNeighbours

java.util.List getNeighbours(int x,
                             int y,
                             int distance,
                             boolean diag,
                             java.lang.Class cls)
Returns the neighbours to the given location. This method only looks at the logical location and not the extent of objects. Hence it is most useful in scenarios where objects only span one cell.

Parameters:
x - Location
y - Location
distance - Distance in which to look for other objects
diag - Is the distance also diagonal?
cls - Class of objects to look for (null or Object.class will find all classes)
Returns:
A collection of all neighbours found

getObjects

public java.util.List getObjects(java.lang.Class cls)
Get all the objects in the world.
If iterating through these objects, you should synchronize on this world to avoid ConcurrentModificationException.

The objects are returned in their paint order. The first object in the List is the one painted first. The last object is the one painted on top of all other objects.

If a class is specified as a parameter, only objects of that class (or its subclasses) will be returned.

Parameters:
cls - Class of objects to look for ('null' will find all objects).
Returns:
An unmodifiable list of objects.

getObjectsAt

public java.util.List getObjectsAt(int x,
                                   int y,
                                   java.lang.Class cls)
Return all objects at a given cell.

An object is defined to be at that cell if its graphical representation overlaps with the cell at any point.

Parameters:
x - X-coordinate of the cell to be checked.
y - Y-coordinate of the cell to be checked.
cls - Class of objects to look return ('null' will return all objects).

getObjectsAtPixel

java.util.Collection getObjectsAtPixel(int x,
                                       int y)

getObjectsInDirection

java.util.List getObjectsInDirection(int x0,
                                     int y0,
                                     int angle,
                                     int length,
                                     java.lang.Class cls)
Return all objects that intersect a straight line from the location at a specified angle. The angle is clockwise.

Parameters:
x - x-coordinate
y - y-coordinate
angle - The angle relative to current rotation of the object. (0-359)
length - How far we want to look (in cells)
cls - Class of objects to look for (passing 'null' will find all objects).

getObjectsInRange

java.util.List getObjectsInRange(int x,
                                 int y,
                                 int r,
                                 java.lang.Class cls)
Returns all objects with the logical location within the specified circle. In other words an object A is within the range of an object B if the distance between the center of the two objects is less thatn r.

Parameters:
x - Center of the cirle
y - Center of the cirle
r - Radius of the cirle
cls - Class of objects to look for (null or Object.class will find all classes)

getObjectsList

java.util.List<greenfoot.Actor> getObjectsList()
Get the list of all objects in the world. This returns a live list which should not be modified by the caller.


getOneIntersectingObject

greenfoot.Actor getOneIntersectingObject(greenfoot.Actor object,
                                         java.lang.Class cls)

getOneObjectAt

greenfoot.Actor getOneObjectAt(greenfoot.Actor object,
                               int dx,
                               int dy,
                               java.lang.Class cls)

getRObject

static rmiextension.wrappers.RObject getRObject(java.lang.Object obj)
                                         throws bluej.extensions.ProjectNotOpenException,
                                                bluej.extensions.PackageNotFoundException,
                                                java.rmi.RemoteException,
                                                bluej.extensions.ClassNotFoundException
Gets the remote reference to the obj.

IMPORTANT: This code is duplicated in greenfoot.Actor!

Throws:
bluej.extensions.ClassNotFoundException
java.rmi.RemoteException
bluej.extensions.PackageNotFoundException
bluej.extensions.ProjectNotOpenException

getWidth

public int getWidth()
Return the width of the world (in number of cells).


getWidthInPixels

int getWidthInPixels()
Get the width of the world in pixels.


isTiled

public boolean isTiled()
Returns true if the world is tiled.

Returns:
Whether the image is tilled.
See Also:
setTiled(boolean)

paintDebug

void paintDebug(java.awt.Graphics g)

removeObject

public void removeObject(greenfoot.Actor object)
Remove an object from the world.

Parameters:
object - the object to remove

removeObjects

public void removeObjects(java.util.Collection objects)
Remove a list of objects from the world.

Parameters:
objects - A list of Actors to remove.

setBackground

public final void setBackground(greenfoot.GreenfootImage image)
Set a background image for the world. If the image size is larger than the world in pixels, it is clipped. If it is smaller than the world, it is tiled unless specifically stated to do otherwise (see setTiled()). A pattern showing the cells can easily be shown by setting a background image with a size equal to the cell size.

Parameters:
image - The image to be shown
See Also:
setBackground(String), setTiled(boolean)

setBackground

public final void setBackground(java.lang.String filename)
                         throws java.lang.IllegalArgumentException
Set a background image for the world from an image file. Images of type 'jpeg', 'gif' and 'png' are supported. If the image size is larger than the world in pixels, it is clipped. If it is smaller than the world, it is tiled unless specifically stated to do otherwise (see setTiled()). A pattern showing the cells can easily be shown by setting a background image with a size equal to the cell size.

Parameters:
filename - The file holding the image to be shown
Throws:
java.lang.IllegalArgumentException - If the image can not be loaded.
See Also:
setBackground(GreenfootImage), setTiled(boolean)

setTiled

public void setTiled(boolean tiled)
If set to true, the background image will be tiled to fill out the entire background of the world.

Parameters:
tiled - Whether it should tile the image or not.

startSequence

void startSequence()
Used to indicate the start of an animation sequence. For use in the collision checker.

See Also:
CollisionChecker.startSequence()

toCellCeil

int toCellCeil(int i)

toCellFloor

int toCellFloor(int i)

updateObjectLocation

void updateObjectLocation(greenfoot.Actor object,
                          int oldX,
                          int oldY)

updateObjectSize

void updateObjectSize(greenfoot.Actor object)