// A program from Chapter 19 of Sams Teach Yourself Java in 24 Hours // by Rogers Cadenhead, http://www.java24hours.com/ package com.java24hours; import javax.swing.*; import java.awt.event.*; public class LottoEvent15 implements ItemListener, ActionListener, Runnable { LottoMadness15 gui; Thread playing; public LottoEvent15(LottoMadness15 in) { gui = in; } @Override public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if ("Play".equals(command)) { startPlaying(); } if ("Stop".equals(command)) { stopPlaying(); } if ("Reset".equals(command)) { clearAllFields(); } } void startPlaying() { playing = new Thread(this); playing.start(); gui.play.setEnabled(false); gui.stop.setEnabled(true); gui.reset.setEnabled(false); gui.quickpick.setEnabled(false); gui.personal.setEnabled(false); } void stopPlaying() { gui.stop.setEnabled(false); gui.play.setEnabled(true); gui.reset.setEnabled(true); gui.quickpick.setEnabled(true); gui.personal.setEnabled(true); playing = null; } void clearAllFields() { for (int i = 0; i < 6; i++) { gui.numbers[i].setText(null); gui.winners[i].setText(null); } gui.got3.setText("0"); gui.got4.setText("0"); gui.got5.setText("0"); gui.got6.setText("0"); gui.drawings.setText("0"); gui.years.setText(null); } @Override public void itemStateChanged(ItemEvent event) { Object item = event.getItem(); if (item == gui.quickpick) { for (int i = 0; i < 6; i++) { int pick; do { pick = (int) Math.floor(Math.random() * 50 + 1); } while (numberGone(pick, gui.numbers, i)); gui.numbers[i].setText("" + pick); } } else { for (int i = 0; i < 6; i++) { gui.numbers[i].setText(null); } } } void addOneToField(JTextField field) { int num = Integer.parseInt("0" + field.getText()); num++; field.setText("" + num); } boolean numberGone(int num, JTextField[] pastNums, int count) { for (int i = 0; i < count; i++) { if (Integer.parseInt(pastNums[i].getText()) == num) { return true; } } return false; } boolean matchedOne(JTextField win, JTextField[] allPicks) { for (int i = 0; i < 6; i++) { String winText = win.getText(); if ( winText.equals( allPicks[i].getText() ) ) { return true; } } return false; } @Override public void run() { Thread thisThread = Thread.currentThread(); while (playing == thisThread) { addOneToField(gui.drawings); int draw = Integer.parseInt(gui.drawings.getText()); float numYears = (float)draw / 104; gui.years.setText("" + numYears); int matches = 0; for (int i = 0; i < 6; i++) { int ball; do { ball = (int)Math.floor(Math.random() * 50 + 1); } while (numberGone(ball, gui.winners, i)); gui.winners[i].setText("" + ball); if (matchedOne(gui.winners[i], gui.numbers)) { matches++; } } switch (matches) { case 3: addOneToField(gui.got3); break; case 4: addOneToField(gui.got4); break; case 5: addOneToField(gui.got5); break; case 6: addOneToField(gui.got6); gui.stop.setEnabled(false); gui.play.setEnabled(true); playing = null; } try { int pauseRate = Integer.parseInt(gui.pause.getText()); Thread.sleep(pauseRate); } catch (InterruptedException e) { // do nothing } catch (NumberFormatException nfe) { gui.pause.setText("100"); } } } }