// GuessApp.java import java.awt.*; import java.awt.event.*; public class GuessApp extends Frame implements ActionListener, KeyListener { private static final String NODATA = "__________"; private int guess, number, ct; private TextField guessField; private Label resultLabel, triesLabel; private Button ok, reset; private GuessGameStarter ggs = null; private boolean runsFromApplet; public GuessApp(GuessGameStarter _ggs, boolean _runsFromApplet) { super("The Guessing Game"); ggs = _ggs; runsFromApplet = _runsFromApplet; setBackground(SystemColor.control); Label title = new Label("The Guessing Game", Label.CENTER); title.setFont(new Font("sansserif", Font.BOLD, 30)); Label prompt = new Label("Guess the number the computer picked (1..100):"); guessField = new TextField(3); guessField.addKeyListener(this); guessField.setBackground(Color.white); ok = new Button("OK"); ok.addActionListener(this); Label result = new Label("Your guess was: ", Label.RIGHT); resultLabel = new Label(NODATA, Label.LEFT); Panel titlePanel = new Panel(); titlePanel.setLayout(new FlowLayout()); titlePanel.add(title); Panel labelPanel = new Panel(); labelPanel.setLayout(new FlowLayout()); labelPanel.add(prompt); labelPanel.add(guessField); labelPanel.add(ok); Label noOfTries = new Label("Number of tries: ", Label.RIGHT); triesLabel = new Label(NODATA, Label.LEFT); Panel resultPanel = new Panel(); resultPanel.setLayout(new GridLayout(1, 2)); resultPanel.add(result); resultPanel.add(resultLabel); Panel triesPanel = new Panel(); triesPanel.setLayout(new GridLayout(1, 2)); triesPanel.add(noOfTries); triesPanel.add(triesLabel); reset = new Button("Reset"); reset.addActionListener(this); Panel buttonPanel = new Panel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(reset); Panel bottomPanel = new Panel(); bottomPanel.setLayout(new GridLayout(4, 1)); bottomPanel.add(labelPanel); bottomPanel.add(resultPanel); bottomPanel.add(triesPanel); bottomPanel.add(buttonPanel); Panel wholePanel = new Panel(); wholePanel.setLayout(new BorderLayout()); wholePanel.add("North", titlePanel); wholePanel.add("Center", bottomPanel); setLayout(new BorderLayout()); add("North", wholePanel); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (runsFromApplet) ggs.close(); else System.exit(0); } }); number = getNumber(); ct = 0; validate(); pack(); setResizable(false); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); Point p = new Point((int)((d.getWidth() - this.getWidth())/2), (int)((d.getHeight() - this.getHeight())/2)); setLocation(p); guessField.requestFocus(); setVisible(true); } public int getNumber() { int num = (int)(Math.random() * 100); while (num == 0) num = (int)(Math.random() * 100); return num; } public void processEntry() { if (guessField.getText().equals("")) { MessageBox mb = new MessageBox(ggs, this, "Error", "You have not entered a number!", "exclaim.gif"); } else { try { String s = guessField.getText().trim(); guessField.setText(s); guess = Integer.parseInt(s); if (guess == number) { ct += 1; resultLabel.setText("correct!"); triesLabel.setText(Integer.toString(ct)); if (ct == 1) { MessageBox mb = new MessageBox(ggs, this, "Congratulations", "You guessed the number in " + ct + " try.", "smile.gif"); } else if (ct <= 7) { MessageBox mb = new MessageBox(ggs, this, "Congratulations", "You guessed the number in " + ct + " tries.", "smile.gif"); } else { MessageBox mb = new MessageBox(ggs, this, "Boo!", "You guessed the number, but it took you " + ct + " tries.", "frown.gif"); } number = getNumber(); ct = 0; guessField.setText(""); resultLabel.setText(NODATA); triesLabel.setText(NODATA); } else if (guess < number) { if (guess < 1) { MessageBox mb = new MessageBox(ggs, this, "Error", "Your guess is outside the specified range.", "exclaim.gif"); } else { ct += 1; resultLabel.setText("too low"); triesLabel.setText(Integer.toString(ct)); } } else { if (guess > 100) { MessageBox mb = new MessageBox(ggs, this, "Error", "Your guess is outside the specified range.", "exclaim.gif"); } else { ct += 1; resultLabel.setText("too high"); triesLabel.setText(Integer.toString(ct)); } } } catch (NumberFormatException nfe) { MessageBox mb = new MessageBox(ggs, this, "Error", "Invalid input for guess.", "exclaim.gif"); } } guessField.select(0, guessField.getText().length()); } public void keyPressed(KeyEvent ke) { if (ke.getKeyText(ke.getKeyCode()).equals("Enter")) processEntry(); } public void keyReleased(KeyEvent ke) { } public void keyTyped(KeyEvent ke) { } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == reset) { number = getNumber(); ct = 0; guessField.setText(""); resultLabel.setText(NODATA); triesLabel.setText(NODATA); guessField.requestFocus(); } else if (ae.getSource() == ok) { guessField.requestFocus(); processEntry(); } } public static void main(String[] args) { GuessApp ga = new GuessApp(null, false); } }