Previous Section  < Day Day Up >  Next Section

Workshop: Enhancing the LottoMadness Application

The LottoMadness application consists of two classes, the LottoMadness class that builds a graphical user interface and the LottoEvent class that receives user events—button clicks and check box selections.

The LottoMadness and LottoEvent classes were developed during Hour 17, "Creating Interactive Web Programs."

Copy these class files and their .java source code files to the same folder as the classes created so far this hour.

The LottoMadness class requires no modifications to work with XML-RPC.

Around 30 lines must be added to LottoEvent to cause it to submit XML-RPC requests.

In Listing 25.5, the shaded lines are new material. Add them, then resave the file in your editor.

Listing 25.5. The Full Text of LottoEvent.java

 1: import javax.swing.*;

 2: import java.awt.event.*;

 3:

 4: public class LottoEvent implements ItemListener, ActionListener,

 5:     Runnable {

 6:

 7:     LottoMadness gui;

 8:     Thread playing;

 9:     LottoClient client;

 10:

 11:     public LottoEvent(LottoMadness in) {

 12:         gui = in;

 13:         client = new LottoClient();

 14:     }

 15:

 16:     public void actionPerformed(ActionEvent event) {

 17:         String command = event.getActionCommand();

 18:         if (command == "Play") {

 19:             startPlaying();

 20:         }

 21:         if (command == "Stop") {

 22:             stopPlaying();

 23:         }

 24:         if (command == "Reset") {

 25:             clearAllFields();

 26:         }

 27:     }

 28:

 29:     void startPlaying() {

 30:         playing = new Thread(this);

 31:         playing.start();

 32:         gui.play.setEnabled(false);

 33:         gui.stop.setEnabled(true);

 34:         gui.reset.setEnabled(false);

 35:         gui.quickpick.setEnabled(false);

 36:         gui.personal.setEnabled(false);

 37:     }

 38:

 39:     void stopPlaying() {

 40:         gui.stop.setEnabled(false);

 41:         gui.play.setEnabled(true);

 42:         gui.reset.setEnabled(true);

 43:         gui.quickpick.setEnabled(true);

 44:         gui.personal.setEnabled(true);

 45:         playing = null;

 46:         sendResults();

 47:     }

 48:

 49:     void clearAllFields() {

 50:         for (int i = 0; i < 6; i++) {

 51:             gui.numbers[i].setText(null);

 52:             gui.winners[i].setText(null);

 53:         }

 54:         gui.got3.setText("0");

 55:         gui.got4.setText("0");

 56:         gui.got5.setText("0");

 57:         gui.got6.setText("0");

 58:         gui.drawings.setText("0");

 59:         gui.years.setText(null);

 60:     }

 61:

 62:     public void itemStateChanged(ItemEvent event) {

 63:         Object item = event.getItem();

 64:         if (item == gui.quickpick) {

 65:             for (int i = 0; i < 6; i++) {

 66:                 int pick;

 67:                 do {

 68:                     pick = (int) Math.floor(Math.random() * 50 + 1);

 69:                 } while (numberGone(pick, gui.numbers, i));

 70:                 gui.numbers[i].setText("" + pick);

 71:             }

 72:         } else {

 73:             for (int i = 0; i < 6; i++) {

 74:                 gui.numbers[i].setText(null);

 75:             }

 76:         }

 77:     }

 78:

 79:     void addOneToField(JTextField field) {

 80:         int num = Integer.parseInt("0" + field.getText());

 81:         num++;

 82:         field.setText("" + num);

 83:     }

 84:

 85:     boolean numberGone(int num, JTextField[] pastNums, int count) {

 86:         for (int i = 0; i < count; i++) {

 87:             if (Integer.parseInt(pastNums[i].getText()) == num) {

 88:                 return true;

 89:             }

 90:         }

 91:         return false;

 92:     }

 93:

 94:     boolean matchedOne(JTextField win, JTextField[] allPicks) {

 95:         for (int i = 0; i < 6; i++) {

 96:             String winText = win.getText();

 97:             if ( winText.equals( allPicks[i].getText() ) ) {

 98:                 return true;

 99:             }

100:         }

101:         return false;

102:     }

103:

104:     public void run() {

105:         Thread thisThread = Thread.currentThread();

106:         while (playing == thisThread) {

107:             addOneToField(gui.drawings);

108:             int draw = Integer.parseInt(gui.drawings.getText());

109:             float numYears = (float)draw / 104;

110:             gui.years.setText("" + numYears);

111:

112:             int matches = 0;

113:             for (int i = 0; i < 6; i++) {

114:                 int ball;

115:                 do {

116:                     ball = (int)Math.floor(Math.random() * 50 + 1);

117:                 } while (numberGone(ball, gui.winners, i));

118:                 gui.winners[i].setText("" + ball);

119:                 if (matchedOne(gui.winners[i], gui.numbers)) {

120:                     matches++;

121:                 }

122:             }

123:             switch (matches) {

124:                 case 3:

125:                     addOneToField(gui.got3);

126:                     break;

127:                 case 4:

128:                     addOneToField(gui.got4);

129:                     break;

130:                 case 5:

131:                     addOneToField(gui.got5);

132:                     break;

133:                 case 6:

134:                     addOneToField(gui.got6);

135:                     gui.stop.setEnabled(false);

136:                     gui.play.setEnabled(true);

137:                     playing = null;

138:             }

139:             try {

140:                 Thread.sleep(100);

141:             } catch (InterruptedException e) {

142:                 // do nothing

143:             }

144:         }

145:     }

146:

147:     private void sendResults() {

148:         try {

149:             int drawings = Integer.parseInt(gui.drawings.getText());

150:             int got3 = Integer.parseInt(gui.got3.getText());

151:             int got4 = Integer.parseInt(gui.got4.getText());

152:             int got5 = Integer.parseInt(gui.got5.getText());

153:             int got6 = Integer.parseInt(gui.got6.getText());

154:             client.send(drawings, got3, got4, got5, got6);

155:         } catch (NumberFormatException nfe) {

156:             // do nothing

157:         } catch (Exception exception) {

158:             System.out.println("Error: " + exception.getMessage());

159:             exception.printStackTrace();

160:         }

161:     }

162: }


When you compile LottoEvent.java, the compiler also prepares any other classes belonging to the project as needed. Run the application by executing LottoMadness with the interpreter:


java LottoMadness


The LottoMadness application opens as it did in Hour 15.

A new instance variable of the LottoClient class is declared in Line 9 and created in the constructor in Line 13:


LottoClient client;

client = new LottoClient();


Every time you stop an ongoing series of the drawings, the application calls a new sendResults() method in the stopPlaying() method in Line 46:


sendResults();


This method, defined in Lines 147–161, retrieves each drawing value from a field of the user interface:


int drawings = Integer.parseInt(gui.drawings.getText());


The gui object is the LottoMadness object holding the user interface, which has a text field called drawings. Calling its getText() method retrieves the contents of the field as a string.

The six fields become arguments to the LottoClient object's send() method:


client.send(drawings, got3, got4, got5, got6);


    Previous Section  < Day Day Up >  Next Section