Laboratory 6: The Scramble Game Applet

March 7, 8, 2006

Bring your Java, Java, Java text to the laboratory.

Objectives

The objectives of this laboratory are
  1. to design and implement GUI interface for the Scramble Game
In preparation for this lab you should review §4.4.10 of Java, Java, Java.

Problem statement

To build a GUI for the Scramble game, we will download and modify the GreeterGUIPanel and GreeterPanelApplet classes that were described in §4.4.10. We will adapt both definitions so that they are appropriate for the Scramble game. No changes to the Scramble class are necessary.

In this version, the applet will present the user with a a scrambled word, such as "avja". The user will input a guess into a JTextField and click on a JTextButton. The applet will then check whether the guess is correct and report the result in a JTextArea.


Sample Solution

Scramble Applet

Preparation

Step 1. Set Up.

Copy the Scramble.java class from your lab3 folder into your lab6 folder. Download the GreeterGUIPanel.java and GreeterPanelApplet.java classes and rename them to ScrambleGUIPanel.java and ScramblePanelApplet.java.

Step 2. Adapting the Applet and Panel Classes.

Modify both ScramblePanelApplet and ScrambleGUIPanel so they interface with the Scramble class. Here are a list of required changes:

Enhancing the GUI

Let's now enhance the GUI and the game so that our result will be like this sample:

Enhanced Solution

Scramble Applet

Panels With Borders

Note how this enhanced version has titled borders around the various sections of the applet. To add a titled border around a JPanel you can use Java library methods. Here's the link to the Java libray:
Java API Documentation

Look up the JPanel class and find a method that you can use to add a border to a panel. The method will be named something like setBorder(). If you can't find a method in the JPanel class, look in its immediate superclass. Remember that a class inherits the public methods of its superclasses and can use them as its own.

What is the immediate superclass of JPanel? You can find the answer by looking at the very beginning of the JPanel documentation. If you have trouble navigating the API, ask for help.

The type of border used in the demo is a TitledBorder. Look up this class in the API and find the simplest constructor that you can use to create a titled border. Preferably you want a constructor that lets you set its title. Note that the TitledBorder class belongs to the javax.swing.border package. This means you will have to add the following import statement to the beginning of your class definition:

import javax.swing.border.*;

Once you've found all the pieces, you will have to put them together. Here's an example of how you would add a titled border to the inputPanel, the panel that contains the prompt and the text field.

inputPanel.setBorder(new TitledBorder("Inputs"));

Try this out. And see how it looks. It's better to add borders to panels, rather than to text areas, or other components. So create additional panels, one for the control buttons and one for the display text area and add titled borders to them. Then add the buttons and the display to their respective panels. Then add the panels to the ScrambleGUIPanel (the main panel) in the North, Center, and South positions.

NOTE: Local vs. Global Variables. Where should you declare these additional JPanels? If a variable is only going to be used in one method, it is better to declare it locally. That way you don't clutter up your class definition with instance variables.

The Play Again Button

Declare a new instance variable for a playAgainButton and incorporate it into your GUI. This means you have to instantiate the button in your buildGUI() method, add it to the controls panel, and assign it an ActionListener. Test things to see how the button looks.

What action should your program take? When the user requests a new game, you have to create a new Scramble object. There is no need to declare a new variable for this. You can assign it to the Scramble instance variable, which will now just refere to a new object. Of course, you want to give this new object a new set of words. Then you have to issue a new prompt in the display area. Try to sample program to see how this should work. Obviously, this will only work once. We'll need to learn more Java in order to play an unlimited number of scramble games.

Hand In.

Before handing in your programs, be sure to document your source code.

Have your work checked by the laboratory instructor or TA. Print out and hand in a copy of your ScrambleGUIPanel.java.

You're done. Great work!