import java.awt.*;
import java.awt.event.*;
class JackAndJillPlay2 extends Frame {
/* The Props for JackAndJill ************/
Button upProp
= new Button("Go up the hill");
Button fetchProp
= new Button("Fetch a pail of water");
Button fallProp
= new Button("Fall down, break crown");
Button tumbleProp
= new Button("Tumble down");
/* The Events in the JackAndJill ********/
class PropEvent implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Object prop = evt.getSource();
if (prop.equals(upProp)) {
fetchProp.setLabel("Fetch a pail of water");
enterNewScene("top");
} else if (prop.equals(fetchProp)) {
fetchProp.setLabel("Fetch another pail");
} else if (prop.equals(fallProp)) {
fallProp.setLabel("Break crown");
tumbleProp.setLabel("Tumble after");
enterNewScene("bottom");
} else if (prop.equals(tumbleProp)) {
enterNewScene("bottom");
} else {
System.out.println("Invalid prop");
}
}
}
/* Creating and starting up the JackAndJill ****/
String currentScene;
public JackAndJillPlay2() {
super("Jack and Jill");
setSize(200, 120);
setLayout(new FlowLayout());
// initialize props
PropEvent a = new PropEvent();
upProp.addActionListener(a);
fetchProp.addActionListener(a);
fallProp.addActionListener(a);
tumbleProp.addActionListener(a);
// start scene
enterNewScene("bottom");
}
public void enterNewScene(String scene) {
removeAll(); // remove previous scene
currentScene = scene;
if (scene.equals("bottom")) {
add(upProp);
add(fallProp);
setBackground(Color.decode("#8888aa"));
} else if (scene.equals("top")) {
add(fetchProp);
add(fallProp);
add(tumbleProp);
setBackground(Color.decode("#dddddd"));
} else {
System.out.println("Invalid scene: "+scene);
}
show();
}
public static void main(String[] args) {
new JackAndJillPlay2();
}
}
|