java.lang.Objectgreenfoot.World
public abstract class World
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.
Actor
Field Summary | |
---|---|
java.util.concurrent.locks.ReentrantReadWriteLock |
lock
Lock used for iterating over actors. |
static int |
READ_LOCK_TIMEOUT
Timeout used for readers attempting to acquire lock |
Constructor Summary | |
---|---|
World(int worldWidth,
int worldHeight,
int cellSize)
Construct a new world. |
Method Summary | |
---|---|
void |
act()
Act method for world. |
void |
addObject(greenfoot.Actor object,
int x,
int y)
Add an Actor to the world. |
greenfoot.GreenfootImage |
getBackground()
Return the world's background image. |
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). |
java.util.List |
getObjects(java.lang.Class<?> cls)
Get all the objects in the world, or all the objects of a particular class. |
java.util.List |
getObjectsAt(int x,
int y,
java.lang.Class cls)
Return all objects at a given cell. |
int |
getWidth()
Return the width of the world (in number of cells). |
boolean |
isTiled()
Returns true if the world is tiled. |
int |
numberOfObjects()
Get the number of actors currently in the world. |
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 |
repaint()
Repaints the world. |
void |
setActOrder(java.lang.Class... classes)
Set the act order of objects in 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 |
setPaintOrder(java.lang.Class... classes)
Set the paint order of objects in the world. |
void |
setTiled(boolean tiled)
If set to true, the background image will be tiled to fill out the entire background of the world. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public java.util.concurrent.locks.ReentrantReadWriteLock lock
public static final int READ_LOCK_TIMEOUT
Constructor Detail |
---|
public World(int worldWidth, int worldHeight, int cellSize)
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 |
---|
public void act()
This method does nothing. It should be overridden in subclasses to implement an world's action.
public void addObject(greenfoot.Actor object, int x, int y)
object
- The new object to add.x
- The x coordinate of the location where the object is added.y
- The y coordinate of the location where the object is added.public greenfoot.GreenfootImage getBackground()
public int getCellSize()
public java.awt.Color getColorAt(int x, int y)
java.lang.IndexOutOfBoundsException
- If the location is not within the world
bounds. If there is no background image at the location it
will return Color.WHITE.getBackground()
public int getHeight()
public java.util.List getObjects(java.lang.Class<?> cls)
If a class is specified as a parameter, only objects of that class (or its subclasses) will be returned.
cls
- Class of objects to look for ('null' will find all objects).
public java.util.List getObjectsAt(int x, int y, java.lang.Class cls)
An object is defined to be at that cell if its graphical representation overlaps with the cell at any point.
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).public int getWidth()
public boolean isTiled()
setTiled(boolean)
public int numberOfObjects()
public void removeObject(greenfoot.Actor object)
object
- the object to removepublic void removeObjects(java.util.Collection objects)
objects
- A list of Actors to remove.public void repaint()
public void setActOrder(java.lang.Class... classes)
Objects of a class not explicitly specified inherit the act order from their superclass.
Objects of classes not listed will act after all objects whose classes have been specified.
classes
- The classes in desired act orderpublic final void setBackground(greenfoot.GreenfootImage image)
image
- The image to be shownsetBackground(String)
,
setTiled(boolean)
public final void setBackground(java.lang.String filename) throws java.lang.IllegalArgumentException
filename
- The file holding the image to be shown
java.lang.IllegalArgumentException
- If the image can not be loaded.setBackground(GreenfootImage)
,
setTiled(boolean)
public void setPaintOrder(java.lang.Class... classes)
Objects of a class not explicitly specified effectively inherit the paint order from their superclass.
Objects of classes not listed will appear below the objects whose classes have been specified.
classes
- The classes in desired paint orderpublic void setTiled(boolean tiled)
tiled
- Whether it should tile the image or not.