1 //Applet to generate lottery entries
2 import javax.swing.*;3 import java.awt.*;4 import java.awt.event.*;5 import java.util.Random; //For random number generator
6
7 @SuppressWarnings("serial")8 public class Lottery extendsJApplet {9 //Generate NUMBER_COUNT random selections from the VALUES array
10 private static int[] getNumbers() {11 int[] numbers = new int[NUMBER_COUNT]; //Store for the numbers to be returned
12 int candidate = 0; //Stores a candidate selection
13 for(int i = 0; i < NUMBER_COUNT; ++i) { //Loop to find the selections
14
15 search:16 //Loop to find a new selection different from any found so far
17 while(true) {18 candidate =VALUES[choice.nextInt(VALUES.length)];19 for(int j = 0 ; j < i ; ++j) { //Check against existing selections
20 if(candidate==numbers[j]) { //If it is the same
21 continue search; //get another random selection
22 }23 }24 numbers[i] = candidate; //Store the selection in numbers array
25 break; //and go to find the next
26 }27 }28 return numbers; //Return the selections
29 }30
31 //Initialize the applet
32 @Override33 public voidinit() {34 SwingUtilities.invokeLater( //Create interface components
35 new Runnable() { //on the event dispatching thread
36 public voidrun() {37 createGUI();38 }39 });40 }41
42 //Create User Interface for applet
43 public voidcreateGUI() {44 //Set up the selection buttons
45 Container content =getContentPane();46 content.setLayout(new GridLayout(0,1)); //Set the layout for the applet47
48 //Set up the panel to hold the lucky number buttons
49 JPanel buttonPane = new JPanel(); //Add the pane containing numbers50
51 //Let's have a fancy panel border
52 buttonPane.setBorder(BorderFactory.createTitledBorder(53 BorderFactory.createEtchedBorder(Color.cyan,54 Color.blue),55 "Every One a Winner!"));56
57 int[] choices = getNumbers(); //Get initial set of numbers
58 for(int i = 0 ; i < NUMBER_COUNT ; ++i) {59 luckyNumbers[i] = newSelection(choices[i]);60 luckyNumbers[i].addActionListener(luckyNumbers[i]); //Button is it's own listener
61 buttonPane.add(luckyNumbers[i]);62 }63 content.add(buttonPane);64
65 //Add the pane containing control buttons
66 JPanel controlPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 10));67
68 //Add the two control buttons
69 JButton button; //A button variable
70 Dimension buttonSize = new Dimension(100,20); //Button size
71
72 controlPane.add(button = new JButton("Lucky Numbers!"));73 button.setBorder(BorderFactory.createRaisedBevelBorder());74 button.addActionListener(newHandleControlButton(PICK_LUCKY_NUMBERS));75 button.setPreferredSize(buttonSize);76
77 controlPane.add(button = new JButton("Color"));78 button.setBorder(BorderFactory.createRaisedBevelBorder());79 button.addActionListener(newHandleControlButton(COLOR));80 button.setPreferredSize(buttonSize);81
82 content.add(controlPane);83 }84
85 //Class defining custom buttons showing lottery selection
86 private class Selection extends JButton implementsActionListener {87 //Constructor
88 public Selection(intvalue) {89 super(Integer.toString(value)); //Call base constructor and set the label
90 this.value = value; //Save the value
91 setBackground(startColor);92 setBorder(BorderFactory.createRaisedBevelBorder()); //Add button border
93 setPreferredSize(new Dimension(80,20));94 }95
96 //Handle selection button event
97 public voidactionPerformed(ActionEvent e) {98 //Change this selection to a new selection
99 int candidate = 0;100 while(true) { //Loop to find a different selection
101 candidate =VALUES[choice.nextInt(VALUES.length)];102 if(!isCurrentSelection(candidate)) { //If it is different
103 break; //end the loop
104 }105 }106 setValue(candidate); //We have one so set the button value
107 }108 //Set the value for the selection
109 public void setValue(intvalue) {110 setText(Integer.toString(value)); //Set value as the button label
111 this.value = value; //Save the value
112 }113
114 //Check the value for the selection
115 boolean hasValue(intpossible) {116 return value==possible; //Return true if equals current value
117 }118
119 //Check the current choices
120 boolean isCurrentSelection(intpossible) {121 for(int i = 0 ; i < NUMBER_COUNT ; ++i) { //For each button
122 if(luckyNumbers[i].hasValue(possible)) { //check against possible
123 return true; //Return true for any =
124 }125 }126 return false; //Otherwise return false
127 }128
129 private int value; //Value for the selection button
130 }131
132 //Class defining a handler for a control button
133 private class HandleControlButton implementsActionListener {134 //Constructor
135 public HandleControlButton(intbuttonID) {136 this.buttonID = buttonID; //Store the button ID
137 }138
139 //Handle button click
140 public voidactionPerformed(ActionEvent e) {141 switch(buttonID) {142 casePICK_LUCKY_NUMBERS:143 int[] numbers = getNumbers(); //Get maxCount random numbers
144 for(int i = 0; i < NUMBER_COUNT; ++i) {145 luckyNumbers[i].setValue(numbers[i]); //Set the button VALUES
146 }147 break;148 caseCOLOR:149 Color color = newColor(150 flipColor.getRGB()^luckyNumbers[0].getBackground().getRGB());151 for(int i = 0; i < NUMBER_COUNT; ++i)152 luckyNumbers[i].setBackground(color); //Set the button colors
153 break;154 }155 }156
157 private intbuttonID;158 }159
160 final static int NUMBER_COUNT = 6; //Number of lucky numbers
161 final static int MIN_VALUE = 1; //Minimum in range
162 final static int MAX_VALUE = 49; //Maximum in range
163 final static int[] VALUES = new int[MAX_VALUE-MIN_VALUE+1]; //Array of possible VALUES
164 static { //Initialize array
165 for(int i = 0 ; i < VALUES.length ; ++i)166 VALUES[i] = i +MIN_VALUE;167 }168
169 //An array of custom buttons for the selected numbers
170 private Selection[] luckyNumbers = newSelection[NUMBER_COUNT];171 final public static int PICK_LUCKY_NUMBERS = 1; //Select button ID
172 final public static int COLOR = 2; //Color button ID173
174 //swap colors
175 Color flipColor = new Color(Color.YELLOW.getRGB()^Color.RED.getRGB());176
177 Color startColor = Color.YELLOW; //start color
178
179 private static Random choice = new Random(); //Random number generator
180 }