Package greenfoot

Class Greenfoot

java.lang.Object
greenfoot.Greenfoot

public class Greenfoot extends 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:

  • "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", "shift", "control"
  • "F1", "F2", .., "F12" (the function keys)
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    ask(String prompt)
    Get input from the user (and freeze the scenario while we are waiting).
    static void
    delay(int time)
    Delay the current execution by a number of time steps.
    static String
    Get the most recently pressed key, since the last time this method was called.
    static int
    Get the microphone input level.
    static MouseInfo
    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(String keyName)
    Check whether a given key is currently pressed down.
    static boolean
    True if the mouse has been released (changed from pressed to non-pressed) on the given object.
    static boolean
    True if a mouse drag has ended.
    static boolean
    True if the mouse is currently being dragged on the given object.
    static boolean
    True if the mouse has been moved on the given object.
    static boolean
    True if the mouse has been pressed (changed from a non-pressed state to being pressed) on the given object.
    static void
    playSound(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
    Run (or resume) the execution.
    static void
    Pause the execution.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Greenfoot

      public Greenfoot()
  • Method Details

    • 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 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(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).
      Parameters:
      time - The number of steps the delay will last.
      See Also:
    • 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).
      Parameters:
      limit - An upper limit which the returned random number will be smaller than.
      Returns:
      A random number within 0 to (limit-1) range.
    • playSound

      public static void playSound(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:
      IllegalArgumentException - If the sound can not be loaded.
    • mousePressed

      public static boolean mousePressed(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(Object obj)
      True if the mouse has been released (changed from pressed to non-pressed) 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. Note that mouseClicked does not require the press to have occurred on the parameter object, just that the release did.
      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(Object obj)
      True if the mouse is currently being 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(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(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 the mouse cursor is outside the world boundary (unless being dragged).
    • 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).
    • ask

      public static String ask(String prompt)
      Get input from the user (and freeze the scenario while we are waiting). The prompt String parameter will be shown to the user (e.g. "How many players?"), and the answer will be returned as a String. If you want to ask for a number, you can use methods like Integer.parseInt to turn the returned String into a number.

      This method can only be used when a world is in place and the scenario is running. It returns null if that is not the case, or if the scenario is reset while the prompt is being shown.

      Parameters:
      prompt - The prompt to show to the user.
      Returns:
      The string that the user typed in.