The aim of this Lab is to help you get comfortable with using NetBeans via the BlueJ Plugin, and to begin to explore some of the features of NetBeans which make it such a productive environment for software development.
We will start by simply examining and running a BlueJ project in NetBeans, then make some simple changes to it which take advantage of some of the NetBeans features, and then move on to a add some GUI elements to what starts off as a text-based system.
This exploration will use the world-of-zuul adventure game described in Chapter 7 of Barnes and Köllings' Objects First with Java book. We will start with a variant of the zuul-with-enums-v2 project called zuul-for-netbeans. First you should download a copy of zuul-for-netbeans.zip,
unpack it and familiarise yourself with it by opening it in BlueJ.
You can run the game by either creating an object of the class
Game and calling its
play() method, or by calling the
static main() method on the class Game.
To assist you in Zuul, here's a map of the university:
Open the same project in NetBeans and examine
it there as well If you have the BlueJ Plugin installed, you will see the view below in the NetBeans project view pane. Note that this does not cause the project to be copied; there is really only one project, and
changes made to it through BlueJ will be reflected in NetBeans and vice
versa. In NetBeans, you cannot directly create objects and invoke
methods, but NetBeans does know more about the special public static
void main() method, and marks classes containing that method with a
In NetBeans you can run the game either by
right-clicking on a class marked with a green triangle and selecting "Run File", or by clicking on the Run Main Project icon in the NetBeans
Notice that the game's implementation does not have a class to
represent the player. At the moment, this is fine as the only thing
that is associated with a player is their current position (recorded in
Game class by the
currentRoom field). As
preparation for adding more to what a player can do in the game
(carrying things, etc.) we need to introduce a class to represent a
player and refactor the current game to use the new class. It is
perfectly possible to do this (by hand) in BlueJ, but the NetBeans refactoring
function gives you a lot of help to automatically
make these small changes to the structure of your code in an automatic (and
correct) fashion. For more details, see the NetBeans online help for
refactoring. For the full story on refactoring, see Martin Fowler's Refactoring Home Page
To add a player to the zuul game, we must:
Create a new class called
Player in NetBeans by
displaying the File > New File wizard. Then select the BlueJ
category and the Standard Class file type. Note that the class types in this list are the same as the ones for which BlueJ has templates.
Click Next to move on, and type
Player as the File Name (leaving the other fields as
they are). An icon for the new
Player class will then appear in your
BlueJ Project View.
Player class definition to contain just a
Room field called
with accessors and mutators. If you just type the declaration of the
field into your
Player class, you can use the NetBeans refactoring
support to create the accessors and mutators for you. Right click in
the editor pane for the
Player class, and select Refactor >
Add a field to hold a player object into the Game class, and
construct the object at an appropriate point.
Would it be sensible to pass a room object into the
Player constructor to ensure
proper initialisation? If so, you can use Refactor > Change Method
Parameters to add an argument to the Player constructor definition and all the calls to it, wherever they are in the project. In this case, that is not so hard as there will only be the one you have just inserted.
Once complete, update the player constructor to initialize the currentRoom field:
Now we have a player object which is capable of storing its own current room, but the
Game class is not using it (it still has its own
currentRoom field). The
Game class only needs to know the starting room in order to feed it to the
Player class. Make this change first by changing the name of the
currentRoom field to
startingRoom. Next, change the parameter passed to the
Player constructor from
These all all the changes you need to make to initialize the
Player object with the
startingRoom. Make sure you understand them.
You may have noticed that when you changed the name of the currentRoom field to startingRoom, NetBeans marked a bunch of lines with wavy red underlines and a red cross in the error margin.
NetBeans in identifying syntax errors in your code before you compile it. We can use this feature to easily find and replace each marked line in the
Game class with calls to the
currentRoom accessor or mutator of the
player object. Note, you can also use the Error Stripe in the right-margin of editor pane to quickly navigate between these errors.
The aim of this section is to briefly explore the NetBeans GUI builder by adding a simple Graphical User Interface to the zuul game. This will consist of an area in which the output text is displayed, and a number of buttons to control the game.
Before we start building the GUI, we need to make a public the methods in the
Game class which implement the game's behaviour (
quit()). We will need to call these from the components of the graphical user interface we will create.
Now create your GUI using the NetBeans GUI Builder. You will need to enable this module by opening the Tools > Module Manager dialog box and ticking the Java > Form Editor box. There is much information on how to use the GUI Builder to create user interfaces, for more information please see the NetBeans Java GUI Application Learning Trail.
You should create an GUI (call it
MainFrame) based on a
JFrame (Java GUI Forms > JFrame Form), which contains a
JTextArea to hold the game's output text, and six
JButtons for the commands: Go North, Go South, Go East, Go West, Help and Quit. This will produce a new class called
MainFrame in your BlueJ project which contains the GUI source code and a
main() method which can be used to run the GUI version of the zuul game. Feel free to design the form as you like. Below is one example of how the form could look.
As a GUI class,
MainFrame can be viewed in two ways in
the editor: one called the Design View and the other called the Source
View. The Design View allows you to add and position GUI components
using the mouse in the editor pane, and change their names and other
properties using the Inspector pane. The Code View allows you to add
ordinary Java code to the generated class so that you can connect your
application to the generated GUI. In the Design View there is a button
to switch to the Source View, but to switch back you first need to enable the editor toolbar using the View > Show Editor Toolbar menu. Now the editor view will also include the button to switch back to the Design view.
Finally, we will need to connect the GUI to the
MainFrameclass and initialise it in the
MainFrameclass' constructor. Also call the game's
Gameclass and add a helper method called go for moving our player:
MainFrameclass and let's rename our buttons so the source code will be easier to read. Right-click the Go North button and select Change Variable Name. Name the button
jButtonNorth. Repeat this process for the West, East, South, Help and Quit buttons.
game.printHelp()and your Quit button can call
You should now have a version of the zuul game which responds to
button presses, and delivers all of its text to the GUI text area. Right-click the MainFrame icon in the BlueJ View tab and select Run File.
If you've run into any problems, you can compare what you've done with the solution project, zuul-for-netbeans-solution.zip.