1 29 30 package echo2tutorial.numberguess; 31 32 import nextapp.echo2.app.ApplicationInstance; 33 import nextapp.echo2.app.Button; 34 import nextapp.echo2.app.Color; 35 import nextapp.echo2.app.ContentPane; 36 import nextapp.echo2.app.Extent; 37 import nextapp.echo2.app.Insets; 38 import nextapp.echo2.app.Label; 39 import nextapp.echo2.app.ResourceImageReference; 40 import nextapp.echo2.app.Column; 41 import nextapp.echo2.app.TextField; 42 import nextapp.echo2.app.Window; 43 import nextapp.echo2.app.event.ActionEvent; 44 import nextapp.echo2.app.event.ActionListener; 45 import nextapp.echo2.app.layout.ColumnLayoutData; 46 47 50 public class NumberGuessApp extends ApplicationInstance { 51 52 private Window window; 53 54 61 void congratulate(int numberOfTries) { 62 window.setContent(new CongratulationsPane(numberOfTries)); 63 } 64 65 68 public Window init() { 69 window = new Window(); 70 window.setTitle("Echo2 Guess-A-Number"); 71 startNewGame(); 72 return window; 73 } 74 75 79 void startNewGame() { 80 window.setContent(new GamePane()); 82 } 83 } 84 85 89 class GamePane extends ContentPane 90 implements ActionListener { 91 92 93 private int randomNumber = ((int) Math.floor(Math.random() * 100)) + 1; 94 95 96 private int lowerBound = 1; 97 98 99 private int upperBound = 100; 100 101 102 private int numberOfTries = 0; 103 104 105 private TextField guessEntryField; 106 107 112 private Label statusLabel = new Label(); 113 114 117 private Label countLabel = new Label("You have made no guesses."); 118 119 124 private Label promptLabel = new Label("Guess a number between 1 and 100: "); 125 126 129 GamePane() { 130 super(); 131 132 Column layoutColumn = new Column(); 133 layoutColumn.setInsets(new Insets(30)); 134 layoutColumn.setCellSpacing(new Extent(10)); 135 add(layoutColumn); 136 137 layoutColumn.add(new Label(new ResourceImageReference("/echo2tutorial/numberguess/TitleBanner.png"))); 138 layoutColumn.add(statusLabel); 139 layoutColumn.add(countLabel); 140 layoutColumn.add(promptLabel); 141 142 guessEntryField = new TextField(); 143 144 guessEntryField.setForeground(Color.WHITE); 145 guessEntryField.setBackground(Color.BLUE); 146 ColumnLayoutData columnLayoutData = new ColumnLayoutData(); 147 columnLayoutData.setInsets(new Insets(20, 0)); 148 guessEntryField.setLayoutData(columnLayoutData); 149 layoutColumn.add(guessEntryField); 150 151 Button submitButton = new Button("Submit Your Guess"); 152 submitButton.setActionCommand("submit guess"); 153 submitButton.setForeground(Color.BLACK); 154 submitButton.setBackground(Color.GREEN); 155 submitButton.setWidth(new Extent(200)); 156 submitButton.addActionListener(this); 157 layoutColumn.add(submitButton); 158 159 Button newGameButton = new Button("Start a New Game"); 160 newGameButton.setActionCommand("new game"); 161 newGameButton.setForeground(Color.WHITE); 162 newGameButton.setBackground(Color.RED); 163 newGameButton.setWidth(new Extent(200)); 164 newGameButton.addActionListener(this); 165 layoutColumn.add(newGameButton); 166 } 167 168 171 public void actionPerformed(ActionEvent e) { 172 if (e.getActionCommand().equals("new game")) { 173 ((NumberGuessApp) ApplicationInstance.getActive()).startNewGame(); 174 } else if (e.getActionCommand().equals("submit guess")) { 175 processGuess(); 176 } 177 } 178 179 182 private void processGuess() { 183 184 int guess; 185 try { 186 guess = Integer.parseInt(guessEntryField.getDocument().getText()); 187 } catch (NumberFormatException ex) { 188 statusLabel.setText("Your guess was not valid."); 189 return; 190 } 191 192 ++numberOfTries; 193 194 if (guess == randomNumber) { 195 ((NumberGuessApp) ApplicationInstance.getActive()).congratulate(numberOfTries); 196 return; 197 } 198 199 if (guess < 1 || guess > 100) { 200 statusLabel.setText("Your guess, " + guess + " was not between 1 and 100."); 201 } else if (guess < randomNumber) { 202 if (guess >= lowerBound) { 203 lowerBound = guess + 1; 204 } 205 statusLabel.setText("Your guess, " + guess + " was too low. Try again:"); 206 } else if (guess > randomNumber) { 207 statusLabel.setText("Your guess, " + guess + " was too high. Try again:"); 208 if (guess <= upperBound) { 209 upperBound = guess - 1; 210 } 211 } 212 213 if (numberOfTries == 1) { 215 countLabel.setText("You have made 1 guess."); 216 } else { 217 countLabel.setText("You have made " + numberOfTries + " guesses."); 218 } 219 220 promptLabel.setText("Guess a number between " + lowerBound + " and " + upperBound + ": "); 222 } 223 } 224 225 229 class CongratulationsPane extends ContentPane 230 implements ActionListener { 231 232 238 CongratulationsPane(int numberOfTries) { 239 Column layoutColumn = new Column(); 240 layoutColumn.setInsets(new Insets(30)); 241 layoutColumn.setCellSpacing(new Extent(30)); 242 add(layoutColumn); 243 244 layoutColumn.add(new Label(new ResourceImageReference("/echo2tutorial/numberguess/CongratulationsBanner.png"))); 245 layoutColumn.add(new Label("You got the correct answer in " + numberOfTries + (numberOfTries == 1 ? " try." : " tries."))); 246 247 Button button = new Button("Play Again"); 248 button.setForeground(Color.WHITE); 249 button.setBackground(Color.RED); 250 button.setWidth(new Extent(200)); 251 button.addActionListener(this); 252 layoutColumn.add(button); 253 } 254 255 258 public void actionPerformed(ActionEvent e) { 259 ((NumberGuessApp) ApplicationInstance.getActive()).startNewGame(); 260 } 261 } 262 | Popular Tags |