Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.42 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.border.EmptyBorder;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.util.ArrayList;
  8.  
  9. final class Gui extends JFrame {
  10.   static String message;
  11.   static String answer;
  12.   private String alertMessage;
  13.   private String guesses;
  14.   private Display display;
  15.   private JTextArea textArea;
  16.   private JButton startButton;
  17.   private JButton submitButton;
  18.   private JLabel timerLabel;
  19.   private JButton pauseButton;
  20.   private JButton quitButton;
  21.   private JButton previousButton;
  22.   private JButton sendButton;
  23.   private JButton nextButton;
  24.   private int badGuesses;
  25.   private boolean gameOver;
  26.   private Timer timer;
  27.   private ArrayList<JButton> alphabetButtons = new ArrayList<>();
  28.  
  29.   Gui() {
  30.     createWindow();
  31.   }
  32.  
  33.   public enum GuiText {
  34.     START("Start"),
  35.     QUIT("Quit"),
  36.     SKIP("Skip"),
  37.     SUBMIT("Submit"),
  38.     RESET("Reset"),
  39.     SEND("Send"),
  40.     NEXT(">"),
  41.     PREVIOUS("<"),
  42.     PAUSE("Pause");
  43.  
  44.     private String guiText;
  45.  
  46.     GuiText(String guiText) {
  47.       this.guiText = guiText;
  48.     }
  49.  
  50.     @Override
  51.     public String toString() {
  52.       return guiText;
  53.     }
  54.   }
  55.  
  56.   /**
  57.    * This class defines the panel that occupies the large central area in the
  58.    * main panel.  The paintComponent() method in this class is responsible for
  59.    * drawing the content of that panel.  It shows everything that that the user
  60.    * is supposed to see, based on the current values of all the instance variables.
  61.    */
  62.   private class Display extends JPanel {
  63.     Display() {
  64.       setPreferredSize(new Dimension(1000, 250));
  65.       setBackground(new Color(0x00bcda));
  66.       setFont(new Font("Tahoma", Font.BOLD, 20));
  67.     }
  68.  
  69.     protected void paintComponent(Graphics g) {
  70.       super.paintComponent(g);
  71.       ((Graphics2D) g).setStroke(new BasicStroke(3));
  72.       if (message != null) {
  73.         g.setColor(Color.DARK_GRAY);
  74.         g.drawString(message, 30, 120);
  75.       }
  76.       if (alertMessage != null) {
  77.         g.setColor(Color.DARK_GRAY);
  78.         g.drawString(alertMessage, 30, 150);
  79.       }
  80.       if (gameOver) {
  81.         alertMessage = "Click on \"Next\" to play again.";
  82.       } else {
  83.         g.drawString("Guesses remaining:  " + (3 - badGuesses), 770, 40);
  84.       }
  85.       g.setColor(Color.DARK_GRAY);
  86.       if (answer != null) {
  87.         for (int i = 0; i < answer.length(); i++) {
  88.           if (String.valueOf(answer.charAt(i)).trim().length() > 0) {
  89.             g.drawLine(30 + i * 70, 210, 70 + i * 70, 210);
  90.             if (guesses.indexOf(answer.charAt(i)) >= 0) {
  91.               g.drawString(String.valueOf(answer.charAt(i)), 45 + i * 70, 195);
  92.             }
  93.           }
  94.         }
  95.       }
  96.     }
  97.   }
  98.  
  99.   /**
  100.    * The constructor that creates the main panel, which is represented
  101.    * by this class.  It makes all the buttons and subpanels and adds
  102.    * them to the main panel.
  103.    */
  104.   private void createWindow() {
  105.     setJMenuBar(menuBarCreator());
  106.  
  107.     // The ActionListener that will respond to button clicks.
  108.     ButtonHandler buttonHandler = new ButtonHandler();
  109.  
  110.     // Create the subpanels and add them to the main panel.
  111.     display = new Display();
  112.     setLayout(new BorderLayout(3, 3));
  113.     add(display, BorderLayout.CENTER);
  114.  
  115.     // Add timer panel
  116.     JPanel timerPanel = new JPanel();
  117.     timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
  118.     timerPanel.setBackground(new Color(0x00bcda));
  119.     add(timerPanel, BorderLayout.PAGE_START);
  120.  
  121.     // Add timer label
  122.     timerLabel = new JLabel("01:00", SwingConstants.RIGHT);
  123.     timerLabel.setFont(new Font("Arial", Font.BOLD, 20));
  124.     timerLabel.setHorizontalAlignment(JLabel.RIGHT);
  125.     GridBagConstraints c = new GridBagConstraints();
  126.     c.gridx = 1;
  127.     c.fill = GridBagConstraints.HORIZONTAL;
  128.     c.gridy = 0;
  129.     timerLabel.setForeground(Color.black);
  130.     timerPanel.add(timerLabel, c);
  131.  
  132.     // Add left panel
  133.     JPanel leftPanel = new JPanel(new GridBagLayout());
  134.     c = new GridBagConstraints();
  135.     leftPanel.setBorder(new EmptyBorder(25, 25, 25, 5));
  136.     leftPanel.setBackground(new Color(0x00bcda));
  137.     add(leftPanel, BorderLayout.WEST);
  138.  
  139.     // Add previous button
  140.     previousButton = new JButton(String.valueOf(GuiText.PREVIOUS));
  141.     previousButton.setFont(new Font("Arial", Font.PLAIN, 60));
  142.     previousButton.addActionListener(buttonHandler);
  143.     c.gridx = 1;
  144.     c.fill = GridBagConstraints.HORIZONTAL;
  145.     c.weighty = 0;
  146.     c.gridy = 0;
  147.     leftPanel.add(previousButton, c);
  148.  
  149.     // Add right panel
  150.     JPanel rightPanel = new JPanel(new GridBagLayout());
  151.     c = new GridBagConstraints();
  152.     c.fill = GridBagConstraints.VERTICAL;
  153.     rightPanel.setBorder(new EmptyBorder(25, 25, 25, 25));
  154.     rightPanel.setBackground(new Color(0x00bcda));
  155.     add(rightPanel, BorderLayout.EAST);
  156.  
  157.     // Add next button
  158.     nextButton = new JButton(String.valueOf(GuiText.NEXT));
  159.     nextButton.setFont(new Font("Arial", Font.PLAIN, 60));
  160.     nextButton.addActionListener(buttonHandler);
  161.     c.gridx = 1;
  162.     c.gridy = 0;
  163.     rightPanel.add(nextButton, c);
  164.  
  165.     // Add actual timer
  166.     initialiseTimer();
  167.  
  168.     // Add bottom panel
  169.     JPanel bottomPanel = new JPanel(new GridBagLayout());
  170.     GridBagConstraints bottomPanelConstraints = new GridBagConstraints();
  171.     bottomPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
  172.     add(bottomPanel, BorderLayout.PAGE_END);
  173.     setBackground(new Color(100, 0, 0));
  174.  
  175.     // Add primary button panel to bottom panel
  176.     JPanel primaryButtonPanel = new JPanel();
  177.     primaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
  178.     primaryButtonPanel.setBackground(new Color(0xFFFFFF));
  179.     c.gridx = 0;
  180.     c.gridy = 3;
  181.     c.fill = GridBagConstraints.HORIZONTAL;
  182.     bottomPanel.add(primaryButtonPanel, c);
  183.  
  184.     // Add text area
  185.     textArea = new JTextArea(1, 10);
  186.     c.gridx = 0;
  187.     c.gridy = 0;
  188.     c.fill = GridBagConstraints.HORIZONTAL;
  189.     textArea.setFont(new Font("Arial", Font.PLAIN, 24));
  190.     textArea.setBackground(new Color(0xCCCCCC));
  191.     textArea.setEditable(false);
  192.     primaryButtonPanel.add(textArea, c);
  193.  
  194.     // Add buttons
  195.     sendButton = new JButton(String.valueOf(GuiText.SEND));
  196.     sendButton.addActionListener(buttonHandler);
  197.     sendButton.setFont(new Font("Arial", Font.PLAIN, 24));
  198.     primaryButtonPanel.add(sendButton);
  199.  
  200.     // Add secondary button panel
  201.     JPanel secondaryButtonPanel = new JPanel();
  202.     secondaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
  203.     secondaryButtonPanel.setBackground(new Color(0xFFFFFF));
  204.     c.gridx = 0;
  205.     c.gridy = 4;
  206.     c.fill = GridBagConstraints.HORIZONTAL;
  207.     bottomPanel.add(secondaryButtonPanel, c);
  208.  
  209.     // Add secondary buttons
  210.     startButton = new JButton(GuiText.START.toString());
  211.     startButton.addActionListener(buttonHandler);
  212.     startButton.setFont(new Font("Arial", Font.PLAIN, 24));
  213.     secondaryButtonPanel.add(startButton);
  214.  
  215.     pauseButton = new JButton(GuiText.PAUSE.toString());
  216.     pauseButton.setFont(new Font("Arial", Font.PLAIN, 24));
  217.     pauseButton.addActionListener(buttonHandler);
  218.     secondaryButtonPanel.add(pauseButton);
  219.  
  220.     quitButton = new JButton(GuiText.QUIT.toString());
  221.     quitButton.setFont(new Font("Arial", Font.PLAIN, 24));
  222.     quitButton.addActionListener(buttonHandler);
  223.     secondaryButtonPanel.add(quitButton);
  224.  
  225.     submitButton = new JButton(GuiText.SUBMIT.toString());
  226.     submitButton.setFont(new Font("Arial", Font.PLAIN, 24));
  227.     submitButton.addActionListener(buttonHandler);
  228.     secondaryButtonPanel.add(submitButton);
  229.  
  230.     // Add keyboard panel
  231.     JPanel keyboardPanel = new JPanel();
  232.     keyboardPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
  233.     keyboardPanel.setBackground(new Color(0xFFFFFF));
  234.     c.gridx = 0;
  235.     c.gridy = 5;
  236.     c.fill = GridBagConstraints.HORIZONTAL;
  237.     bottomPanel.add(keyboardPanel, c);
  238.     keyboardPanel.setLayout(new GridLayout(3, 10, 5, 5));
  239.     for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
  240.       JButton button = new JButton(String.valueOf(alphabet));
  241.       button.addActionListener(buttonHandler);
  242.       button.setFont(new Font("Arial", Font.PLAIN, 20));
  243.       keyboardPanel.add(button);
  244.       alphabetButtons.add(button);
  245.     }
  246.  
  247.     disablePrimaryButtons();
  248.   }
  249.  
  250.   private void initialiseTimer() {
  251.     timer = new Timer(1000, new ActionListener() {
  252.       int time = 60;
  253.       @Override
  254.       public void actionPerformed(ActionEvent e) {
  255.         time--;
  256.         timerLabel.setText(format(time / 60) + ":" + format(time % 60));
  257.         if (time == 0) {
  258.           timer = (Timer) e.getSource();
  259.           timer.stop();
  260.         }
  261.       }
  262.     });
  263.   }
  264.  
  265.   private static String format(int i) {
  266.     String result = String.valueOf(i);
  267.     if (result.length() == 1) {
  268.       result = "0" + result;
  269.     }
  270.     return result;
  271.   }
  272.  
  273.   private boolean wordIsComplete() {
  274.     String answerWithoutSpaces = answer.replaceAll(" ", "");
  275.     for (int i = 0; i < answerWithoutSpaces.length(); i++) {
  276.       char ch = answerWithoutSpaces.charAt(i);
  277.       if (this.guesses.indexOf(ch) == -1) {
  278.         return false;
  279.       }
  280.     }
  281.     return true;
  282.   }
  283.  
  284.   private void disablePrimaryButtons() {
  285.     startButton.setEnabled(true);
  286.   }
  287.  
  288.   private JMenuBar menuBarCreator() {
  289.     // Create the menu bar
  290.     JMenuBar menuBar = new JMenuBar();
  291.     JMenu menuFile = new JMenu("File");
  292.     JMenu menuHelp = new JMenu("Help");
  293.  
  294.     JMenuItem menuFileExit = new JMenuItem("Exit");
  295.     JMenuItem menuHelpAbout = new JMenuItem("About");
  296.     JMenuItem menuHelpRules = new JMenuItem("Rules");
  297.  
  298.     // Exit action
  299.     menuFileExit.addActionListener(e -> System.exit(0));
  300.  
  301.     // Make the shortcuts for the items
  302.     menuFile.setMnemonic(KeyEvent.VK_F);
  303.     menuHelp.setMnemonic(KeyEvent.VK_H);
  304.  
  305.     // Out the menu parts with each other
  306.     menuBar.add(menuFile);
  307.     menuBar.add(menuHelp);
  308.     menuFile.add(menuFileExit);
  309.     menuHelp.add(menuHelpRules);
  310.     menuHelp.add(menuHelpAbout);
  311.     menuHelp.add(menuHelpRules);
  312.  
  313.     return menuBar;
  314.   }
  315.  
  316.   /**
  317.    * This class defines a listener that will respond to the events that occur
  318.    * when the user clicks any of the buttons in the button.  The buttons are
  319.    * labeled "Next word", "Give up", "Quit", "A", "B", "C", ..., "Z".
  320.    */
  321.   private class ButtonHandler implements ActionListener {
  322.     @Override
  323.     public void actionPerformed(java.awt.event.ActionEvent e) {
  324.       // The text from the button that the user clicked.
  325.       String cmd = e.getActionCommand();
  326.       // Respond to Quit button by ending the program.
  327.       if (cmd.equals(GuiText.QUIT.toString())) {
  328.         System.exit(0);
  329.       } else if (cmd.equals(GuiText.START.toString())) {
  330.         timer.start();
  331.         startGame();
  332.       } else if (cmd.equals(GuiText.NEXT.toString())) {
  333.         timer.stop();
  334.         initialiseTimer();
  335.         timer.start();
  336.         alertMessage = "";
  337.         guesses = "";
  338.         QuestionList.selectRandomQuestion();
  339.       } else if (cmd.equals(GuiText.SUBMIT.toString())) {
  340.         processQuestionResponse(textArea.getText());
  341.       } else if (cmd.equals(GuiText.RESET.toString())) {
  342.         textArea.setText("");
  343.       } else {
  344.         textArea.setText(cmd);
  345.       }
  346.       // Causes the display to be redrawn, to show any changes made.
  347.       display.repaint();
  348.     }
  349.   }
  350.  
  351.   private void startGame() {
  352.     this.guesses = "";
  353.     this.badGuesses = 0;
  354.     QuestionList.loadQuestions();
  355.     QuestionList.selectRandomQuestion();
  356.   }
  357.  
  358.   private void processQuestionResponse(String response) {
  359.     char guess = response.toUpperCase().charAt(0);
  360.     guesses = String.valueOf(guesses) + guess;
  361.     if (wordIsComplete()) {
  362.       alertMessage = "Correct answer! Click on \"Next\" to play again.";
  363.       nextButton.setEnabled(true);
  364.       for (JButton b : alphabetButtons)
  365.         b.setEnabled(false);
  366.     } else if (answer.indexOf(guess) >= 0) {
  367.       alertMessage = String.format("Yes, %s is in the word. Pick your next letter.", guess);
  368.     } else {
  369.       badGuesses = badGuesses + 1;
  370.       if (badGuesses == 3) {
  371.         alertMessage = String.format("Sorry, you're hung! The answer was '%s'", answer);
  372.         nextButton.setEnabled(true);
  373.         for (JButton b : alphabetButtons)
  374.           b.setEnabled(false);
  375.         gameOver = true;
  376.       } else {
  377.         alertMessage = String.format("Sorry, %s is not in the word. Pick your next letter.", guess);
  378.       }
  379.     }
  380.     display.repaint();
  381.   }
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement