This site requires JavaScript, please enable it in your browser!
Greenfoot back

Greenfoot 3.1.0 Font and Color Change

In Greenfoot 3.0.4 and earlier, the Greenfoot API used the java.awt.Font and java.awt.Color classes. These are part of the Java desktop API, and using them was hampering potential future work to get Greenfoot running in a wider variety of settings. We decided to change the API to supply our own classes to improve future flexibility.

What you should do from now on

The Greenfoot library now has two new classes, greenfoot.Color and greenfoot.Font, to replace use of java.awt.Color and java.awt.Font. In all future scenarios, please use these classes in preference of the awt classes. Normally you do not need to separately import them: Most Greenfoot scenarios use an import statement of the form
import greenfoot.*;
which will include the new classes.

Automatic Conversion

When you load a scenario edited with Greenfoot version 3.0.4 or earlier in the new 3.1.0 or later, it will ask if you want to automatically convert your scenario to use the new classes. The easiest thing to do is to say yes and let Greenfoot do the conversion. This should be sufficient in 90% of cases; read on for details on what you need to do if the automatic conversion isn't enough, or if you need to use the scenario with an older version of Greenfoot again in future.

Adjusting to the change

If you did not use Font or Color in your scenario then nothing will need to be changed (Greenfoot will still ask if you want to automatically convert the scenario, but it won't find anything to convert.) If you were using Font or Color, you probably had an import statement at the top of your class like:
import greenfoot.*;
import java.awt.Color;
To convert, just remove the "import java.awt.Color;" line; the "import greenfoot.*;" will pick up the new greenfoot.Color class automatically. This is what Greenfoot does when it automatically converts. There are three main issues which may still require manual adjustment:
  • One is if you have used a fully qualified class name in your code (e.g. "java.awt.Color.RED") to pass a color to a Greenfoot method. In this case, simply remove the package ("Color.RED") or change to "greenfoot.Color.RED").
  • If you are using java.awt methods directly (e.g. to paint a BufferedImage), then you may still need to use java.awt.Color. In this case, you will want to do the opposite, and change "Color.RED" to "java.awt.Color.RED" to be clear which color class you are referring to.
  • We have replicated many of the constructors and color constants from Font and Color into the new Greenfoot classes, but not all. You may need to upper-case your color constants, or use a different constructor of Font.
You can see the new API online or by going to the Help menu in Greenfoot 3.1.0 and selecting "Greenfoot Class Documentation".

Using Old and New Greenfoot with the Same Scenario

One unfortunate consequence of this change is that a scenario which uses Color or Font cannot be used unchanged with both Greenfoot 3.0.4 (and earlier) and 3.1.0 (and later). To convert between the two, see the steps above (in most cases, adding/deleting the "import java.awt.Color;"/"import java.awt.Font;" line). If you are studying for a course and need to work on a scenario at home and at school (which has 3.0.4 or earlier), you may want to use 3.0.4 at home too for the duration of the course, to prevent problems. You can download 3.0.4 from our old versions page.