Java Reference
In-Depth Information
// Rest of the inner class definition...
}
final static int NUMBER_COUNT = 6;
// Number of
lucky numbers
final static int MIN_VALUE = 1;
// Minimum
in range
final static int MAX_VALUE = 49;
// Maximum
in range
// Array of possible VALUES
final static int[] VALUES = new int[MAX_VALUE-MIN_VALUE+1];
static {
// Initialize
array
for(int i = 0 ; i<VALUES.length ; ++i)
VALUES[i] = i + MIN_VALUE;
}
// An array of custom buttons for the selected numbers
private Selection[] luckyNumbers = new Selection[NUMBER_COUNT];
private static Random choice = new Random();
// Random
number generator
}
Directory "Lottery 1"
How It Works
The applet class is called Lottery , and it contains two inner classes: Selection and HandleCon-
trolButton . The Selection class provides a custom button that shows a number as its label, the number
being passed to the constructor as an argument. You can make an object of the Selection class listen
for its own action events. As I said at the outset, an event for a selection button changes the label of the
button to a different value, so of course, you need to make sure this doesn't duplicate any of the values
for the other buttons.
The two control buttons use separate listeners to handle their action events, and the response to an event
is quite different for each of them. One control button creates a new set of lucky numbers and the other
control button just changes the color of the buttons.
The NUMBER_COUNT member of the Lottery class sets the number of values that is created. The
MIN_VALUE and MAX_VALUE members specify the range of possible values that lottery numbers can have.
The possible values for selections are stored in the VALUES array, and this is set up in the static initial-
ization block. The Lottery class has an array of Selection objects as a data member — you can have
arrays of components just like arrays of any other kind of object. Because the Selection buttons are
all the same, it's very convenient to create them as an array, and having an array of components enables
you to set them up in a loop. You also have a Random object as a class member that you use to generate
random integers. You can now set about filling in the sections of the program that you have roughed out.
Filling in the Details
The generation of maxCount random values from the elements in the values array is quite independent
of everything else here, so you can define a static method in the Lottery class to do this:
Search WWH ::




Custom Search