greenfoot
Class Greenfoot

java.lang.Object
  extended by greenfoot.Greenfoot

public class Greenfoot
extends java.lang.Object

This utility class provides methods to control the simulation and interact with the system.

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:

Version:
2.2
Author:
Davin McCall

Constructor Summary
Greenfoot()
           
 
Method Summary
static void delay(int time)
          Delay the current execution by a number of time steps.
static java.lang.String getKey()
          Get the most recently pressed key, since the last time this method was called.
static int getMicLevel()
          Get the microphone input level.
static MouseInfo getMouseInfo()
          Return a mouse info object with information about the state of the mouse.
static int getRandomNumber(int limit)
          Return a random number between 0 (inclusive) and limit (exclusive).
static boolean isKeyDown(java.lang.String keyName)
          Check whether a given key is currently pressed down.
static boolean mouseClicked(java.lang.Object obj)
          True if the mouse has been clicked (pressed and released) on the given object.
static boolean mouseDragEnded(java.lang.Object obj)
          True if a mouse drag has ended.
static boolean mouseDragged(java.lang.Object obj)
          True if the mouse has been dragged on the given object.
static boolean mouseMoved(java.lang.Object obj)
          True if the mouse has been moved on the given object.
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.
static void playSound(java.lang.String soundFile)
          Play sound from a file.
static void setSpeed(int speed)
          Set the speed of the execution.
static void setWorld(World world)
          Sets the World to run to the one given.
static void start()
          Run (or resume) the execution.
static void stop()
          Pause the execution.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Greenfoot

public Greenfoot()
Method Detail

setWorld

public static void setWorld(World world)
Sets the World to run to the one given. This World will now be the main World that Greenfoot runs with on the next act.

Parameters:
world - The World to switch running to, cannot be null.

getKey

public static java.lang.String getKey()
Get 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 stop()
Pause the execution.


start

public static void start()
Run (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 getMouseInfo()
Return 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.

getMicLevel

public static int getMicLevel()
Get the microphone input level. This level is an approximation of the loudness any noise that is currently being received by the microphone.

Returns:
The microphone input level (between 0 and 100, inclusive).


Greenfoot homepage