Submitting a Runnable Greenfoot Scenario

Submitting a Runnable Greenfoot Scenario

When you run your Greenfoot Scenario in the Greenfoot IDE, the world starts up as an empty grid. To add objects (wombats, leaves, etc.) to the grid, you right-click on a class in the class browser, select new <whatever> from the menu, and place the object in the grid. Once you have placed objects in the grid, you can click on Act or Run to run the program and see the changes in the world.

When you submit your work to FreshBrain, you will export your Scenario as a Java Applet. This is code that will run and in a web browser, and allows the FreshBrain site to display your work on the FreshBrain web pages. However, the applet will only display the grid and the Act/Run/Reset buttonsi; the class browser will not be displayed, so you will not be able to add objects to the grid when viewing in the web page. This makes for a pretty boring demonstration of your work.

To work around this limitation, you can make some small changes to your code to populate your world with some objects when it is created. This is pretty simple to do. When the Scenario starts up, is calls the constructor for your world class (in the case of the tutorial, the WombatWorld class). If you look at the source code for the WombatWorld class (right-click on the WombatWorld class and select Open Editor), you'll see three functions:

  • public WombatWorld(), which is the constructor for the class; this is what creates your WombatWorld

  • public void populate(), a function that creates two Wombats and 6 Leafs, and adds them to the Wombat World

  • public void randomLeaves(int howMany), a function that creates some leaves and adds them at random positions in the WombatWorld

Don't worry if you don't understand what all of this means right away, as you progress in Java programming it will all make sense.

The code for the constructor looks like this:


    public WombatWorld() 
{
super(8, 8, 60);
setBackground("cell.jpg");
}


What this does is create an 8x8 grid, with each cell of the grid 60 pixels square, and then sets the background image for each cell using a jpeg image called cell.jpg, which looks like sand. This gives us our big, square, sandy looking Wombat World, with no wombats in it. That's why, if you just publish your world as it is, you'll end up with an applet that shows the grid and nothing else.

We can make a very simple change to add some objects to the world when it is created:


    public WombatWorld() 
{
super(8, 8, 60);
setBackground("cell.jpg");
populate();
}


Here we have just added a call to the populate() function after the world is created. As described earlier, this will add a couple of wombats and a few leaves to the world. After you make this change in the text editor, cllick the Class menu, and select Save; then click the Class menu, and select Close. Back in the Greenfoot IDE, you'll see that the WombatWorld class object has a crosshatch background; this means that the code has been modified, but has not been re-compiled. If you click on Run right now, you will not see the changes. First you need to click on Compile all to compile the WombatWorld class; after that, you can run it and see your world will be pre-populated with objects.

Now, this does create a little bit of a problem, as it's now impossible to create an empty Wombat World; when a world is created, it is always populated. Fortunately, when you're running in the Greenfoot IDE, you can remove objects from the world just as easily as you can add them.

When you first open the Scenario, or anytime you hit the Reset button in the IDE, you'll get your new world, populated with those wombats and leaves. To remove any objects form the world, right-click on the object in the grid (the Wombat, or Leaf, etc.), and select Remove from the menu. The object is gone! In this way, you can remove all of the objects if you want, and can start with your clean, empty Wombat World.

In some activities, you man be creating your own world class.  If you are using a world other than the WombatWorld, you will need to do something similar in your world; you may need to create your own populate() method, and then call it in the constructor.  You can always test your code before submission to see how it will look in the browser by doing the export, and then opening the html file in your export directory with your browser.  The export as Webpage creates a .jar file and a .html file.  Visit the .html file with your browser to test the applet.


iThe Speed control may also show up in the applet; this depends on whether or not you check the Lock scenario checkbox in the Export dialog when you export your scenario as an applet. If you lock the scenario, this removes the speed control from the applet, and the applet will run at the speed that you had selected in the IDE at the time you exported the applet. If you don't lock the scenario, the speed control will show up, and the user will be able to adjust the speed of your scenario. Food for thought: why would you want to remove the speed control from your applet?