Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Jacob Gallucci
- E-mail: [email protected]
- Course: CMPSC 221
- Assignment: Programming Assignment 4 - Trivia Game Super GUI Edition
- Due date: 4/24/2018
- File: BorderLayoutFrame.java
- Purpose: BorderLayoutFrame.java - BorderLayoutFrame is the code for the GUI, sets up Screen in connstructor, sets up question array and
- game is primarily contained in the button action listener
- Compiler/IDE: OpenJDK 1.8.0_151 (compiled from CLI) - Atom / Vim text editors
- Operating system: Debian Stretch 9
- Reference(s): https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html - GridLayout
- https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html - Action listener
- Extra Note: Don't question why it's called BorderLayoutFrame when I'm using grid layout.. I startd with BorderLayout.. got frustrated and
- Switched to GridLayout and never bothered to change it (it_just_works.java)
- */
- import javax.swing.*; // Used for GUI
- import java.awt.*; // for gridlayout and fancy colors
- import java.io.*; // Used for standard IO
- 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
- public class BorderLayoutFrame extends JFrame implements ActionListener
- {
- private Boolean start = false; // Start boolean
- private int indexOfQuestions = 0; // Used to index through the question array
- private int score = 0; // User's score
- private Question[] questionBank = new Question[10]; // Question Array
- private JLabel scoreLabel = new JLabel("Score: " + score); // Displays user's score
- private JLabel questionBoi = new JLabel("Click Button to Get first Question"); // Displays current question
- private JTextField userAnswer = new JTextField("Input Text Here", 50); // Textbox to get user input
- private JLabel answerBoi = new JLabel(""); // Displays if user is correct or incorrect
- public static final int x = 800; // Horizontal size of the screen
- public static final int y = 100; // Vertical size of the screen
- public BorderLayoutFrame() // Constructor - Sets up screen
- {
- super(); // Calls JFrame's constructor
- setSize(x,y); // Sets the screen size
- setTitle("CSGO TRIVIA TIME BY JACOB GALLUCCI!!!!"); // Sets the title bar's text
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Sets the frame to end program on clicking the x
- setLayout(new GridLayout(5,1)); // Sets layout to a 5x1 grid
- for (int i = 0; i < 10; i++) // Initializes all the questions in the array
- questionBank[i] = new Question();
- questionBank = setupQuestions(questionBank); // sets up the question array with question, answer and score value
- JButton submitAnswer = new JButton("CLICK TO SUBMIT YOUR ANSWER"); // Sets up button to click to check answer
- submitAnswer.addActionListener(this); // Creates an actionlistener for the button
- add(scoreLabel); // adds scoreLabel to the frame
- add(questionBoi); // Adds question label to the frame
- add(userAnswer); // adds usersAnswer (text box) to frame
- add(submitAnswer); // adds button to screen
- add(answerBoi); // adds answerBoi to the screen
- }
- // action listener method -- Most code is in here
- public void actionPerformed(ActionEvent action)
- {
- String playerAnswer; // String for player's answer
- if ((action.getActionCommand()).equals("CLICK TO SUBMIT YOUR ANSWER")) // if the button is clicked
- {
- if (!start && (indexOfQuestions == 0)) // If the game has not started
- {
- questionBoi.setText(questionBank[indexOfQuestions].displayQuestion()); // Display the first questions
- start = true; // sets game started boolean to true
- }
- else if(start && (indexOfQuestions < 10)) // if game has started and not reached the end of the questions
- {
- playerAnswer = (userAnswer.getText()).trim(); // stores a trimmed version of the user's answer into playerAnswer string
- if (questionBank[indexOfQuestions].evalUserAnswer(playerAnswer)) // if playerAnswer is the same as the answer
- {
- score += questionBank[indexOfQuestions].getPointVal(); // add the point value to the current socre
- scoreLabel.setText("Score: " + score); // Update store
- answerBoi.setText("Correct!"); // Display that the user was correct
- answerBoi.setForeground(Color.green); // Changes text color to green so that the word Correct! is green (Fancy)
- if (indexOfQuestions != 9) // if the user is not on the last question
- {
- indexOfQuestions++; // Increments question index
- questionBoi.setText(questionBank[indexOfQuestions].displayQuestion()); // updates to the new question
- userAnswer.setText("Input Answer Here"); // Resets the userAnswer Text box
- }
- else // if index of questions = 9
- {
- start = false; // changes start to false, ending the game
- }
- }
- else // if the user is incorrect
- {
- answerBoi.setText("Incorrect! - Correct Answer: " + questionBank[indexOfQuestions].displayAnswer() + " | Your Answer:" + playerAnswer); // Tells user they're incorrect and displays the correct answer
- answerBoi.setForeground(Color.red); // Changes the above label to red (fancy)
- indexOfQuestions++; //increments question index
- questionBoi.setText(questionBank[indexOfQuestions].displayQuestion()); // displays new question
- userAnswer.setText("Input Answer Here"); // resets userAnswer text box
- }
- }
- else if (!start && (indexOfQuestions != 0)) // if the game is over
- {
- answerBoi.setText("Trivia game over! Your score: " + score + " close the and rerun to go again"); // Displays final score
- answerBoi.setForeground(Color.blue); // changes above text to blue
- }
- }
- }
- private static Question[] setupQuestions(Question[] questions) // Sets up the questions from a text document
- {
- int i = 0; // Counter Integer
- String dummyString; // String used to store input from text document
- // Tries This
- try
- {
- FileReader reader = new FileReader("questions.txt"); // Sets up file reader
- BufferedReader buffReader = new BufferedReader(reader); // Sets up buffer reader
- /**
- * note: This reads in the question first and answer second, a modified text file may that is not
- * in this format will produce some funky outputs / outputting answers and expecting a question as an answer
- * Or the score value (which is supposed to be an integer) might crash the whole program if something is out of place
- */
- // this loop inputs data from file into question object while there is data in the file or integer i is less than 10
- while (((dummyString = buffReader.readLine()) != null) || (i < 10))
- {
- questions[i].setQuest(dummyString); // Sets question to the most recent line read from the file
- dummyString = buffReader.readLine(); // Reads the next line (the answer to the previous line)
- questions[i].setAnswer(dummyString); // Sets Answer to the most recent line read from the file
- dummyString = buffReader.readLine(); // Reads the next line (the score value for the previous answer / question pair )
- questions[i].setPointVal(Integer.parseInt(dummyString)); // Sets the point value to the most recent line read from the file
- i++;
- }
- }
- // If the file is not found
- catch(FileNotFoundException ex)
- {
- System.out.println("File not found!");
- }
- // If the file is broken
- catch(IOException ex)
- {
- System.out.println("File broken!");
- }
- return questions; // returns questions array to the main after setting up the array of questions
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement