package com.craigc.progen;
import java.awt.*;
import java.awt.event.*;
class JackAndJillPlay3 extends Frame {
    
    /* The Prop and Scene tables *******************/
    
    final static String title = "Jack and Jill #3";
    final int width = 300;
    final int height = 120;
    final int startScene = 0; // bottom
    
    // String[] propNames = { "up", "fetch", "fall", "down" };
    
    // initial labels
    final String[] propLabels = {
        "Go up the hill",
        "Fetch a pail of water",
        "Fall down, break crown",
        "Tumble down",
    };
    
    // map props to actions after each prop's action event
    final int[][][] propActions = {
        { { 1, 0 } },  // fetch "Fetch a pail of water"
        { { 1, 1 } },  // fetch "Fetch another pail"
        { { 2, 2 },    // fall "Break crown"
          { 3, 3 }, }, // tumble "Tumble after"
        {},
    };
    
    // map props to next scene
    final int[] nextScene = {
        1,  // top
        -1, // none
        0,  // bottom
        0,  // bottom
    };
    
    // String[] sceneNames = { "bottom", "top" };
    
    // map scenes to list of props to add for that scene
    final int[][] sceneProps = {
        { 0, 2 },    // up fall
        { 1, 2, 3 }, // fetch fall tumble
    };
    
    final String[] sceneColors = {
        "#8888aa",
        null,
    };
    final String[] traitValues = {
        "Fetch a pail of water",
        "Fetch another pail",
        "Break crown",
        "Tumble after",
    };
    
    /* The Events in the JackAndJillPlay *******************/
    
    class PropEvent implements ActionListener {
        int propnum;    // what prop this listener listens for
        
        public PropEvent(int j) {
            propnum = j;
        }
        
        public void actionPerformed(ActionEvent evt) {
            for (int j=0; j<propActions[propnum].length; ++j) {
                props[propActions[propnum][j][0]].setLabel(
                    traitValues[propActions[propnum][j][1]]);
            }
            
            if (nextScene[propnum] >= 0) {
                enterNewScene(nextScene[propnum]);
            }
        }
    }
    
    /* Creating and starting up the JackAndJillPlay *********/
    
    int currentScene;
    Button[] props; // the props
    
    public JackAndJillPlay3() {
        super(title);
        setSize(width, height);
        setLayout(new FlowLayout());
        
        // initialize props
        props = new Button[propLabels.length];
        for (int j=0; j<propLabels.length; ++j) {
            props[j] = new Button();
            props[j].setLabel(propLabels[j]);
            props[j].addActionListener(new PropEvent(j));
        }
  
        // start scene
        enterNewScene(startScene);
    }
    
    public void enterNewScene(int scene) {
        removeAll();  // remove previous scene if any
        currentScene = scene;
        for (int j=0; j<sceneProps[scene].length; ++j) {
            add(props[sceneProps[scene][j]]);
        }
        String bg = sceneColors[scene];
        setBackground(Color.decode(bg==null?"#dddddd":bg));
        show();
    }
    
    public static void main(String[] args) {
        new JackAndJillPlay3();
    }
}
 
 |