Advertisement
SageTheWizard

BorderLayoutFrame.java

Apr 23rd, 2018
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.05 KB | None | 0 0
  1. /*
  2.   Author: Jacob Gallucci
  3.   Course: CMPSC 221
  4.   Assignment: Programming Assignment 4 - Trivia Game Super GUI Edition
  5.   Due date: 4/24/2018
  6.   File: BorderLayoutFrame.java
  7.   Purpose: BorderLayoutFrame.java  - BorderLayoutFrame is the code for the GUI, sets up Screen in connstructor, sets up question array and
  8.                                      game is primarily contained in the button action listener
  9.   Compiler/IDE: OpenJDK 1.8.0_151 (compiled from CLI) - Atom / Vim text editors
  10.   Operating system: Debian Stretch 9
  11.   Reference(s): https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html - GridLayout
  12.                 https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html - Action listener
  13.   Extra Note: Don't question why it's called BorderLayoutFrame when I'm using grid layout.. I startd with BorderLayout.. got frustrated and
  14.               Switched to GridLayout and never bothered to change it (it_just_works.java)
  15. */
  16. import javax.swing.*;         // Used for GUI
  17. import java.awt.*;            // for gridlayout and fancy colors
  18. import java.io.*;             // Used for standard IO
  19. import java.awt.event.*;      // For some reason just importing java.awt.* doesn't get the action listner stuff so this needs to be here for that
  20.  
  21. public class BorderLayoutFrame extends JFrame implements ActionListener
  22. {
  23.   private Boolean start = false;     // Start boolean
  24.   private int indexOfQuestions = 0; // Used to index through the question array
  25.   private int score = 0;           // User's score
  26.  
  27.   private Question[] questionBank = new Question[10];                                 // Question Array
  28.  
  29.   private JLabel scoreLabel = new JLabel("Score: " + score);                        // Displays user's score
  30.   private JLabel questionBoi = new JLabel("Click Button to Get first Question");   // Displays current question
  31.   private JTextField userAnswer = new JTextField("Input Text Here", 50);          // Textbox to get user input
  32.   private JLabel answerBoi = new JLabel("");                                     // Displays if user is correct or incorrect
  33.  
  34.   public static final int x = 800;   // Horizontal size of the screen
  35.   public static final int y = 100;  // Vertical size of the screen
  36.  
  37.   public BorderLayoutFrame() // Constructor  - Sets up screen
  38.   {
  39.     super(); // Calls JFrame's constructor
  40.     setSize(x,y);  // Sets the screen size
  41.     setTitle("CSGO TRIVIA TIME BY JACOB GALLUCCI!!!!"); // Sets the title bar's text
  42.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Sets the frame to end program on clicking the x
  43.     setLayout(new GridLayout(5,1));     // Sets layout to a 5x1 grid
  44.  
  45.     for (int i = 0; i < 10; i++)  // Initializes all the questions in the array
  46.       questionBank[i] = new Question();
  47.  
  48.     questionBank = setupQuestions(questionBank); // sets up the question array with question, answer and score value
  49.  
  50.     JButton submitAnswer = new JButton("CLICK TO SUBMIT YOUR ANSWER"); // Sets up button to click to check answer
  51.     submitAnswer.addActionListener(this);  // Creates an actionlistener for the button
  52.  
  53.     add(scoreLabel);     // adds scoreLabel to the frame
  54.     add(questionBoi);   // Adds question label to the frame
  55.     add(userAnswer);        // adds usersAnswer (text box) to frame
  56.     add(submitAnswer);     // adds button to screen
  57.     add(answerBoi);       // adds answerBoi to the screen
  58.   }
  59.  
  60.   // action listener method -- Most code is in here
  61.   public void actionPerformed(ActionEvent action)
  62.   {
  63.     String playerAnswer; // String for player's answer
  64.     if ((action.getActionCommand()).equals("CLICK TO SUBMIT YOUR ANSWER")) // if the button is clicked
  65.     {
  66.       if (!start && (indexOfQuestions == 0)) // If the game has not started
  67.       {
  68.         questionBoi.setText(questionBank[indexOfQuestions].displayQuestion()); // Display the first questions
  69.         start = true; // sets game started boolean to true
  70.       }
  71.       else if(start && (indexOfQuestions < 10)) // if game has started and not reached the end of the questions
  72.       {
  73.         playerAnswer = (userAnswer.getText()).trim();  // stores a trimmed version of the user's answer into playerAnswer string
  74.  
  75.         if (questionBank[indexOfQuestions].evalUserAnswer(playerAnswer)) // if playerAnswer is the same as the answer
  76.         {
  77.           score += questionBank[indexOfQuestions].getPointVal(); // add the point value to the current socre
  78.           scoreLabel.setText("Score: " + score);   // Update store
  79.           answerBoi.setText("Correct!");          // Display that the user was correct
  80.           answerBoi.setForeground(Color.green);  // Changes text color to green so that the word Correct! is green  (Fancy)
  81.  
  82.           if (indexOfQuestions != 9) // if the user is not on the last question
  83.           {
  84.             indexOfQuestions++; // Increments question index
  85.             questionBoi.setText(questionBank[indexOfQuestions].displayQuestion()); // updates to the new question
  86.             userAnswer.setText("Input Answer Here"); // Resets the userAnswer Text box
  87.           }
  88.           else // if index of questions = 9
  89.           {
  90.             start = false; // changes start to false, ending the game
  91.           }
  92.         }
  93.         else // if the user is incorrect
  94.         {
  95.           answerBoi.setText("Incorrect! - Correct Answer: " + questionBank[indexOfQuestions].displayAnswer() + " | Your Answer:" + playerAnswer); // Tells user they're incorrect and displays the correct answer
  96.           answerBoi.setForeground(Color.red);  // Changes the above label to red (fancy)
  97.           indexOfQuestions++; //increments question index
  98.           questionBoi.setText(questionBank[indexOfQuestions].displayQuestion()); // displays new question
  99.           userAnswer.setText("Input Answer Here"); // resets userAnswer text box
  100.         }
  101.       }
  102.       else if (!start && (indexOfQuestions != 0)) // if the game is over
  103.       {
  104.         answerBoi.setText("Trivia game over! Your score: " + score + " close the and rerun to go again"); // Displays final score
  105.         answerBoi.setForeground(Color.blue); // changes above text to blue
  106.       }
  107.     }
  108.   }
  109.   private static Question[] setupQuestions(Question[] questions) // Sets up the questions from a text document
  110.   {
  111.     int i = 0; // Counter Integer
  112.     String dummyString; // String used to store input from text document
  113.     // Tries This
  114.     try
  115.     {
  116.       FileReader reader = new FileReader("questions.txt");     // Sets up file reader
  117.       BufferedReader buffReader = new BufferedReader(reader); // Sets up buffer reader
  118.  
  119.        /**
  120.         *  note: This reads in the question first and answer second, a modified text file may that is not
  121.         *        in this format will produce some funky outputs / outputting answers and expecting a question as an answer
  122.         *        Or the score value (which is supposed to be an integer) might crash the whole program if something is out of place
  123.         */
  124.       // this loop inputs data from file into question object while there is data in the file or integer i is less than 10
  125.       while (((dummyString = buffReader.readLine()) != null) || (i < 10))
  126.       {
  127.         questions[i].setQuest(dummyString);     // Sets question to the most recent line read from the file
  128.         dummyString = buffReader.readLine();   // Reads the next line (the answer to the previous line)
  129.         questions[i].setAnswer(dummyString);  // Sets Answer to the most recent line read from the file
  130.         dummyString = buffReader.readLine();   // Reads the next line (the score value for the previous answer / question pair )
  131.         questions[i].setPointVal(Integer.parseInt(dummyString)); // Sets the point value to the most recent line read from the file
  132.         i++;
  133.       }
  134.     }
  135.     // If the file is not found
  136.     catch(FileNotFoundException ex)
  137.     {
  138.       System.out.println("File not found!");
  139.     }
  140.     // If the file is broken
  141.     catch(IOException ex)
  142.     {
  143.       System.out.println("File broken!");
  144.     }
  145.     return questions; // returns questions array to the main after setting up the array of questions
  146.   }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement