In this section, we will explain how to write the following user interface elements: buttons, textboxes, menus, and heads-up displays (HUDs). We are going to work through a Greenfoot scenario (shown in Figure 1) that only contains user interface elements, so we can discuss each element independently.
Some of the code we write will be general and able to be applied to many different scenarios. In other cases, we will write user interface code that will only need minor modification to be used across scenarios. In the next section, we will add these elements to the MazeWorld
scenario that we wrote in the previous chapter, to make it a more polished and playable game.
To work through this scenario, start with a new Greenfoot scenario named UIMainWorld
, create a subclass of World
named UIMainWorld
, and then associate a plain background to it. The background I chose was bluerock.jpg
. Here is the code for UIMainWorld
:
import greenfoot.*; import java.awt.Color...