World++Class

java.lang.Object greenfoot.World
 * greenfoot Class World**

public abstract class Worldextends java.lang.ObjectWorld 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: 2.1 Author: Poul Henriksen, Michael Kolling See Also: Actor

Constructor Summary World(int worldWidth, int worldHeight, int cellSize) Construct a new world. World(int worldWidth, int worldHeight, int cellSize, boolean bounded) Construct a new world.
 * 构造方法**

Method Summary Act method for world.
 * 其他方法**
 * void act**

Add an Actor to the world.
 * void addObject(Actor object, int x, int y)** 添加一个对象object到（x,y)

Return the world's background image.
 * GreenfootImage getBackground 背景图片提取**

Return the size of a cell (in pixels).
 * int getCellSize** 得到一个像素的大小

Return the color at the centre of the cell.
 * java.awt.Color getColorAt(int x, int y)** 设置给定位置的颜色值

int getHeight 得到world对象的高度 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.

Return all objects at a given cell.
 * java.util.List getObjectsAt(int x, int y, java.lang.Class cls)** 得到给定位置的对象

Return the width of the world (in number of cells).
 * int getWidth** 得到world对象的宽度

int numberOfObjects Get the number of actors currently in the world.

Remove an object from the world.
 * void removeObject(Actor object)** 消去对象

Remove a list of objects from the world.
 * void removeObjects(java.util.Collection objects)** 消去对象集合

Repaints the world.
 * void repaint** 重新画出world

Set the act order of objects in the world.
 * void setActOrder(java.lang.Class... classes)** 设置动作顺序

Set a background image for the world.
 * void setBackground(GreenfootImage image)** 设置背景图片

Set a background image for the world from an image file.
 * void setBackground(java.lang.String filename)** 设置背景图片

Set the paint order of objects in the world.
 * void setPaintOrder(java.lang.Class... classes)** 设置着色顺序

This method is called by the Greenfoot system when the execution has started.
 * void started** 动作开始后进行的方法

This method is called by the Greenfoot system when the execution has stopped. Methods inherited from class java.lang.Object clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 * void stopped** 动作结束时进行的方法

下面的没意思，和上面的一样 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.

World public World(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 setBackground public final void setBackground(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.

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

setBackground public final void setBackground(java.lang.String filename) throws java.lang.IllegalArgumentExceptionSet 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.

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)

getBackground public GreenfootImage getBackgroundReturn the world's background image. The image may be used to draw onto the world's background.

Returns: The background image

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.

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

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

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

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

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.

Parameters: classes - The classes in desired paint order

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.

Parameters: classes - The classes in desired act order

addObject public void addObject(Actor object, int x, int y)Add an Actor to the 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.

removeObject public void removeObject(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.

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.

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

numberOfObjects public int numberOfObjectsGet the number of actors currently in the world.

Returns: The number of actors

repaint public void repaintRepaints the world.

act public void actAct method for world. The act method is called by the greenfoot framework at each action step in the environment. The world's act method is called before the act method of any objects in the world. This method does nothing. It should be overridden in subclasses to implement an world's action.

started public void startedThis 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. This default implementation is empty.

stopped public void stoppedThis 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. This default implementation is empty.

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 the center of the cell.

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).