Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.border.EmptyBorder;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.util.ArrayList;
- final class Gui extends JFrame {
- static String message;
- static String answer;
- private String alertMessage;
- private String guesses;
- private Display display;
- private JTextArea textArea;
- private JLabel questionLabel;
- private JButton startButton;
- private JButton submitButton;
- private JButton sendButton;
- private JButton quitButton;
- private JButton pauseButton;
- private JButton previousButton;
- private JButton nextButton;
- private int badGuesses;
- private int totalGuesses = 3;
- private boolean gameOver;
- private Timer timer;
- private ArrayList<JButton> alphabetButtons = new ArrayList<>();
- Gui() {
- createWindow();
- }
- public enum GuiText {
- START("Start"),
- SEND("Send"),
- QUIT("Quit"),
- NEXT(">"),
- PREVIOUS("<"),
- SUBMIT("Submit"),
- RESET("Reset"),
- PAUSE("Pause");
- private String guiText;
- GuiText(String guiText) {
- this.guiText = guiText;
- }
- @Override
- public String toString() {
- return guiText;
- }
- }
- /**
- * This class defines the panel that occupies the large central area in the
- * main panel. The paintComponent() method in this class is responsible for
- * drawing the content of that panel. It shows everything that that the user
- * is supposed to see, based on the current values of all the instance variables.
- */
- private class Display extends JPanel {
- Display() {
- setPreferredSize(new Dimension(1024, 768));
- setBackground(new Color(0x00bcda));
- setFont(new Font("Serif", Font.BOLD, 20));
- }
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- ((Graphics2D) g).setStroke(new BasicStroke(3));
- if (message != null) {
- g.setColor(Color.RED);
- g.drawString(message, 30, 40);
- }
- if (alertMessage != null) {
- g.setColor(Color.RED);
- g.drawString(alertMessage, 30, 70);
- }
- if (gameOver) {
- alertMessage = "Click on \"Next\" to play again.";
- }
- g.setColor(Color.DARK_GRAY);
- if (answer != null) {
- for (int i = 0; i < answer.length(); i++) {
- g.drawLine(10 + i * 50, 400, 50 + i * 50, 400);
- if (guesses.indexOf(answer.charAt(i)) >= 0) {
- g.drawString(String.valueOf(answer.charAt(i)), 25 + i * 50, 395);
- }
- }
- }
- }
- }
- /**
- * The constructor that creates the main panel, which is represented
- * by this class. It makes all the buttons and subpanels and adds
- * them to the main panel.
- */
- private void createWindow() {
- setJMenuBar(menuBarCreator());
- // The ActionListener that will respond to button clicks.
- ButtonHandler buttonHandler = new ButtonHandler();
- // Create the subpanels and add them to the main panel.
- display = new Display();
- setLayout(new BorderLayout(0, 0));
- add(display, BorderLayout.CENTER);
- // Add left panel
- JPanel leftPanel = new JPanel(new GridBagLayout());
- GridBagConstraints c = new GridBagConstraints();
- leftPanel.setBorder(new EmptyBorder(25, 25, 25, 5));
- leftPanel.setBackground(new Color(0x00bcda));
- add(leftPanel, BorderLayout.WEST);
- /// Add scoreboard panel
- JPanel scoreboardPanel = new JPanel();
- scoreboardPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
- scoreboardPanel.setBackground(new Color(0x757575));
- c.gridx = 0;
- c.fill = GridBagConstraints.VERTICAL;
- c.weighty = 0.5;
- c.insets = new Insets(0,0,0,20);
- c.gridy = 0;
- leftPanel.add(scoreboardPanel, c);
- // Add scoreboard
- JLabel redLabel = new JLabel("Scoreboard");
- redLabel.setForeground(Color.white);
- redLabel.setFont(new Font("Serif", Font.BOLD, 30));
- scoreboardPanel.add(redLabel);
- // Add previous button
- previousButton = new JButton(String.valueOf(GuiText.PREVIOUS));
- previousButton.setFont(new Font("Arial", Font.PLAIN, 60));
- c.gridx = 1;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.weighty = 0;
- c.gridy = 0;
- leftPanel.add(previousButton, c);
- // Add right panel
- JPanel rightPanel = new JPanel(new GridBagLayout());
- c = new GridBagConstraints();
- c.fill = GridBagConstraints.VERTICAL;
- rightPanel.setBorder(new EmptyBorder(25, 25, 25, 25));
- rightPanel.setBackground(new Color(0x00bcda));
- add(rightPanel, BorderLayout.EAST);
- // Add next button
- nextButton = new JButton(String.valueOf(GuiText.NEXT));
- nextButton.setFont(new Font("Arial", Font.PLAIN, 60));
- nextButton.addActionListener(buttonHandler);
- c.gridx = 1;
- c.gridy = 0;
- rightPanel.add(nextButton, c);
- // Add center panel
- JPanel centerPanel = new JPanel(new GridBagLayout());
- c = new GridBagConstraints();
- centerPanel.setBackground(new Color(0xffffff));
- add(centerPanel, BorderLayout.CENTER);
- // Add timer panel
- JPanel timerPanel = new JPanel();
- timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
- timerPanel.setBackground(new Color(0x757575));
- c.gridx = 0;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.insets = new Insets(0,0,0,20);
- c.gridy = 0;
- centerPanel.add(timerPanel, c);
- // Add timer label
- JLabel timerLabel = new JLabel("00:00", SwingConstants.RIGHT);
- timerLabel.setForeground(Color.black);
- timerLabel.setFont(new Font("Serif", Font.BOLD, 30));
- timerLabel.setHorizontalAlignment(JLabel.RIGHT);
- c.gridx = 1;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridy = 0;
- timerLabel.setForeground(Color.white);
- timerPanel.add(timerLabel, c);
- // Add question panel
- JPanel questionPanel = new JPanel();
- questionPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
- questionPanel.setBackground(new Color(0xcc4b37));
- c.gridx = 0;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridy = 1;
- centerPanel.add(questionPanel, c);
- // Add question label
- questionLabel = new JLabel("Welcome to Guess Game");
- questionLabel.setForeground(Color.black);
- questionLabel.setFont(new Font("Arial", Font.BOLD, 20));
- c.gridx = 0;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridy = 0;
- questionLabel.setForeground(Color.white);
- questionPanel.add(questionLabel, c);
- // Add answer panel
- JPanel answerPanel = new JPanel();
- answerPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
- answerPanel.setBackground(new Color(0xa49e51));
- c.gridx = 0;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridy = 2;
- centerPanel.add(answerPanel, c);
- // Add answer label
- JLabel answerLabel = new JLabel("Press Start To Begin");
- answerLabel.setForeground(Color.black);
- answerLabel.setFont(new Font("Arial", Font.BOLD, 20));
- c.gridx = 0;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridy = 0;
- answerLabel.setForeground(Color.white);
- answerPanel.add(answerLabel, c);
- // Add primary button panel
- JPanel primaryButtonPanel = new JPanel();
- primaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
- primaryButtonPanel.setBackground(new Color(0xff8d8d));
- c.gridx = 0;
- c.gridy = 3;
- c.fill = GridBagConstraints.HORIZONTAL;
- centerPanel.add(primaryButtonPanel, c);
- // Add text area
- textArea = new JTextArea(1, 10);
- c.gridx = 0;
- c.gridy = 0;
- c.fill = GridBagConstraints.HORIZONTAL;
- textArea.setFont(new Font("Arial", Font.PLAIN, 24));
- primaryButtonPanel.add(textArea, c);
- // Add buttons
- sendButton = new JButton(String.valueOf(GuiText.SEND));
- sendButton.addActionListener(buttonHandler);
- sendButton.setFont(new Font("Arial", Font.PLAIN, 24));
- primaryButtonPanel.add(sendButton);
- // Add secondary button panel
- JPanel secondaryButtonPanel = new JPanel();
- secondaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
- secondaryButtonPanel.setBackground(new Color(0xffcc82));
- c.gridx = 0;
- c.gridy = 4;
- c.fill = GridBagConstraints.HORIZONTAL;
- centerPanel.add(secondaryButtonPanel, c);
- // Add secondary buttons
- startButton = new JButton(GuiText.START.toString());
- startButton.addActionListener(buttonHandler);
- startButton.setFont(new Font("Arial", Font.PLAIN, 24));
- secondaryButtonPanel.add(startButton);
- pauseButton = new JButton(GuiText.PAUSE.toString());
- pauseButton.setFont(new Font("Arial", Font.PLAIN, 24));
- pauseButton.addActionListener(buttonHandler);
- secondaryButtonPanel.add(pauseButton);
- quitButton = new JButton(GuiText.QUIT.toString());
- quitButton.setFont(new Font("Arial", Font.PLAIN, 24));
- quitButton.addActionListener(buttonHandler);
- secondaryButtonPanel.add(quitButton);
- submitButton = new JButton(GuiText.SUBMIT.toString());
- submitButton.setFont(new Font("Arial", Font.PLAIN, 24));
- submitButton.addActionListener(buttonHandler);
- secondaryButtonPanel.add(submitButton);
- // Add keyboard panel
- JPanel keyboardPanel = new JPanel();
- keyboardPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
- keyboardPanel.setBackground(new Color(0x5302d0));
- c.gridx = 0;
- c.gridy = 5;
- c.fill = GridBagConstraints.HORIZONTAL;
- centerPanel.add(keyboardPanel, c);
- keyboardPanel.setLayout(new GridLayout(3, 10, 5, 5));
- for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
- JButton button = new JButton(String.valueOf(alphabet));
- button.addActionListener(buttonHandler);
- button.setFont(new Font("Arial", Font.PLAIN, 20));
- keyboardPanel.add(button);
- alphabetButtons.add(button);
- }
- // Disable buttons before game starts
- startButton.setEnabled(true);
- nextButton.setEnabled(false);
- previousButton.setEnabled(false);
- sendButton.setEnabled(false);
- submitButton.setEnabled(false);
- pauseButton.setEnabled(false);
- for (JButton alphabetButton : alphabetButtons) {
- alphabetButton.setEnabled(false);
- }
- }
- private boolean wordIsComplete() {
- for (int i = 0; i < answer.length(); i++) {
- char ch = answer.charAt(i);
- if (this.guesses.indexOf(ch) == -1) {
- return false;
- }
- }
- return true;
- }
- private JMenuBar menuBarCreator() {
- // Create the menu bar
- JMenuBar menuBar = new JMenuBar();
- JMenu menuFile = new JMenu("File");
- JMenu menuHelp = new JMenu("Help");
- JMenuItem menuFileExit = new JMenuItem("Exit");
- JMenuItem menuHelpAbout = new JMenuItem("About");
- JMenuItem menuHelpRules = new JMenuItem("Rules");
- // Exit action
- menuFileExit.addActionListener(e -> System.exit(0));
- // Make the shortcuts for the items
- menuFile.setMnemonic(KeyEvent.VK_F);
- menuHelp.setMnemonic(KeyEvent.VK_H);
- // Out the menu parts with each other
- menuBar.add(menuFile);
- menuBar.add(menuHelp);
- menuFile.add(menuFileExit);
- menuHelp.add(menuHelpRules);
- menuHelp.add(menuHelpAbout);
- menuHelp.add(menuHelpRules);
- return menuBar;
- }
- /**
- * This class defines a listener that will respond to the events that occur
- * when the user clicks any of the buttons in the button. The buttons are
- * labeled "Next word", "Give up", "Quit", "A", "B", "C", ..., "Z".
- */
- private class ButtonHandler implements ActionListener {
- @Override
- public void actionPerformed(java.awt.event.ActionEvent e) {
- // The text from the button that the user clicked.
- String cmd = e.getActionCommand();
- // Respond to Quit button by ending the program.
- if (cmd.equals(GuiText.QUIT.toString())) {
- System.exit(0);
- } else if (cmd.equals(GuiText.START.toString())) {
- startGame();
- } else if (cmd.equals(GuiText.PREVIOUS.toString())) {
- QuestionList.selectRandomQuestion();
- questionLabel.setText(message);
- } else if (cmd.equals(GuiText.NEXT.toString())) {
- QuestionList.selectRandomQuestion();
- questionLabel.setText(message);
- } else if (cmd.equals(GuiText.SUBMIT.toString())) {
- processQuestionResponse(textArea.getText());
- } else if (cmd.equals(GuiText.RESET.toString())) {
- textArea.setText("");
- } else {
- textArea.setText(cmd);
- }
- // Causes the display to be redrawn, to show any changes made.
- display.repaint();
- }
- }
- private void startGame() {
- this.guesses = "";
- this.badGuesses = 0;
- startButton.setEnabled(false);
- nextButton.setEnabled(true);
- previousButton.setEnabled(true);
- sendButton.setEnabled(true);
- submitButton.setEnabled(true);
- pauseButton.setEnabled(true);
- for (JButton alphabetButton : alphabetButtons) {
- alphabetButton.setEnabled(true);
- }
- QuestionList.loadQuestions();
- QuestionList.selectRandomQuestion();
- questionLabel.setText(message);
- }
- private void processQuestionResponse(String response) {
- char guess = response.toUpperCase().charAt(0);
- guesses = String.valueOf(guesses) + guess;
- if (wordIsComplete()) {
- alertMessage = "Correct answer! Click on \"Next\" to play again.";
- nextButton.setEnabled(true);
- for (JButton b : alphabetButtons)
- b.setEnabled(false);
- gameOver = true;
- } else if (answer.indexOf(guess) >= 0) {
- alertMessage = String.format("Yes, %s is in the word. Pick your next letter.", guess);
- } else {
- badGuesses = badGuesses + 1;
- if (badGuesses == totalGuesses) {
- alertMessage = String.format("Sorry, you're ran out of guesses! The answer was '%s'.", answer);
- nextButton.setEnabled(true);
- for (JButton b : alphabetButtons)
- b.setEnabled(false);
- gameOver = true;
- } else {
- alertMessage = String.format("Sorry, %s is not in the word. Pick your next letter.", guess);
- }
- }
- display.repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement