Greenfoot+class

Class Greenfoot java.lang.Object greenfoot.Greenfoot
 * greenfoot**

public class Greenfoot extends java.lang.ObjectThis utility class provides methods to control the simulation and interact with the system.

Greenfoot类提供模拟和系统交互的方法，非常关键.

Key names Part of the functionality provided by this class is the ability to retrieve keyboard input. The methods getKey and isKeyDown are used for this and they return/understand the following key names:

所有这些功能中最主要的是能捕捉键盘和鼠标动作的方法--**getKey（）和isKeyDown（）**

•"a", "b", .., "z" (alphabetical keys), "0".."9" (digits), most punctuation marks. getKey also returns uppercase characters when appropriate. •"up", "down", "left", "right" (the cursor keys) •"enter", "space", "tab", "escape", "backspace" •"F1", "F2", .., "F12" (the function keys)

Version: 2.1 Author: Davin McCall

构造方法 Constructor Summary 具体方法： Method Summary Delay the current execution by a number of time steps.
 * Greenfoot**
 * static void delay(int time)** 延迟现有动作time步后执行

Get the most recently pressed key, since the last time this method was called.
 * static java.lang.String getKey** 捕捉按下的键盘的字母或符号名

Return a mouse info object with information about the state of the mouse.
 * static MouseInfo getMouseInfo** 捕捉鼠标的状态

Return a random number between 0 (inclusive) and limit (exclusive).
 * static int getRandomNumber(int limit)** 得到0-limit之间的随机数

Check whether a given key is currently pressed down.
 * static boolean isKeyDown(java.lang.String keyName)** 检查keyName健是否被按下

True if the mouse has been clicked (pressed and released) on the given object.
 * static boolean mouseClicked(java.lang.Object obj)** 检查对象obj是否被鼠标点击又释放

True if a mouse drag has ended.
 * static boolean mouseDragEnded(java.lang.Object obj)** 检查鼠标对对象obj的拖拽是否停止

True if the mouse has been dragged on the given object.
 * static boolean mouseDragged(java.lang.Object obj)** 检查对象obj是否被鼠标拖拽

True if the mouse has been moved on the given object.
 * static boolean mouseMoved(java.lang.Object obj)** 检查鼠标是否移动到对象obj上

True if the mouse has been pressed (changed from a non-pressed state to being pressed) on the given object.
 * static boolean mousePressed(java.lang.Object obj)** 检查对象obj是否被鼠标点击

Play sound from a file.
 * static void playSound(java.lang.String soundFile)** 播放soundFile音乐文件

Set the speed of the execution.
 * static void setSpeed(int speed)** 设置全部对象的运行速度

Run (or resume) the execution. Pause the execution. Methods inherited from class java.lang.Object clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 * static void start** 开始执行
 * static void stop** 结束执行

Constructor Detail Greenfoot public GreenfootMethod Detail getKey public static java.lang.String getKeyGet the most recently pressed key, since the last time this method was called. If no key was pressed since this method was last called, it will return null. If more than one key was pressed, this returns only the most recently pressed key.

Returns: The name of the most recently pressed key

isKeyDown public static boolean isKeyDown(java.lang.String keyName)Check whether a given key is currently pressed down.

Parameters: keyName - The name of the key to check Returns: True if the key is down

delay public static void delay(int time)Delay the current execution by a number of time steps. The size of one time step is defined by the Greenfoot environment (the speed slider).

See Also: setSpeed(int)

setSpeed public static void setSpeed(int speed)Set the speed of the execution.

Parameters: speed - The new speed. the value must be in the range (1..100)

stop public static void stopPause the execution.

start public static void startRun (or resume) the execution.

getRandomNumber public static int getRandomNumber(int limit)Return a random number between 0 (inclusive) and limit (exclusive).

playSound public static void playSound(java.lang.String soundFile)Play sound from a file. The following formats are supported: AIFF, AU and WAV. The file name may be an absolute path, a base name for a file located in the project directory or in the sounds directory of the project directory.

Parameters: soundFile - Typically the name of a file in the sounds directory in the project directory. Throws: java.lang.IllegalArgumentException - If the sound can not be loaded.

mousePressed public static boolean mousePressed(java.lang.Object obj)True if the mouse has been pressed (changed from a non-pressed state to being pressed) on the given object. If the parameter is an Actor the method will only return true if the mouse has been pressed on the given actor. If there are several actors at the same place, only the top most actor will receive the press. If the parameter is a World then true will be returned if the mouse was pressed on the world background. If the parameter is null, then true will be returned for any mouse press, independent of the target pressed on.

Parameters: obj - Typically one of Actor, World or null Returns: True if the mouse has been pressed as explained above

mouseClicked public static boolean mouseClicked(java.lang.Object obj)True if the mouse has been clicked (pressed and released) on the given object. If the parameter is an Actor the method will only return true if the mouse has been clicked on the given actor. If there are several actors at the same place, only the top most actor will receive the click. If the parameter is a World then true will be returned if the mouse was clicked on the world background. If the parameter is null, then true will be returned for any click, independent of the target clicked on.

Parameters: obj - Typically one of Actor, World or null Returns: True if the mouse has been clicked as explained above

mouseDragged public static boolean mouseDragged(java.lang.Object obj)True if the mouse has been dragged on the given object. The mouse is considered to be dragged on an object if the drag started on that object - even if the mouse has since been moved outside of that object. If the parameter is an Actor the method will only return true if the drag started on the given actor. If there are several actors at the same place, only the top most actor will receive the drag. If the parameter is a World then true will be returned if the drag action was started on the world background. If the parameter is null, then true will be returned for any drag action, independent of the target clicked on.

Parameters: obj - Typically one of Actor, World or null Returns: True if the mouse has been dragged as explained above

mouseDragEnded public static boolean mouseDragEnded(java.lang.Object obj)True if a mouse drag has ended. This happens when the mouse has been dragged and the mouse button released. If the parameter is an Actor the method will only return true if the drag started on the given actor. If there are several actors at the same place, only the top most actor will receive the drag. If the parameter is a World then true will be returned if the drag action was started on the world background. If the parameter is null, then true will be returned for any drag action, independent of the target clicked on.

Parameters: obj - Typically one of Actor, World or null Returns: True if the mouse has been dragged as explained above

mouseMoved public static boolean mouseMoved(java.lang.Object obj)True if the mouse has been moved on the given object. The mouse is considered to be moved on an object if the mouse pointer is above that object. If the parameter is an Actor the method will only return true if the move is on the given actor. If there are several actors at the same place, only the top most actor will receive the move. If the parameter is a World then true will be returned if the move was on the world background. If the parameter is null, then true will be returned for any move, independent of the target under the move location.

Parameters: obj - Typically one of Actor, World or null Returns: True if the mouse has been moved as explained above

getMouseInfo public static MouseInfo getMouseInfoReturn a mouse info object with information about the state of the mouse.

Returns: The info about the current state of the mouse. Or null if nothing has happened with the mouse since it was last checked.