Greenfoot Tutorial11
Greenfoot Tutorial11
Greenfoot Tutorial
Version 1.0
for Greenfoot Version 1.0
Greenfoot Tutorial
Michael Klling
Contents
Foreword
1. About Greenfoot
2. Scope and audience
3. Copyright, licensing and redistribution
Installation
4.
5.
6.
7.
Installing on Windows
Installing on Mac OS X
Installing on other systems
The wombats scenario
Getting Started
Greenfoot Tutorial
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
Programming
Foreword
Greenfoot Tutorial
1. About Greenfoot
This tutorial is an introduction to the Greenfoot Object World.
Greenfoot is a software tool designed to let beginners get experience with
object-oriented programming. It supports development of graphical applications
in the Java Programming Language.
Greenfoot was designed and implemented at the University of Kent, England,
and Deakin University, Melbourne, Australia.
More information about Greenfoot is available at www.greenfoot.org.
Greenfoot Tutorial
The Greenfoot system and this tutorial are available 'as is', free of charge to
anyone for use and non-commercial re-distribution. Disassembly of the system
is prohibited.
No part of the Greenfoot system or its documentation may be sold for profit
or included in a package that is sold for profit without written authorisation of
the authors.
The copyright for Greenfoot is held by Poul Henriksen and Michael Klling.
Installation
Three different versions are available: one for Windows, one for MacOS X, and
one for all other systems.
Prerequisites
You must have Java 5 (a.k.a. JDK 1.5) or later installed on your system to
use Greenfoot. Generally, updating to the latest stable (non-beta) Java release
is recommended. If you do not have JDK installed you can download it from
Suns web site at http://java.sun.com/j2se/ . Do not download a version
named JRE (Java Runtime Environment) the JRE is not sufficient. You need
a version that is named JDK.
On MacOS X, a recent J2SE version is pre-installed - you do not need to install
it yourself.
4. Installing on Windows
Greenfoot Tutorial
5. Installing on Mac OS X
For Mac OS X, download the Mac OS version of the software. The download file is
a zip file. Your browser may unpack that file automatically for you. If not,
double-click the zip file to unpack it.
The resulting folder contains the Greenfoot application and sample scenarios.
Both can be placed anywhere you like. One common arrangement is to place
the Greenfoot application into the Applications folder and the scenarios into
the Documents folder.
Greenfoot Tutorial
This tutorial uses two scenarios, called wombats and wombats2 . They
are distributed together with this tutorial. Each scenario will appear as one folder
in your file system.
Greenfoot Tutorial
Getting Started
8. Open a Greenfoot project
You may have opened this tutorial from within the Greenfoot application.
That's good. If not, then you need to start Greenfoot now.
The installation process has installed an application named greenfoot. Start it.
(On Linux and Unix systems, greenfoot is an executable shell script.)
Once Greenfoot has started up, and if the Wombats scenario is not
automatically opened, choose Open... from the Project menu and select
the wombats scenario from the Greenfoot sample scenarios.
You should see something like Figure 1 on screen (except that you will not see
any leaves or wombats in the world yet).
The large grid area that covers the majority of the window is called the world
. Since we have a scenario here that has to do with wombats (see Footnote 1),
we see a wombat world. Towards the right side of the window is the class
display . Here you can see all Java classes that are involved in the project.
The classes World and Actor will always be there they come with the
Greenfoot Tutorial
Greenfoot system. The other classes belong to the wombat scenario, and will
be different if you use different scenarios.
Below the world are the Execution Controls (the area with the Act and Run
buttons and the slider).
Greenfoot Tutorial
Greenfoot Tutorial
Make sure you have a wombat in the world, and the scenario is not running.
Then right-click on the wombat, and you see that objects in the world also have
a pop-up menu (Figure 2).
You can select any of the methods shown here to ask the wombat to do
something. Try, for example, turnLeft(). Selecting this from the menu tells
the wombat to turn to its left. Try move().
Greenfoot Tutorial
Some methods give you an answer. getLeavesEaten(), for example, will tell you
how many leaves this wombat has eaten so far. Try it.
You will also notice a method called act(). This method is called every time you
click the Act button. If you want just one object to act instead of all the objects
in the world, you can do this by invoking the objects act method directly.
Greenfoot Tutorial
Programming
The remainder of this tutorial assumes that readers are familiar with some basics
of Java programming. It is not intended as a first activity for beginning students.
Greenfoot Tutorial
You can program your own objects wombats, or anything else you like by
writing some Java code for the class of the object. Thats what well do now.
Double-click the Wombat class in the class display. A text editor opens and you
will see the Java source code for the Wombat class.
The first thing we want to do is change the wombats behaviour so that, when
it cannot move forward, it turns to a random direction instead of always turning
left. To achieve this, we first add a turnRandom() method. Add the
following method to the Wombat class:
/**
* Turn in a random direction.
*/
public void turnRandom()
{
// get a random number between 0 and 3...
int turns = Greenfoot.getRandomNumber(4);
Greenfoot Tutorial
Greenfoot Tutorial
a parameter of type GreenfootImage, the other takes the name of a file (and
then reads that image file into a GreenfootImage and sets it).
We will use the variation that reads an image from a file. The task we want
to achieve is that wombats dont appear upside-down when walking to the left.
The wombats project already contains a file named wombat-left.gif in its
images sub-folder. This is an image file in GIF format that shows a mirrored
image of the wombat we have seen so far: a wombat looking left.
We can change the image of the wombat to the left-facing version by writing:
setImage("wombat-left.gif");
We will use this method when the wombat changes direction. To do this we need
to edit the Wombats setDirection(int Direction) method. Find it in the
Wombat source code.
We will add some lines here to set the correct image and correct rotation when
we set a direction. We will use the wombat-left image when heading west
and north, and the original wombat image when going east or south. Note that
the wombat-left image faces west by default it does not need to be rotated
when we head west.
Here is the new version of setDirection:
/**
* Sets the direction we're facing.
*/
public void setDirection(int direction)
{
this.direction = direction;
switch(direction) {
case SOUTH :
setImage("wombat.gif");
setRotation(90);
break;
case EAST :
setImage("wombat.gif");
setRotation(0);
Greenfoot Tutorial
break;
case NORTH :
setImage("wombat-left.gif");
setRotation(90);
break;
case WEST :
setImage("wombat-left.gif");
setRotation(0);
break;
default :
break;
This works well enough as it is. If this was done more seriously, we should load
the image objects from file only once (in the constructor) and store them in fields
of type GreenfootImage . Then we can use the two image objects to change
the image repeatedly.
Maybe we should now add separate images for up and down movement we
leave this to the enthusiastic reader.
Greenfoot Tutorial
From that page, you can get to an online version of the Greenfoot class
descriptions (called the Greenfoot API), and you can download the description in
a single, printable document.
Greenfoot Tutorial
image and place it into the projects images directory before creating the class.
It would then be presented in this dialogue. In this case, we have already placed
an image file named rock.gif into the project for you to make things a little quicker.
Select the image, click Ok, and a new class named Rock is created.
Now open the editor for that class. You will notice that a source skeleton
is automatically generated. We dont actually have to write any code right
now. (Rocks dont have any particular behaviour.)
Close the editor, compile and test (create an object). Voil there are your rocks!
Now populate the scenario a bit and test the wombats. (You could use the
worlds populate and randomLeaves methods, and then add some rocks by
hand.) You will notice that you have rocks, but the wombats still run through
them. Oh well more work to do.
Greenfoot Tutorial
This code snippet gets a list of all rocks from the cell in front of us. If this list
is empty, we can move forward, otherwise we cannot.
The project wombats2, which was included in the download with this
tutorial, includes all the changes discussed here.
Greenfoot Tutorial
Join the Greenfoot mailing list and get talking to other Greenfoot programmers.
1 A wombat is a native Australian animal. If you dont know about them, look them up
at Wikipedia: http://en.wikipedia.org/wiki/Wombat .