diff --git a/cit591-madlibs/src/madlibs/MadLib.java b/cit591-madlibs/src/madlibs/MadLib.java index 41a5f78..c62415b 100755 --- a/cit591-madlibs/src/madlibs/MadLib.java +++ b/cit591-madlibs/src/madlibs/MadLib.java @@ -5,6 +5,9 @@ import java.util.ArrayList; import java.util.Scanner; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; + /** * MadLib class is the primary class of the Literature Mad-Lib game. It will call on * all the other methods and classes to run the game. @@ -49,9 +52,12 @@ public class MadLib { private int firstStart; private int invalidCount; private int menuSize; + public MadLib() { + classicsMenu = new ArrayList(); + childrensMenu = new ArrayList(); } @@ -62,20 +68,10 @@ public MadLib() { */ public void madLibRunner() { - //Opens UserInterface window - - //Design Progress Note: Window in demo stage, game still played in console at this point - // ** UserInterface == demo ; UserInterface1 == interface test area - - UserInterface ui = new UserInterface(); - ui.windowSetUp(); - - // Program greeting and menu prompt. Loop prevents introduction from being // presented each time madLibRunner() method is called. - while (firstStart == 0) { System.out.println(); System.out.println("Welcome to Literature Mad-Libs"); @@ -85,9 +81,10 @@ public void madLibRunner() { firstStart++; } + + //Reads in the player's choice of literature based on menu, verifies valid input, identifies passage file name. Scanner myScanner = new Scanner(System.in); - //Reads in the player's choice of literature based on menu, verifies valid input, identifies passage file name. int litChoice; String passageFileName; Passage passage; @@ -127,8 +124,7 @@ public void madLibRunner() { invalidStart(invalidCount, invalidEntry); } - - + myScanner.close(); } @@ -142,8 +138,8 @@ public void madLibRunner() { */ public void makeMenu () { File indexFile = new File("index.csv"); - classicsMenu = new ArrayList(); - childrensMenu = new ArrayList(); + //classicsMenu = new ArrayList(); + //childrensMenu = new ArrayList(); try { Scanner in = new Scanner(indexFile); while (in.hasNextLine()) { @@ -175,12 +171,9 @@ else if (litFileName.contains("classic")) { e.printStackTrace(); } - + //Displays the Children's Literature menu to console int i; int j; - - - //Displays the Children's Literature menu to console System.out.println(); System.out.println("Children's Literature"); @@ -192,11 +185,38 @@ else if (litFileName.contains("classic")) { //Displays the Classic Literature menu to console System.out.println("Classic Literature"); for (j = childrensMenu.size() + 1; j <= childrensMenu.size() + classicsMenu.size(); j++) { - System.out.println(j+ " " + classicsMenu.get(j - childrensMenu.size() - 1).getLitTitle() + " by" + childrensMenu.get(j - childrensMenu.size() - 1).getLitAuthor()); + System.out.println(j+ " " + classicsMenu.get(j - childrensMenu.size() - 1).getLitTitle() + " by" + classicsMenu.get(j - childrensMenu.size() - 1).getLitAuthor()); } - System.out.println(); + System.out.println(); + + //Opens UserInterface window and passes childrensMenu and classicMenu to UserInterface1 + //to display menu + + JFrame window = new JFrame("Literature Mad-Libs Game"); + window.setContentPane(new UserInterface1(childrensMenu, classicsMenu)); + //.setSize sets the frame size -- without it the frame would be tiny + window.setSize(1100, 600); + //window.pack(); + //exits the program when the window is closed + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.setResizable(true); + window.setLocation(150, 100); + //.setVisible allows frame to be visible + window.setVisible(true); + + } + + public ArrayList getClassicsMenu() { + return classicsMenu; + } + + public ArrayList getChildrensMenu() { + return childrensMenu; } + + + /** * promptForReplacement is a temporary method that demonstrates how to use the passage object * to determine which indexes should be replaced, prompt the user for the @@ -272,6 +292,7 @@ public void invalidStart(int invalidCount, String notInteger) { } public static void main(String[] args) { + MadLib m = new MadLib(); m.madLibRunner(); diff --git a/cit591-madlibs/src/madlibs/UserInterface.java b/cit591-madlibs/src/madlibs/UserInterface.java index e9e0584..e93b3e2 100644 --- a/cit591-madlibs/src/madlibs/UserInterface.java +++ b/cit591-madlibs/src/madlibs/UserInterface.java @@ -9,7 +9,7 @@ * * @author Team 42 - Ross Beck-MacNeil, Paul Lysander, and Brenda Moss * - * Design Progress Notes: Transition from console to GUI is a work in progress. Currently + * Design Progress Notes: Transition from console to GUI is a work in progress. Currently UserInterface * contains demo of a button, a text input box, a text box (pop-up menu), and a text area. */ public class UserInterface extends JPanel implements ActionListener { @@ -108,7 +108,7 @@ public void actionPerformed(ActionEvent evt) { * Literature Mad-Libs game */ public void windowSetUp() { - JFrame window = new JFrame("Literature Mad-Libs Game"); + JFrame window = new JFrame("Demo of Swing Design Options for Mad-Lib Game"); window.setContentPane(new UserInterface()); window.pack(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); diff --git a/cit591-madlibs/src/madlibs/UserInterface1.java b/cit591-madlibs/src/madlibs/UserInterface1.java index dd0e9f0..b2e5fdc 100644 --- a/cit591-madlibs/src/madlibs/UserInterface1.java +++ b/cit591-madlibs/src/madlibs/UserInterface1.java @@ -1,5 +1,150 @@ package madlibs; -public class UserInterface1 { +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.lang.reflect.Array; +import java.util.ArrayList; +/** + * UserInterface1 class is a play area for testing implementation of GUI components using Swing library. + * + * @author Team 42 - Ross Beck-MacNeil, Paul Lysander, and Brenda Moss + * + * Design Progress Notes: This class is a test area for figuring out how to transition from console to GUI. + */ +public class UserInterface1 extends JPanel implements ActionListener { + + private static final int STANDARD_WIDTH = 640; + private JTextArea text; // a message will be posted to this text area + // each time an event is generated by some + // user action + + private JComboBox combobox; // The pop-up menu. + + + /** + * This constructor adds several GUI components to the panel and listens + * for action events from some of them. + */ + public UserInterface1(ArrayList childrensMenu, ArrayList classicsMenu) { + + setBorder(BorderFactory.createLineBorder(Color.GRAY, 3)); + setBackground(Color.WHITE); + + + + //Sets BorderLayout as overall layout and inserts title in the top (NORTH) cell + + setLayout(new BorderLayout()); + JLabel title = new JLabel("Welcome to Literature Mad-Libs"); + title.setHorizontalAlignment(SwingConstants.CENTER); + title.setPreferredSize(new Dimension(STANDARD_WIDTH, 50)); + add(title, BorderLayout.NORTH); + + + //Nests GridLayout into the left-middle (WEST) cell of the overall BorderLayout + //This area will include radio buttons to select the literature menu + JPanel litMenu = new JPanel(); + //Calculates numberMenuRows for GridLayout based on how many passage options are available + //plus three to compensate for the instructions and "Children's Literature" & "Classic Literature" labels + int numMenuRows = (childrensMenu.size() + classicsMenu.size() + 3); + litMenu.setLayout(new GridLayout(numMenuRows, 1, 3, 10)); + litMenu.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); + add(litMenu, BorderLayout.WEST); + + //Populates the left-middle (WEST) cell with instructions and radio buttons for each menu option + JLabel instructions = new JLabel("Please select a literature passage you would like to Mad-Lib: "); + litMenu.add(instructions); + + int i; + int j; + JRadioButton[] radioButton = new JRadioButton[(childrensMenu.size() + classicsMenu.size() + 1)]; + ButtonGroup group = new ButtonGroup(); + + + //Displays the Children's Literature menu options + JLabel childrenLabel = new JLabel("Children's Literature:"); + litMenu.add(childrenLabel); + + + for (i = 1; i <= childrensMenu.size(); i++){ + radioButton[i] = new JRadioButton(childrensMenu.get(i-1).getLitTitle() + " by" + childrensMenu.get(i-1).getLitAuthor()); + litMenu.add(radioButton[i]); + group.add(radioButton[i]); + } + + + + //Displays the Classic Literature menu to console + JLabel classicLabel = new JLabel("Classic Literature:"); + litMenu.add(classicLabel); + + + for (j = childrensMenu.size() + 1; j <= (childrensMenu.size() + classicsMenu.size()); j++) { + radioButton[j] = new JRadioButton(classicsMenu.get(j - childrensMenu.size() - 1).getLitTitle() + " by" + classicsMenu.get(j - childrensMenu.size() - 1).getLitAuthor()); + litMenu.add(radioButton[j]); + group.add(radioButton[j]);; + } + + + + //Places scrolling text area into the right-middle (EAST) cell of the overall BorderLayout + text = new JTextArea(); + text.setEditable(false); + text.setMargin(new Insets(4, 4, 4, 4)); + add(new JScrollPane(text), BorderLayout.CENTER); + post("Eventually the original text of each passage will be displayed here when the player clicks on the menu selection."); + + //Places a "PLAY MAD-LIBS button into the bottom cell of the overall BorderLayout + JButton playButton = new JButton(); + + //Nests another BorderLayout within the button in order to display a 2-line button + playButton.setLayout(new BorderLayout()); + JLabel label1 = new JLabel("PLAY MAD-LIBS"); + JLabel label2 = new JLabel("with selected passage"); + label1.setHorizontalAlignment(SwingConstants.CENTER); + label2.setHorizontalAlignment(SwingConstants.CENTER); + playButton.add(BorderLayout.NORTH,label1); + playButton.add(BorderLayout.SOUTH,label2); + + playButton.addActionListener(this); + add(playButton, BorderLayout.SOUTH); + + + + } + + // end of constructor + + + private void post(String message) { // add a message and line feed to the text + text.append(message + "\n\n"); + } + + /** + * Respond to an ActionEvent from one of the GUI components in the panel. + * In each case, a message about the event is posted to the text area. + * This method is part of the ActionListener interface. + * + * Design Progress Note: This section needs to be expanded to display the text when each + * radio button is pressed. + */ + public void actionPerformed(ActionEvent evt) { + Object target = evt.getSource(); // component that produced this event + if (target instanceof JButton) { + // Initiates program for that passage + post("Eventually the Mad-Lib game will run when you push this button"); + } + else if (target instanceof JRadioButton) { + if (((JRadioButton) target).isSelected()) + post("Checkbox was turned on."); + else + post("Checkbox was turned off."); + } + } + + } + +