This is a Java game program that will run inside the JJ2
browser. By making slight modifications to this game, you can personalize
it to create your own game. Follow the directions below:
- Click on this text so that the next step works.
- Ctrl-A: to select all (it will scroll to the bottom).
- Program->Run-Selected: to run the selected code.
- Once you have played the game, copy it (Ctrl-A then Ctrl-C) and
place it in a new editor window. Read through and modify the code to
make your own game.
*/
import java.awt.*;
class GuessColor extends java.applet.Applet
{
private Button b1, b2, b3, b4, b5, b6;
private TextField tfBottom;
//You must write an init()
//which is called when the applet is opened
public void init()
{
setupLocationOfButtonsAndSuch();
} // end init
//The name of this method is whatever you want
//OR
//all this code could have been in the init() method
private void setupLocationOfButtonsAndSuch()
{
Panel pButtons2by3;
pButtons2by3 = new Panel (new GridLayout(2,3));
b1 = new Button("red");
b2 = new Button("orange");
b3 = new Button("yellow");
b4 = new Button("green");
b5 = new Button("blue");
b6 = new Button("purple");
pButtons2by3.add(b1);
pButtons2by3.add(b2);
pButtons2by3.add(b3);
pButtons2by3.add(b4);
pButtons2by3.add(b5);
pButtons2by3.add(b6);
tfBottom = new TextField("Please guess a color");
setLayout(new BorderLayout());
add("North", new Label("Pick a color, any color"));
add("Center", pButtons2by3);
add("South", tfBottom);
} // end setupLocationOfButtonsAndSuch
// If you extend JJAppletWithButtons, you need to
// write this handleButtonPress void method that
// is passed one Button value
public void handleButtonPress(Button b)
{
String replyText;
if (b == b1) {
replyText = "Not yet, please guess again";
} else if (b == b2) {
replyText = "Almost, guess again";
} else if (b == b3) {
replyText = "Nope, guess once more";
} else if (b == b4) {
replyText = "Incorrect guess, try again";
} else if (b == b5) {
replyText = "No, keep guessing";
} else { // (b == b6)
replyText = "YOU GUESSED IT!!!";
}
tfBottom.setText(replyText);
} //end handleButtonPress
//Java calls here when a button is pressed
public boolean action (Event e, Object o)
{
handleButtonPress((Button)e.target);
return true;
}//end action
} //end class GuessColor
|