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 JButton startButton;
- private JButton submitButton;
- private JLabel timerLabel;
- private JButton pauseButton;
- private JButton quitButton;
- private JButton previousButton;
- private JButton sendButton;
- private JButton nextButton;
- private int badGuesses;
- private boolean gameOver;
- private Timer timer;
- private ArrayList<JButton> alphabetButtons = new ArrayList<>();
- Gui() {
- createWindow();
- }
- public enum GuiText {
- START("Start"),
- QUIT("Quit"),
- SKIP("Skip"),
- SUBMIT("Submit"),
- RESET("Reset"),
- SEND("Send"),
- NEXT(">"),
- PREVIOUS("<"),
- 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(1000, 250));
- setBackground(new Color(0x00bcda));
- setFont(new Font("Tahoma", Font.BOLD, 20));
- }
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- ((Graphics2D) g).setStroke(new BasicStroke(3));
- if (message != null) {
- g.setColor(Color.DARK_GRAY);
- g.drawString(message, 30, 120);
- }
- if (alertMessage != null) {
- g.setColor(Color.DARK_GRAY);
- g.drawString(alertMessage, 30, 150);
- }
- if (gameOver) {
- alertMessage = "Click on \"Next\" to play again.";
- } else {
- g.drawString("Guesses remaining: " + (3 - badGuesses), 770, 40);
- }
- g.setColor(Color.DARK_GRAY);
- if (answer != null) {
- for (int i = 0; i < answer.length(); i++) {
- if (String.valueOf(answer.charAt(i)).trim().length() > 0) {
- g.drawLine(30 + i * 70, 210, 70 + i * 70, 210);
- if (guesses.indexOf(answer.charAt(i)) >= 0) {
- g.drawString(String.valueOf(answer.charAt(i)), 45 + i * 70, 195);
- }
- }
- }
- }
- }
- }
- /**
- * 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(3, 3));
- add(display, BorderLayout.CENTER);
- // Add timer panel
- JPanel timerPanel = new JPanel();
- timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
- timerPanel.setBackground(new Color(0x00bcda));
- add(timerPanel, BorderLayout.PAGE_START);
- // Add timer label
- timerLabel = new JLabel("01:00", SwingConstants.RIGHT);
- timerLabel.setFont(new Font("Arial", Font.BOLD, 20));
- timerLabel.setHorizontalAlignment(JLabel.RIGHT);
- GridBagConstraints c = new GridBagConstraints();
- c.gridx = 1;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridy = 0;
- timerLabel.setForeground(Color.black);
- timerPanel.add(timerLabel, c);
- // Add left panel
- JPanel leftPanel = new JPanel(new GridBagLayout());
- c = new GridBagConstraints();
- leftPanel.setBorder(new EmptyBorder(25, 25, 25, 5));
- leftPanel.setBackground(new Color(0x00bcda));
- add(leftPanel, BorderLayout.WEST);
- // Add previous button
- previousButton = new JButton(String.valueOf(GuiText.PREVIOUS));
- previousButton.setFont(new Font("Arial", Font.PLAIN, 60));
- previousButton.addActionListener(buttonHandler);
- 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 actual timer
- initialiseTimer();
- // Add bottom panel
- JPanel bottomPanel = new JPanel(new GridBagLayout());
- GridBagConstraints bottomPanelConstraints = new GridBagConstraints();
- bottomPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
- add(bottomPanel, BorderLayout.PAGE_END);
- setBackground(new Color(100, 0, 0));
- // Add primary button panel to bottom panel
- JPanel primaryButtonPanel = new JPanel();
- primaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
- primaryButtonPanel.setBackground(new Color(0xFFFFFF));
- c.gridx = 0;
- c.gridy = 3;
- c.fill = GridBagConstraints.HORIZONTAL;
- bottomPanel.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));
- textArea.setBackground(new Color(0xCCCCCC));
- textArea.setEditable(false);
- 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(0xFFFFFF));
- c.gridx = 0;
- c.gridy = 4;
- c.fill = GridBagConstraints.HORIZONTAL;
- bottomPanel.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(0xFFFFFF));
- c.gridx = 0;
- c.gridy = 5;
- c.fill = GridBagConstraints.HORIZONTAL;
- bottomPanel.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);
- }
- disablePrimaryButtons();
- }
- private void initialiseTimer() {
- timer = new Timer(1000, new ActionListener() {
- int time = 60;
- @Override
- public void actionPerformed(ActionEvent e) {
- time--;
- timerLabel.setText(format(time / 60) + ":" + format(time % 60));
- if (time == 0) {
- timer = (Timer) e.getSource();
- timer.stop();
- }
- }
- });
- }
- private static String format(int i) {
- String result = String.valueOf(i);
- if (result.length() == 1) {
- result = "0" + result;
- }
- return result;
- }
- private boolean wordIsComplete() {
- String answerWithoutSpaces = answer.replaceAll(" ", "");
- for (int i = 0; i < answerWithoutSpaces.length(); i++) {
- char ch = answerWithoutSpaces.charAt(i);
- if (this.guesses.indexOf(ch) == -1) {
- return false;
- }
- }
- return true;
- }
- private void disablePrimaryButtons() {
- startButton.setEnabled(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())) {
- timer.start();
- startGame();
- } else if (cmd.equals(GuiText.NEXT.toString())) {
- timer.stop();
- initialiseTimer();
- timer.start();
- alertMessage = "";
- guesses = "";
- QuestionList.selectRandomQuestion();
- } 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;
- QuestionList.loadQuestions();
- QuestionList.selectRandomQuestion();
- }
- 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);
- } 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 == 3) {
- alertMessage = String.format("Sorry, you're hung! 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