Getting Started with Java Using Greenfoot

In this tutorial you l earn how to create 2D games using basic Java programming in this self-paced, online course. No programming experience required! We will define and program a game using the Greenfoot development software.

Time to Complete

Approximately 12 hours to complete the lessons.

Prepare for this course

This lesson will provide an overview of system requirements, show you how to download necessary software, and describe the purpose for using Greenfoot tools to learn Java.

Lesson Objectives

In this lesson you will learn to:

What is Java?

What is Greenfoot?

Java is designed for production systems

The Java programming language, like C++ and other languages, is designed for creating code that runs in production systems like Amazon.com, Facebook, and Instagram.
The language and syntax can be hard to learn, especially for beginners. Below is some sample Java code in the Eclipse development environment.

Java Eclipse EE

2d Actor objects

Greenfoot uses two-dimensional (“2D”) Actor objects. Programmers define the Actor objects and then drags an instance of them onto a world to create a scenario. Putting a visual representation of an object onto the world is referred to as creating an instance of an object. Look at the image below. In this image you can see that bee and butterfly instances have been placed onto the Greenfoot world. The world in this example is displayed as a grid with vertical and horizontal lines. The bee and butterfly objects are programmed in a game-like fashion to move around the world. The objects, the world, and the programming rules are what defines the scenario.

2D Demo

Program execution controls

There are two other important areas in the Greenfoot image above: The program execution controls and the class display. The program execution controls let you run, pause, stop, or step through a scenario.

2D Demo

The class display is used to display the Actor object names, known as classes, in a hierarchical fashion.

2D Demo Class Display

Example: Object, class, and instance

The programming statements that control the actions of the instances are actually written in each of the 3 classes. For example, you write the programming statements to move the butterflies around the scenario in the butterfly class. Every butterfly instance will execute the programming statements within the butterfly class.

To create this scenario, we will add many flower, mushroom, and butterfly objects to the world. We will also add one bee object to the world that will be controlled by the person playing the game. The three actor objects that need to be created are Bee, Flower, Mushroom, and Butterfly. There will also be grass objects in the world that act as the scenery, as well as an object to tell us what the instructions are for the game.

Objects are instances of a class.

2D Instances

Object orientation is a programming paradigm that represents concepts as objects that have attributes and methods.

Syntax is the term used to describe the symbols and words that form a programming statement.

Code is a term used to refer to one or more programming statements or programs.

A scenario is the term used to define the objects, the world, and the programming rules for a graphical program created in Greenfoot.

A world in Greenfoot is the area in which objects interact.

A class is a defined object that is shown in the Greenfoot class display.

Programming Actor objects

Greenfoot API

You use the Greenfoot Application Programming Interface (API) to write the programming statements for the Actor objects. Clicking twice on an actor object will display the API for the object.

Summary

Greenfoot is a visual and interactive program that uses object orientation with Java to build games, simulations, and other graphical programs. Students who are new to the Java programming language enjoy using Greenfoot because the interactive development environment is fun and easy to use. By learning Greenfoot, you are actually learning how to program! Greenfoot is also used at many colleges and universities in Introduction to Programming courses.

Visit the Greenfoot website to learn more about Greenfoot: http://www.greenfoot.org/overview

Check system requirements

This online course includes practice exercises that you can work on to reinforce the concepts being taught. You will need to have the Java Development Kit (JDK) and Greenfoot installed on your machine. You will also need ZIP file extraction software to view the example scenarios provided in this course. Use the checklist below to ensure that your system meets the minimum system requirements for using Greenfoot.

Instructions:

  1. Review the system requirements checklist below.
  2. Check your system to ensure it meets the system requirements listed in the checklist.

Processor

Pentium 4 or higher

Operating System

One of the following:

Browsers

Any modern browser that supports Java:

Memory

Free Disk Space

Graphics

Sound Card

Software

*Note: This is required to view the videos in this course. It is not required to use Greenfoot.

Download the Java Development Kit

The Java Development Kit (JDK) is required to use Greenfoot. You should download and install it first before downloading and installing Greenfoot.

Steps to download the Java Development Kit

  1. Open any Internet Browser and type the following URL in the address bar: http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. From the Oracle Technology Network website, click Java Platform (JDK).
  3. From the Java SE Development Kit 7 Downloads page, review the License Agreement.
  4. Accept or Decline the License Agreement (if you decline, the software will not download).
  5. Locate and select the Java SE Development Kit for your computer operating system.
  6. When prompted, save the file to a location you easily remember. Click OK.

Steps to launch the JDK executable file

  1. Locate the executable file and double click it to execute.
    Launch Executable Screenshot
  2. When prompted to run the executable file, click Run
    Run Executable Screenshot

Steps to run the JDK Installation Wizard

  1. From the Installation Wizard dialog box, click Next>.

JDK Wizard Screenshot

  • To accept the default settings, Click Next>.
  • When prompted for installation location, click Next>. If prompted, close the Internet browser window.
  • Click Continue to acknowledge the successful installation.
  • You will be prompted to set-up the Software Developer Kit (SDK). Click Next>.
  • When prompted for the installation location, click Next>.
  • When the set-up is complete, click Close.
  • Watch this video to see an example installation of the Java Development Kit (JDK).

    Instructions:
    Visit this link to download and install the Java Development Kit: http://www.oracle.com/technetwork/java/javase/downloads/index.html

    Download Greenfoot

    The Java Development Kit (JDK) is required to use Greenfoot. Ensure that you download and installed the JDK prior to downloading and installing Greenfoot.

    Steps to download Greenfoot

    1. Open any Internet Browser and type the following URL in the address bar: http://www.greenfoot.org
    2. Select The Software link.

    Greenfoot Software

    Locate and select the correct version for your computer.

    Software Version

  • When prompted, click Save to save the Greenfoot Installer file.
  • Steps to launch the Greenfoot Installer Wizard

    1. When the download is complete, either select Run or Open Folder, then double-click the executable file.

    Download Complete

  • A Greenfoot Setup window will appear.
  • Steps to install Greenfoot

    1. From the Installer Wizard, select the Next option.
    2. You may be asked who to install this for. Select the required installation setting, then select the Next option.

    Installation Setting

    Review the file Associations settings and select the Next option.

    File Associations Settings

  • To accept the default setting for the installation Destination Folder, select the Next> option.
  • To begin the installation, click Install. A progress window will display while Greenfoot is installed on your computer.
  • To complete the installation, click Finish.
  • Steps to launch Greenfoot

    1. After Greenfoot is installed, the Greenfoot icon will appear on your desktop.

    Alice 3 Icon Screenshot

  • Double-click the icon to launch Greenfoot.
  • Greenfoot download and installation support links

    Join the forum to get help from other users:hhttp://www.greenfoot.org/topics

    Watch this video to see an example installation of the Greenfoot software.

    Instructions:

    Summary

    In this lesson you have learned to:

    You are now ready to continue learning about Greenfoot. You can practice all of the actions described in this course using Greenfoot. If you continue through the lessons sequentially, you can use the Greenfoot files you are creating and saving as instructed. If you want to jump to a specific lesson and practice the actions described, you will need to download the practice file provided, as many of the actions in each lesson build upon a previous lesson.

    A single example will be used throughout the course to demonstrate the actions in Greenfoot. Another single example will be used to complete all practice activities.

    Get started with Greenfoot

    Lesson Objectives

    In this lesson you will learn to:

    Watch this video to see a preview of what you will learn in this lesson.

    Understand the Scene editor

    An initial scene is the starting point of your game. It has three components:

    The initial scene is the first scene of a game where you select the background template for the world and position the moving and non-moving objects.

    Below is a graphic depicting components of an initial scene. The components include:

    Initial Scene

    Watch this video to better understand the components of the Scene editor.

    Open and examine an existing scenario

    By opening an existing scenario, we can examine its components.

    Launch Greenfoot

    To launch Greenfoot, double-click the Greenfoot icon on your desktop. If you do not have a Greenfoot icon on your desktop, locate the Greenfoot program file in the folder in which you downloaded the Greenfoot software.

    Greenfoot Icon

    Greenfoot IDE

    The Greenfoot interactive development environment (IDE) is made up of three main areas: the class display, the world, and the execution controls. Below is the Greenfoot IDE.

    Greenfoot IDE

    The World

    The world is where the action for your scenario will take place and where the user will interact with your scenario.

    Greenfoot World

    The Greenfoot World coordinate system starts at (0,0) at the top left. You can write code that specifies the size, height, and cell size of the world.

    Coordinate System

    The class display

    The class display is where all of the Actor objects, or classes, involved with the scenario are displayed. Each class has a Code editor where you will program the objects in your scenario to move and perform other actions.

    Class Display

    A class contains the specifications that define the appearance and movement of an object. The class provides instructions to Greenfoot on how to create and display instances (also called objects) when they are added to your scenario.

    The execution controls

    The execution controls are used to run and control the scenario.

    Class Display

    Open a scenario in Greenfoot

    Steps to open a scenario in Greenfoot:

    Class Display

    1. Double-click the Greenfoot icon to launch Greenfoot.
    2. Locate the folder on your computer where you store your Greenfoot scenarios. Select the scenario, then click Open.
    3. The scenario will open in a new window.

    Watch this video to learn how to open an existing scenario in Greenfoot.

    Download:

    The activity below requires that you start with the Jungle_L1S3 scenario. Download this file and extract its contents to a folder on your computer before you complete this activity.

    Instructions:

    1. Create a folder on your computer, hard drive, or USB drive named “Greenfoot Course Scenarios” where you will save all of the scenarios that you create in this course.
    2. Download the L1S3_Jungle.zip file.
    3. Open the zip file. Copy the L1S3_Jungle folder contained inside of the zip file to the Greenfoot Course Scenarios folder on your computer.
    4. Locate the Greenfoot program on your computer. Double-click the Greenfoot icon to launch Greenfoot.
    5. From the Scenario menu, select Open.
    6. Locate the L1S3_Jungle scenario that you downloaded to your computer. Select the file, then select Open.
    7. Locate and examine the execution controls, class display, and world. Count how many classes are in this scenario.

    Add an instance to a scenario

    After opening an existing scenario, the next step is to add the instances that will move and act into the scenario.

    Scene editor

    The Scene editor is where you will add and position the instances that will move and act in the scenario.

    A class defines the actions and the attributes of an object. Every object placed in a scenario is an instance of the class from which it was created.

    In the Scene editor, you can:

    Below, the scene contains non-moving scenery instances (trees, rocks, bananas) and moving instances (elephant, hippo, lemur).

    Jungle Scenario

    A class contains the specifications that define the actions and attributes of an object. The class provides instructions to Greenfoot for how to create and display instances when they are added to the world.

    Instances are the objects from a class that act in the scenario.

    Steps to add an instance of a class to the world


    1. Right click on the name of the class.
    2. Select the new [class name()] option.
      Select Class
    3. You should be able to see the instance on your cursor. Drag the instance into the world with your cursor and drop it onto the world.
      Drag Instance

    Watch this video to learn how to add an instance of a class to the world.

    Download:

    The activity below requires that you start with the project file that was saved in the previous topic. Download this Greenfoot project file if you did not complete the previous topic's Try it activity.


    Instructions:

    1. Open Greenfoot.
    2. Open the Jungle scenario you saved in the previous topic. If you did not complete the Try It activity from the previous topic, download the scenario file by clicking the download link above (make sure to copy the contents of the zip file to the Greenfoot Course Scenarios folder on your computer).
    3. Once the scenario is opened in Greenfoot, add one hippo, one elephant, and one lemur to the world. Then, add several bananas, rocks, and trees to the world.
    4. Save the scenario.

    Write a programming instruction and run the scenario

    The Code editor is where you will program the instructions for how the instances of a class should act in your scenario. Opening the Code editor for a class displays all of the programming instructions for the class. Programming instructions are organized into methods in the class display. A method defined in a class comprises one or more programming statements.

    By default, each class has a method called act(). This method contains all of the actions that will execute when the execution controls are selected in the scenario. These actions can be simple programming statements, such as move forward, or they could also be others that you create, such as a method to control an object using keyboard keys.

    For example, below is the Code editor for a Bee class. Right now, the programmer has not written any code for the bee in the act() method. If we clicked the Run button in the scenario, the bee would stand still.


    Code Editor

    For example, if we programmed a Bee to fly around the world, the bee instance would need to receive those instructions on how to fly through the act() method. Below, we have programmed the bee to continuously move forward 5 steps and turn 5 degrees when the scenario is run. This commands the bee to turn in circles.


    Act Method

    The act() method described

    The act() method comprises a method signature and a body. The method signature for the act() method is the name act, followed by an open and closed parentheses. The method body is contained within the curly braces. Programming statements within the curly braces are called the body of the method.


    Act Method Body

    Example: Methods

    For example, the Actor class has methods such as:

    Example: Coding the act() method

    Enter methods in the act() method to program the instances of the class to act. For example, you can program an instance to move a certain number of steps forward, then turn a certain number of degrees.


    Act Method Coded

    Before you can run the scenario to see what happens, you have to click the Compile button that displays below the class display.

    The process of compilation translates the code you write into machine code that the computer can understand.

    Compilation

    Compilation is required whenever the code of a class is modified or when a new class is created. You can identify when compilation is required by the striped appearance of one or more classes. After successful compilation, the striped appearance of a class will be removed. When the compile button generates an error message, you need to go back to your code to determine what has been typed incorrectly. Correct any mistakes, then click the compile button again to obtain a successful compilation.


    Compilation

    After you have added and positioned instances in your world, the next step is to run the scenario to test how the programming instruction causes the objects in the scenario to move. In the Execution Controls area, click the Run button to run the scenario. It will run all actions in the scenario repeatedly until Pause is clicked.


    Run Button

    Watch this video to learn how to code a programming instruction in the act() method and then run the scenario to see the results of the programming instruction.

    Download:

    The activity below requires that you start with the project file that was saved in the previous topic. Download this Greenfoot project file if you did not complete the previous topic's Try it activity.


    Instructions:

    1. Open the Code editor for the Elephant class.
    2. In the act() method, program the elephant to move 8 steps, then turn 2 degrees.
    3. Open the Code editor for the Hippo class. Program the hippo to move 8 steps, then turn 2 degrees.
    4. Compile the scenario.
    5. Run the scenario. Observe how the elephant and hippo move.
    6. Save the scenario.

    Save versions of a scenario

    It is good practice to save a scenario frequently as you work. Each time you close Greenfoot, it saves your current work, but while working in Greenfoot it is advised to save multiple versions of scenarios.

    As your scenarios become more complex, you may wish to have multiple versions of your scenario so that you can revert back to a version. You may also want to save two versions to test out different actions without impacting your original scenario.

    Steps to save a version of a scenario:

    1. From the opened scenario, select Save As… from the Scenario menu.
    2. Save a copy to a folder on your computer or USB drive.

    Watch this video to learn how to save a version of a scenario.

    Download:

    The activity below requires that you start with the project file that was saved in the previous topic. Download this Greenfoot project file if you did not complete the previous topic's Try it activity.


    Instructions:

    1. Reposition the objects in the scenario with your cursor. Add additional objects to the scenario.
    2. From the Scenario menu, select Save As.
    3. Save a copy of the scenario. The scenario will open in a new window.

    Summary

    In this lesson you have learned to:

    Add and position objects

    Lesson objectives

    In this lesson you will learn to:

    Watch this video to see a preview of what you will learn in this lesson.

    Create and save a new scenario

    Now that you have learned how to open, compile, and run a previously-created scenario, you will learn how to create one of your own. When you create a new scenario, Greenfoot will open a window displaying the world, the class display, and the execution controls. This is where you will set up your scene, adding classes to create instances for the background image, non-moving scenery objects, and moving objects.

    Greenfoot has two primary perspectives within which you will build a game: the Edit Code perspective and the Scene Setup perspective. These perspectives will be referred to as the Code editor and the Scene editor from this point on.

    The Code editor


    Code Editor

    The Scene editor

    Scene Editor

    Create a new scenario

    To create a new scenario follow these steps:

    1. Select New from the Scenario menu.
      New Scenario
    2. Navigate to the folder where you wish to save the scenario.
    3. Enter the name of the Scenario in the Folder name text box.
    4. Click Create.
      Scenario Name

    The new scenario will open in Greenfoot. The only classes that appear in the Class Display are World and Actor.

    Scenario New Clean

    Save the scenario

    Periodically save the scenario that you are working on to minimize loss of work due to system crashes or other unforeseen events.

    To save a scenario, click Save from the Scenario menu.

    Scenario Saved

    If you wish to save a copy with a new name, select the Save As… option from the Scenario menu, then enter a new name.

    Save Scenario As.

    Watch this video to learn how to create and save a scenario in Greenfoot.

    Instructions:

    1. From the Scenario menu in Greenfoot, select New.
    2. Navigate to the Greenfoot Course Scenarios folder on your computer.
    3. Name the scenario FrogFly_L2S2.
    4. Click the Create button. The scenario will open in a new window.

    Create a new world subclass and compile the scenario

    What is a class?

    A class contains the specifications that define the appearance and movement of an object. The class provides instructions to Greenfoot for how to create and display instances when they are added to the world.

    Inherited characteristics

    In nature, a bee inherits some characteristics that are common to all bees: two wings, a black and yellow color, and a stinger.

    The bee may also inherit the characteristics of its breed that gives it a specific color, shape and size.

    In Greenfoot, a class, such as the Bee class, defines the characteristics of all bee objects that act in the scenario, such as how they look and ways they can act.

    Bee Class

    Classes in Greenfoot

    When you add a class to your scenario, it appears in the class display (to the right of the world).

    Class Display

    You can have as many Classes within your scenario as you require for your game to meet your concept and design.

    You can add as many instances of the class as you wish to the world and they will all look and act based on the programming statements you add to the class.

    Superclass types

    The term superclass is used to describe the overarching class of a group of classes. A superclass is basically the same as any other class in Java, except that other subclasses created from the superclass inherit all of its characteristics.

    For example, Insect is a superclass. Bees, butterflies, ants, and flies are all subclasses of the Insect superclass. All programming statements defined in the Insect superclass are inherited by the bee, butterfly, ant, and fly subclasses.

    Inheritance

    Two types of superclasses appear in the Greenfoot class display: World and Actor. These two superclasses are always visible in the class display.

    Superclasses

    You can add as many instances of the class as you wish to the world and they will all look and act based on the programming statements you add to the class.

    Subclasses

    A subclass is a child of the parent class and is created from the superclass. Subclasses are a specialization of a class.

    For example, in Greenfoot, the Butterfly class is a subclass of the Actor superclass.

    Butterfly Class

    The butterfly subclass:

    An arrow in the class hierarchy shows the subclass’s relationship to the superclass.

    Create a world subclass

    To create a background for your new scenario you will need to create a world subclass. The world subclass will hold the background image for the scenario.

    Steps to create a new world subclass:

    1. Right-click on the World superclass.
    2. Select New subclass.
      New World Subclass
    3. In the New Class dialog box, name the subclass a descriptive name.
    4. Select an image that the class will display. This is the background for the world. Select the backgrounds category, then select an image.
    5. Click OK.
      Select Image
    6. The world subclass will appear in the class hierarchy.
      New World Subclass Display

    Compile the scenario

    Compilation is required when a subclass or superclass is created or modified. It is also required when you create or modify the source code for a class. To compile a scenario, click the Compile button.

    Compile

    Watch this video to learn how to create a World subclass.

    Download:

    The Try it activity below requires that you start with the project file that was saved in the previous topic. Download and open this Greenfoot scenario file if you did not complete the previous topic's Try it activity.


    Instructions:

    1. Right-click on the World superclass. Select new subclass.
    2. Name the class FrogWorld.
    3. In the Backgrounds image category, select the sand.jpg background image.
    4. Click OK.
    5. Compile the scenario.
    6. Save the scenario.

    Create a new actor subclass and import images into the gallery

    You can create a new actor subclass and assign your own image to the object. Actor subclasses hold the objects that we place in the world. In this example illustrated below there is a Bee subclass and a Butterfly subclass.

    Bee Butterfly

    These subclasses inherit, or possess, all of the behaviors from the Actor superclass. Right-clicking on a an instance object that is placed in the world enables you to see the methods inherited from the Actor superclass.

    Inherited Methods

    In addition to the many images that Greenfoot provides in its Gallery, you can import your own images into the gallery to use with your classes. When a new class is created you can select the option to import a file which lets you adds the image to the gallery.

    For example, you may create an image with instructions for how to play your game. You can save this image to your computer, then import the image into Greenfoot for use in a subclass named Instructions. Adding an instance of Instructions to the world will display the instructions for your game.

    Instructions

    Steps to create a new subclass with an image imported from your computer:

    1. Right-click on the Actor superclass.
    2. Select New subclass.
      New Subclass
    3. In the New class window, click the Import From File. button.
      Import File
    4. Select the image file to import from your computer.
      Instructions
    5. Name the new subclass, then click OK.
      Instructions
    6. The subclass with the new image appears in the class hierarchy. Click the Compile button to compile the scenario.
      Instructions Class

    Add an Actor subclass instance to the scenario

    When a new scenario is created, it displays one instance of the World subclass, as it provides the background template for your scenario. A scenario may display multiple Actor instances - as many Actor instances as you choose.

    Steps to add an Instance of an Actor subclass to a world:

    1. Right click on the class.
    2. Click the new [class name] option.
      New Instructions
    3. Drag the instance into the scenario with your cursor.
      Instructions Instance

    Watch this video to learn how to create a new subclass and import images into the Greenfoot gallery.

    Download:

    The Try it activity below requires that you start with the project file that was saved in the previous topic. Download and open this Greenfoot scenario file if you did not complete the previous topic's Try it activity.


    Instructions:

    1. In your computer's paint program, create an image with instructions for your game that says “Control the Frog with the arrow keys on your keyboard. You must eat 10 flies to win the game. Avoid the ants and rocks.”
    2. Create an Actor subclass and import the image file into the subclass.
    3. Name the new subclass “Instructions”. Click OK.
    4. Create Frog, Fly, Ant, and Rock classes. Note that these classes have images in the Greenfoot gallery that you can use.
    5. Compile the scenario.
    6. Save the scenario.

    Steps to add an instance of a class to the world

    1. Right click on the name of the class.
    2. Select the new [class name()] option.
    3. You should be able to see the instance on your cursor. Drag the instance into the world with your cursor and drop it onto the world.

    Position instances

    Instances added to a world can be positioned when they are created or positioned later as you continue to define the scenario. There are two ways to position instances:

    Drag and drop instances with your cursor

    To drag and drop an instance with your cursor, click the instance with your cursor, and drag it to the position you want in the world.

    Drag and Drop

    To use methods to re-position instances:

    Position Methods

    1. Right-click on an instance in the world. In the Inherited from Actor menu, you will see a list of methods available to position your object.
    2. Select one of the methods (such as move(), turn(), setLocation(), setRotation(), or turnTowards()) to reposition the instance.

    Save the world

    You will notice that each time you open the scenario, you have to re-add all of the Actor instances to the world.

    To save the positioning of the instances placed in the world so that they appear in the same position each time you open the scenario, right-click on the world, and select Save the World.

    Save World

    The next time you open that scenario, the instances will automatically appear in the world. You can always reposition the instances and save the world again.

    Watch this video to learn how to position instances in a scenario.

    Download:

    The Try it activity below requires that you start with the project file that was saved in the previous topic. Download and open this Greenfoot scenario file if you did not complete the previous topic's Try it activity.


    Instructions:

    1. Add 2 rocks, 2 ants, 1 frog, 10 flies, and 1 instructions instance to the scenario.
    2. Position the flies using your cursor. Also, position the flies using the setRotation() method so they are all facing different directions.
    3. Position the frog at x = 50 y = 50.
    4. Position the instructions at the bottom of the world.
    5. Position the ants and rocks anywhere you wish in the world.
    6. Save the world so that when you re-open the scenario, the instances are automatically positioned in the world.
    7. Save the scenario.

    Summary

    In this lesson you have learned how to:

    Explore the Code editor

    Lesson objectives

    In this lesson you will learn how to:

    Watch this video to see a preview of what you will learn in this lesson.

    What is source code?

    Source code defines what all instances of each class are capable of doing. The behavior of each instance is determined by the source code of its class. Think of source code as a program's DNA. DNA gives humans certain characteristics, such as appearance, mobility, and communication. Like DNA, source code is written to tell the class how its instances can act in the world.

    Source Code

    View the source code of a class

    Steps to view the source code of a class:

    Source Code

    1. Right click on a class in the class menu.
    2. Select Open editor.

    Code editor

    The Code editor displays the source code of a class. This is where you add programming instructions that enable instances of the class to act. When you select Open editor, the Code editor for the class will open in a new window.

    You can tell that you are in the Code editor when you see Java syntax.

    Code Editor

    You can tell that you are in the Scene editor when you see the execution controls, world, and class display.

    Scene Editor

    Using methods from other classes

    Each class inherits methods it can use. However, you can call a method that is inherited or defined (created) in a different class. There are many class libraries for programmers to use when writing class source code. You can always refer to the Greenfoot Class Documentation, which is a complete library of all of the methods you can use in Greenfoot. To use methods from another class in the source code of a class, you must ensure that you reference the Library containing the class.

    From the Help menu in the Scene editor, select Greenfoot Class Documentation. This will show you all of the methods that you can use in Greenfoot.

    Class Documentation

    Components of source code

    Source code has several components that describe the class, define the actions that the class's instances will perform when the scenario is run, and describe any programmer-defined methods that you will create for the class to use. Learning what each means will help you understand how to properly write code for your scenarios.

    Class description

    The class description is a set of comments that can be modified to describe the class. The description includes:

    Class Description

    Class definition components

    The class definition includes:

    Class Description

    Defining classes

    The class definition defines: