Getting Your Feet Wet
Use Greenfoot to create Java classes, objects, and methods.
MICHAEL KÖLLING
In previous articles I’ve written, for example, “Wombat Object
Basics” and “Wombat Classes
Basics,” I covered important
basic concepts of object orientation. The concepts were explored
mostly by investigating existing
source code and by discussing
theory. Programming, however,
is about doing. So in this article,
we’ll jump right in and start to
make our own class. In the process, you will learn about objects,
methods, and parameters.
Greenfoot calls its projects
scenarios. So the first thing we’ll
do is to create a new scenario.
1. Start Greenfoot.
2. Greenfoot usually opens the
last scenario you worked
on. Close this by selecting
the Close function from the
Scenario menu.
3. Select New from the Scenario
menu.
4. In the New Scenario dialog
box, name the new scenario
turtle and click OK.
You should now have a new
scenario window that is mainly
gray, as shown in Figure 1.
We now have a shell for a new
scenario, a place where we can
start to work. We do not have a
world yet to make things happen
in, and we do not have any actors
that can do things.
The two classes you see on the
right side of the window, World
and Actor, are superclasses. They
are not a specific world or a spe-
cific actor, but rather descriptions
of all possible worlds and actors.
We will create a specific world and
actors by creating subclasses of
these. Subclasses represent spe-
cializations of the
superclasses.
JAVA IN ACTION
Prerequisites
To follow along, you will need the
following software installed on
your computer:
■ ■ Java Platform, Standard Edition
(Java SE)
■ ■ Greenfoot version 2. 1.0 or newer
Ensure that you have a new
enough version of Greenfoot. You
can check your version using the
About Greenfoot menu item.
Creating Your Own Project
In my previous articles, I worked
with an existing project (
wombats). This time, let’s create our
own project.
Creating a New
World
1. Right-click the
World class
(the beige box)
and select the
New Subclass
function.
A dialog box asks
you for a name
and an image for
your new class (see
Figure 2).
1. Name your class
Turtle World, and
select an im-
age from the
Backgrounds
category of the
image library.
For the screen-
shots in this ar-
ticle, I selected a
background image
called weave.jpg.
2. Click OK and
then click the
Figure 1
ABOUT US
Figure 2
blog
PHOTOGRAPH BY JOHN BLYTHE
Figure 3