Copyright RIT 2009
$$
You are to work on this lab completely on your own.
In last week's lab you learned how to use Swing to build simple graphical user interfaces. Although the GUIs you built were neat, they did nothing. In this week's lab you will implement event-handling code to produce a functioning version of the Lotto GUI that you laid out components for in last week's lab. You will be making use of ActionEvents from JButtons, and creating listeners that handle these events appropriately. You also will be making use of the Java Collections framework to store and compare selected and winning lotto numbers.
Review your class notes on Java Swing.
Read through the Java tutorial trail on How to Use Actions (http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html) .
In last week's lab you wrote a program to create and display a GUI similar to the one shown below:

Lotto start
An important step in design for programs with GUIs is to decide on the underlying model for the data that the interface will be used to view and manipulate. This model will be implemented using some variety of variables and data structures along with associated operations (e.g. mutators and accessors). We have included a couple of methods in the LottoGUI Javadoc, that you must implement as part of your design, but feel free to add additional methods if you like.
Our model here is simple. We need data structures to represent which numbers have been selected and the winning numbers, which will allow these to be easily compared to determine which numbers they have in common. You will need to think about which data structures in the Java Collections Framework are well-suited for this. In our model we will also need to represent the players' current balance, and the current state (mode) of the game.
Once you have chosen a representation, it is time to add
some functionality to the number buttons.
When a
JButton
is pressed, an
ActionEvent
is generated. A GUI can capture button clicks by creating and
installing an
ActionListener.
Normally one
ActionListener
will handle the
ActionEvents
generated for a group of buttons. The
ActionListener
can determine which button was pressed by invoking the
ActionEvent's getActionCommand() or
getSource() method.
To implement the
ActionListener
interface, we need a single
method, actionPerformed(). You may choose to
have your LottoGUI implement this interface itself, however
eventually we will have several different types of buttons
to react to. Therefore, you may prefer to
implement ActionListeners for different types of buttons
with inner classes or anonymous classes.
In the LottoGUI, you should handle events from the number selection pad so that:
Here is what the pad looks like after selecting three buttons:

Lotto pick
Make sure to implement and use the static initializeButton() method. Often in GUI programming there are a lot of initialization operations that are repeated. Methods like initializeButton() make code easier to read and maintain, by representing common sequences of initializations as one operation.
Once you are convinced that your program is working correctly, and that the GUI is handling the window closing event correctly, submit your work using the following command:
|
In this activity, we will implement some functionality for the "Reset", "Quit", and "Draw" buttons. The "Quit" button should close the main window using the dispose() method of JFrame. Closing the window should also make the program stop. You can make this action occur by changing the default close operation of your GUI window.
"Reset" will deselect all numbers, in the GUI turning their associated button's background color back to the default. The "Draw" button should become active when six numbers have been picked (the letters in "Draw" turn green). If the user deselects a number after six picks, "Draw" should become inactive again.

Lotto ready
When the "Draw" button is active, if the user clicks on the button, six winning numbers are chosen at random (see the Math.random() method), and the player loses $1 (the cost of one play). The selection of the numbers is done using the pickWinner() method. Clicking 'normally' on draw when it is active should invoke pickWinner with the argument 'false,' to choose the winner in this way.
However, the user may also cheat by clicking on the active "Draw" button while holding down the shift key. This results in the function 'pickWinner()' being called with the argument 'true.' This causes the winning numbers to match the currently selected numbers. The ActionEvent class has a routine called 'getModifiers()' that returns a value indicating what modifier keys are held down. The constant ActionEvent.SHIFT_MASK is used to indicate that shift key was held down during the event. If you perform bitwise and (&) with the current modifer value and the shift mask, you can tell whether the shift key is pressed.
For this activity, it is sufficient to simply print the selected and winning numbers when "picked" to the standard output.
Once you are convinced that your program is working correctly, submit it using the following command:
|
Now we will fully implement the "Draw" operation so that we can actually play the game. In the last activity we implemented "Draw" so that we can select winners numbers (whether fairly or not-so-fairly). In addition to losing $1 each time we press "Draw", we will now implement the payoffs. Your program will now determine how many selected numbers match winning numbers, and pay out accordingly. The two possible outcomes are:
The user is shown which numbers were picked in the following way: Winning numbers that match selected numbers are shown in green, while winning numbers that do not match selected numbers are shown in red.

Lotto matches
It is important that the player must not be able to change the state of the buttons by clicking on them at this point; only hitting the "Reset" button should return all the number pad buttons back to their default colors, set the mode status message back to "Picking numbers...", make "Draw" inactive, and allow the user to play again.
For this program, we will allow negative balances for the player; they can keep playing into negative numbers if they choose.
Once you are convinced that the program is working correctly, submit it using the following command:
|
Grade Breakdown: