Class ThreadWorld

java.lang.Object
  extended by greenfoot.World
      extended by ThreadWorld

public abstract class ThreadWorld
extends greenfoot.World

class of Threadfoot

Counterpart of Greenfoot-class World. Working with Threadfoot you should use always this class instead of class World.

Remark 1: If you override the methods "started" or "stopped" in a derived class you have to call the ThreadWorld-methods "started" respectively "stopped" within those methods!

Remark 2: Do not use method "setBackground". Use "setImage" instead. (World.setBackground is declared final and could not be overridden.)

Version:
(2.0) 2010/12/08
Author:
Dietrich Boles (Uni Oldenburg, Germany)

Constructor Summary
ThreadWorld(int worldWidth, int worldHeight, int cellSize)
          Construct a new world.
ThreadWorld(int worldWidth, int worldHeight, int cellSize, boolean bounded)
          Construct a new world.
 
Method Summary
 void act()
          meaningless in Threadfoot
 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 centre 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).
 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 setImage(greenfoot.GreenfootImage image)
          Set a background image for the world.
 void setImage(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 started()
          This method is called by the Greenfoot system when the execution has started.
 void stopped()
          This method is called by the Greenfoot system when the execution has stopped.
 
Methods inherited from class greenfoot.World
setBackground, setBackground
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadWorld

public ThreadWorld(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.

ThreadWorld

public ThreadWorld(int worldWidth,
                   int worldHeight,
                   int cellSize,
                   boolean bounded)
Construct a new world. The size of the world (in number of cells) and the size of each cell (in pixels) must be specified. This constructor allows the option of creating an unbounded world, which actors can move outside the boundaries of.

Parameters:
worldWidth - The width of the world (in cells).
worldHeight - The height of the world (in cells).
cellSize - Size of a cell in pixels.
bounded - Should actors be restricted to the world boundary?
Method Detail

act

public final void act()
meaningless in Threadfoot

Overrides:
act in class greenfoot.World
See Also:
World.act()

addObject

public void addObject(greenfoot.Actor object,
                      int x,
                      int y)
Add an Actor to the world.

Overrides:
addObject in class greenfoot.World
Parameters:
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.

getBackground

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

Overrides:
getBackground in class greenfoot.World
Returns:
The background image

getCellSize

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

Overrides:
getCellSize in class greenfoot.World

getColorAt

public java.awt.Color getColorAt(int x,
                                 int y)
Return the color at the centre of the cell. To paint a color, you need to get the background image for the world and paint on that.

Overrides:
getColorAt in class greenfoot.World
Parameters:
x - The x coordinate of the cell.
y - The y coordinate of the cell.
Throws:
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.
See Also:
getBackground()

getHeight

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

Overrides:
getHeight in class greenfoot.World

getObjects

public java.util.List getObjects(java.lang.Class cls)
Get all the objects in the world, or all the objects of a particular class.

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

Overrides:
getObjects in class greenfoot.World
Parameters:
cls - Class of objects to look for ('null' will find all objects).
Returns:
A 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.

Overrides:
getObjectsAt in class greenfoot.World
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).

getWidth

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

Overrides:
getWidth in class greenfoot.World

numberOfObjects

public int numberOfObjects()
Get the number of actors currently in the world.

Overrides:
numberOfObjects in class greenfoot.World
Returns:
The number of actors

removeObject

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

Overrides:
removeObject in class greenfoot.World
Parameters:
object - the object to remove

removeObjects

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

Overrides:
removeObjects in class greenfoot.World
Parameters:
objects - A list of Actors to remove.

repaint

public void repaint()
Repaints the world.

Overrides:
repaint in class greenfoot.World

setActOrder

public void setActOrder(java.lang.Class... classes)
Set the act order of objects in the world. Act order is specified by class: objects of one class will always act before objects of some other class. The order of objects of the same class cannot be specified. Objects of classes listed first in the parameter list will act before any objects of classes listed later.

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.

Overrides:
setActOrder in class greenfoot.World
Parameters:
classes - The classes in desired act order

setImage

public void setImage(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. A pattern showing the cells can easily be shown by setting a background image with a size equal to the cell size.

Do not use method "setBackground". Use "setImage" instead.

Parameters:
image - The image to be shown
See Also:
World.setBackground(String)

setImage

public void setImage(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. A pattern showing the cells can easily be shown by setting a background image with a size equal to the cell size.

Do not use method "setBackground". Use "setImage" instead.

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

setPaintOrder

public void setPaintOrder(java.lang.Class... classes)
Set the paint order of objects in the world. Paint order is specified by class: objects of one class will always be painted on top of objects of some other class. The order of objects of the same class cannot be specified. Objects of classes listed first in the parameter list will appear on top of all objects of classes listed later.

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.

Overrides:
setPaintOrder in class greenfoot.World
Parameters:
classes - The classes in desired paint order

started

public void started()
This method is called by the Greenfoot system when the execution has started. This method can be overridden to implement custom behaviour when the execution is started.

If you override this method in a derived class you have to call this method within those methods!

Overrides:
started in class greenfoot.World
See Also:
World.started()

stopped

public void stopped()
This method is called by the Greenfoot system when the execution has stopped. This method can be overridden to implement custom behaviour when the execution is stopped.

If you override this method in a derived class you have to call this method within those methods!

Overrides:
stopped in class greenfoot.World
See Also:
World.stopped()