/** * Program creates a GUI that then adds to a String variable story and displays * the string in a TextArea output. It receives input from the user into the * JTextArea userInput and then passes that input off for error checking by * author, an instance of the StoryTeller class. It also passes the memory * address of an instance of the Story class called theStory to author so that * author can display the text in the various methods there based on the user's * input. * ------------------------------------------------------ * Programmer: * Program: Wounded * Last modified: March 3, 2013 5:00 PM Central Time * ------------------------------------------------------ */ import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JTextArea; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JScrollPane; import java.awt.Font; import javax.swing.JLabel; public class MainProgram extends JApplet implements ActionListener { //Declare all the necessary variables and initialize as needed. private JTextArea userInput; private JTextArea output; private JScrollPane scrollPane; private boolean canProceed; static final long serialVersionUID = 1; // added in an attempt to fix an error, highly confused as to what it does. public String story = "My Little Pony: Friendship is Magic is the property of\n" + "Hasbro, Lauren Faust, and the rest of its creative team. \n" + "Please support the official release. \n\n" + "Your eyes are bleary... you feel drained, so very " + "drained... and dizzy. \nYou tilt your head over to " + "the side of the rickety cot you're lying on and \n" + "manage to throw up. To your surprise, there's a " + "basin waiting for you.\n" + "\"Hey there,\" comes a voice.\n" + "You see an Earth Pony mare in an orange vest sitting at your bedside.\n" + "She gives you a half hearted smile. \"Welcome to the land of the living.\" \n" + "\"Can you tell me your name?\" She asks. \n" + "1)Heavy Hooves\n" + "2)Careful Crafts\n" + "3)Free Faller\n" + "4)Lie about name\n\n"; public String input; StoryTeller author = new StoryTeller(0, 0, 0, 0, 0, 0); Story theStory = new Story(); public void init() { //Add the necessary content pane and set the layout. Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); //Create Wounded Label JLabel title = new JLabel(); title.setText("WOUNDED"); title.setFont(new Font("Courier", Font.PLAIN, 28)); contentPane.add(title); //Create the JTextArea for user input. userInput = new JTextArea(1, 20); userInput.setText("Enter your choice here"); contentPane.add(userInput); //Create the essential choose button to signal that //the program should retrieve user input from userInput. JButton chooseButton = new JButton(); chooseButton.setText("Choose"); chooseButton.addActionListener(this); contentPane.add(chooseButton); //Create the JTextArea for the display of story. output = new JTextArea(20, 100); scrollPane = new JScrollPane(output); output.setFont(new Font("Courier", Font.PLAIN, 16)); output.setText(story); contentPane.add(scrollPane); } /*Method that realizes when chooseButton has been pressed * and then stores the string entered into userInput * in input. Then it checks if it can proceed with that * input by calling the author.readInput method which * also returns a boolean which is stored in canProceed. * If canProceed is true, the program invokes the * author.continueStory method which returns the new story * (among other things discussed in the documentation * for StoryTeller). If canProceed is false, then * an error message is added to story. In either case * the text of output is set to story. */ public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("Choose")) { input = userInput.getText(); canProceed = author.readInput(input); if (canProceed) { story = author.continueStory(story, theStory); output.setText(story); } else { story = story + "\n Not a valid choice, choose again:\n"; output.setText(story); } } } }