Advertisement
Guest User

Untitled

a guest
Oct 27th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.98 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 JLabel questionLabel;
  17. private JButton startButton;
  18. private JButton submitButton;
  19. private JButton sendButton;
  20. private JButton quitButton;
  21. private JButton pauseButton;
  22. private JButton previousButton;
  23. private JButton nextButton;
  24. private int badGuesses;
  25. private int totalGuesses = 3;
  26. private boolean gameOver;
  27. private Timer timer;
  28. private ArrayList<JButton> alphabetButtons = new ArrayList<>();
  29.  
  30. Gui() {
  31. createWindow();
  32. }
  33.  
  34. public enum GuiText {
  35. START("Start"),
  36. SEND("Send"),
  37. QUIT("Quit"),
  38. NEXT(">"),
  39. PREVIOUS("<"),
  40. SUBMIT("Submit"),
  41. RESET("Reset"),
  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(1024, 768));
  65. setBackground(new Color(0x00bcda));
  66. setFont(new Font("Serif", 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.RED);
  74. g.drawString(message, 30, 40);
  75. }
  76. if (alertMessage != null) {
  77. g.setColor(Color.RED);
  78. g.drawString(alertMessage, 30, 70);
  79. }
  80. if (gameOver) {
  81. alertMessage = "Click on \"Next\" to play again.";
  82. }
  83. g.setColor(Color.DARK_GRAY);
  84. if (answer != null) {
  85. for (int i = 0; i < answer.length(); i++) {
  86. g.drawLine(10 + i * 50, 400, 50 + i * 50, 400);
  87. if (guesses.indexOf(answer.charAt(i)) >= 0) {
  88. g.drawString(String.valueOf(answer.charAt(i)), 25 + i * 50, 395);
  89. }
  90. }
  91. }
  92. }
  93. }
  94.  
  95. /**
  96. * The constructor that creates the main panel, which is represented
  97. * by this class. It makes all the buttons and subpanels and adds
  98. * them to the main panel.
  99. */
  100. private void createWindow() {
  101. setJMenuBar(menuBarCreator());
  102.  
  103. // The ActionListener that will respond to button clicks.
  104. ButtonHandler buttonHandler = new ButtonHandler();
  105.  
  106. // Create the subpanels and add them to the main panel.
  107. display = new Display();
  108. setLayout(new BorderLayout(0, 0));
  109. add(display, BorderLayout.CENTER);
  110.  
  111. // Add left panel
  112. JPanel leftPanel = new JPanel(new GridBagLayout());
  113. GridBagConstraints c = new GridBagConstraints();
  114. leftPanel.setBorder(new EmptyBorder(25, 25, 25, 5));
  115. leftPanel.setBackground(new Color(0x00bcda));
  116. add(leftPanel, BorderLayout.WEST);
  117.  
  118. /// Add scoreboard panel
  119. JPanel scoreboardPanel = new JPanel();
  120. scoreboardPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
  121. scoreboardPanel.setBackground(new Color(0x757575));
  122. c.gridx = 0;
  123. c.fill = GridBagConstraints.VERTICAL;
  124. c.weighty = 0.5;
  125. c.insets = new Insets(0,0,0,20);
  126. c.gridy = 0;
  127. leftPanel.add(scoreboardPanel, c);
  128.  
  129. // Add scoreboard
  130. JLabel redLabel = new JLabel("Scoreboard");
  131. redLabel.setForeground(Color.white);
  132. redLabel.setFont(new Font("Serif", Font.BOLD, 30));
  133. scoreboardPanel.add(redLabel);
  134.  
  135. // Add previous button
  136. previousButton = new JButton(String.valueOf(GuiText.PREVIOUS));
  137. previousButton.setFont(new Font("Arial", Font.PLAIN, 60));
  138. c.gridx = 1;
  139. c.fill = GridBagConstraints.HORIZONTAL;
  140. c.weighty = 0;
  141. c.gridy = 0;
  142. leftPanel.add(previousButton, c);
  143.  
  144. // Add right panel
  145. JPanel rightPanel = new JPanel(new GridBagLayout());
  146. c = new GridBagConstraints();
  147. c.fill = GridBagConstraints.VERTICAL;
  148. rightPanel.setBorder(new EmptyBorder(25, 25, 25, 25));
  149. rightPanel.setBackground(new Color(0x00bcda));
  150. add(rightPanel, BorderLayout.EAST);
  151.  
  152. // Add next button
  153. nextButton = new JButton(String.valueOf(GuiText.NEXT));
  154. nextButton.setFont(new Font("Arial", Font.PLAIN, 60));
  155. nextButton.addActionListener(buttonHandler);
  156. c.gridx = 1;
  157. c.gridy = 0;
  158. rightPanel.add(nextButton, c);
  159.  
  160. // Add center panel
  161. JPanel centerPanel = new JPanel(new GridBagLayout());
  162. c = new GridBagConstraints();
  163. centerPanel.setBackground(new Color(0xffffff));
  164. add(centerPanel, BorderLayout.CENTER);
  165.  
  166. // Add timer panel
  167. JPanel timerPanel = new JPanel();
  168. timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
  169. timerPanel.setBackground(new Color(0x757575));
  170. c.gridx = 0;
  171. c.fill = GridBagConstraints.HORIZONTAL;
  172. c.insets = new Insets(0,0,0,20);
  173. c.gridy = 0;
  174. centerPanel.add(timerPanel, c);
  175.  
  176. // Add timer label
  177. JLabel timerLabel = new JLabel("00:00", SwingConstants.RIGHT);
  178. timerLabel.setForeground(Color.black);
  179. timerLabel.setFont(new Font("Serif", Font.BOLD, 30));
  180. timerLabel.setHorizontalAlignment(JLabel.RIGHT);
  181. c.gridx = 1;
  182. c.fill = GridBagConstraints.HORIZONTAL;
  183. c.gridy = 0;
  184. timerLabel.setForeground(Color.white);
  185. timerPanel.add(timerLabel, c);
  186.  
  187. // Add question panel
  188. JPanel questionPanel = new JPanel();
  189. questionPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
  190. questionPanel.setBackground(new Color(0xcc4b37));
  191. c.gridx = 0;
  192. c.fill = GridBagConstraints.HORIZONTAL;
  193. c.gridy = 1;
  194. centerPanel.add(questionPanel, c);
  195.  
  196. // Add question label
  197. questionLabel = new JLabel("Welcome to Guess Game");
  198. questionLabel.setForeground(Color.black);
  199. questionLabel.setFont(new Font("Arial", Font.BOLD, 20));
  200. c.gridx = 0;
  201. c.fill = GridBagConstraints.HORIZONTAL;
  202. c.gridy = 0;
  203. questionLabel.setForeground(Color.white);
  204. questionPanel.add(questionLabel, c);
  205.  
  206. // Add answer panel
  207. JPanel answerPanel = new JPanel();
  208. answerPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
  209. answerPanel.setBackground(new Color(0xa49e51));
  210. c.gridx = 0;
  211. c.fill = GridBagConstraints.HORIZONTAL;
  212. c.gridy = 2;
  213. centerPanel.add(answerPanel, c);
  214.  
  215. // Add answer label
  216. JLabel answerLabel = new JLabel("Press Start To Begin");
  217. answerLabel.setForeground(Color.black);
  218. answerLabel.setFont(new Font("Arial", Font.BOLD, 20));
  219. c.gridx = 0;
  220. c.fill = GridBagConstraints.HORIZONTAL;
  221. c.gridy = 0;
  222. answerLabel.setForeground(Color.white);
  223. answerPanel.add(answerLabel, c);
  224.  
  225. // Add primary button panel
  226. JPanel primaryButtonPanel = new JPanel();
  227. primaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
  228. primaryButtonPanel.setBackground(new Color(0xff8d8d));
  229. c.gridx = 0;
  230. c.gridy = 3;
  231. c.fill = GridBagConstraints.HORIZONTAL;
  232. centerPanel.add(primaryButtonPanel, c);
  233.  
  234. // Add text area
  235. textArea = new JTextArea(1, 10);
  236. c.gridx = 0;
  237. c.gridy = 0;
  238. c.fill = GridBagConstraints.HORIZONTAL;
  239. textArea.setFont(new Font("Arial", Font.PLAIN, 24));
  240. primaryButtonPanel.add(textArea, c);
  241.  
  242. // Add buttons
  243. sendButton = new JButton(String.valueOf(GuiText.SEND));
  244. sendButton.addActionListener(buttonHandler);
  245. sendButton.setFont(new Font("Arial", Font.PLAIN, 24));
  246. primaryButtonPanel.add(sendButton);
  247.  
  248. // Add secondary button panel
  249. JPanel secondaryButtonPanel = new JPanel();
  250. secondaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
  251. secondaryButtonPanel.setBackground(new Color(0xffcc82));
  252. c.gridx = 0;
  253. c.gridy = 4;
  254. c.fill = GridBagConstraints.HORIZONTAL;
  255. centerPanel.add(secondaryButtonPanel, c);
  256.  
  257. // Add secondary buttons
  258. startButton = new JButton(GuiText.START.toString());
  259. startButton.addActionListener(buttonHandler);
  260. startButton.setFont(new Font("Arial", Font.PLAIN, 24));
  261. secondaryButtonPanel.add(startButton);
  262.  
  263. pauseButton = new JButton(GuiText.PAUSE.toString());
  264. pauseButton.setFont(new Font("Arial", Font.PLAIN, 24));
  265. pauseButton.addActionListener(buttonHandler);
  266. secondaryButtonPanel.add(pauseButton);
  267.  
  268. quitButton = new JButton(GuiText.QUIT.toString());
  269. quitButton.setFont(new Font("Arial", Font.PLAIN, 24));
  270. quitButton.addActionListener(buttonHandler);
  271. secondaryButtonPanel.add(quitButton);
  272.  
  273. submitButton = new JButton(GuiText.SUBMIT.toString());
  274. submitButton.setFont(new Font("Arial", Font.PLAIN, 24));
  275. submitButton.addActionListener(buttonHandler);
  276. secondaryButtonPanel.add(submitButton);
  277.  
  278. // Add keyboard panel
  279. JPanel keyboardPanel = new JPanel();
  280. keyboardPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
  281. keyboardPanel.setBackground(new Color(0x5302d0));
  282. c.gridx = 0;
  283. c.gridy = 5;
  284. c.fill = GridBagConstraints.HORIZONTAL;
  285. centerPanel.add(keyboardPanel, c);
  286. keyboardPanel.setLayout(new GridLayout(3, 10, 5, 5));
  287. for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
  288. JButton button = new JButton(String.valueOf(alphabet));
  289. button.addActionListener(buttonHandler);
  290. button.setFont(new Font("Arial", Font.PLAIN, 20));
  291. keyboardPanel.add(button);
  292. alphabetButtons.add(button);
  293. }
  294.  
  295. // Disable buttons before game starts
  296. startButton.setEnabled(true);
  297. nextButton.setEnabled(false);
  298. previousButton.setEnabled(false);
  299. sendButton.setEnabled(false);
  300. submitButton.setEnabled(false);
  301. pauseButton.setEnabled(false);
  302. for (JButton alphabetButton : alphabetButtons) {
  303. alphabetButton.setEnabled(false);
  304. }
  305. }
  306.  
  307. private boolean wordIsComplete() {
  308. for (int i = 0; i < answer.length(); i++) {
  309. char ch = answer.charAt(i);
  310. if (this.guesses.indexOf(ch) == -1) {
  311. return false;
  312. }
  313. }
  314. return true;
  315. }
  316.  
  317. private JMenuBar menuBarCreator() {
  318. // Create the menu bar
  319. JMenuBar menuBar = new JMenuBar();
  320. JMenu menuFile = new JMenu("File");
  321. JMenu menuHelp = new JMenu("Help");
  322.  
  323. JMenuItem menuFileExit = new JMenuItem("Exit");
  324. JMenuItem menuHelpAbout = new JMenuItem("About");
  325. JMenuItem menuHelpRules = new JMenuItem("Rules");
  326.  
  327. // Exit action
  328. menuFileExit.addActionListener(e -> System.exit(0));
  329.  
  330. // Make the shortcuts for the items
  331. menuFile.setMnemonic(KeyEvent.VK_F);
  332. menuHelp.setMnemonic(KeyEvent.VK_H);
  333.  
  334. // Out the menu parts with each other
  335. menuBar.add(menuFile);
  336. menuBar.add(menuHelp);
  337. menuFile.add(menuFileExit);
  338. menuHelp.add(menuHelpRules);
  339. menuHelp.add(menuHelpAbout);
  340. menuHelp.add(menuHelpRules);
  341.  
  342. return menuBar;
  343. }
  344.  
  345. /**
  346. * This class defines a listener that will respond to the events that occur
  347. * when the user clicks any of the buttons in the button. The buttons are
  348. * labeled "Next word", "Give up", "Quit", "A", "B", "C", ..., "Z".
  349. */
  350. private class ButtonHandler implements ActionListener {
  351. @Override
  352. public void actionPerformed(java.awt.event.ActionEvent e) {
  353.  
  354. // The text from the button that the user clicked.
  355. String cmd = e.getActionCommand();
  356.  
  357. // Respond to Quit button by ending the program.
  358. if (cmd.equals(GuiText.QUIT.toString())) {
  359. System.exit(0);
  360. } else if (cmd.equals(GuiText.START.toString())) {
  361. startGame();
  362.  
  363. } else if (cmd.equals(GuiText.PREVIOUS.toString())) {
  364. QuestionList.selectRandomQuestion();
  365. questionLabel.setText(message);
  366. } else if (cmd.equals(GuiText.NEXT.toString())) {
  367. QuestionList.selectRandomQuestion();
  368. questionLabel.setText(message);
  369. } else if (cmd.equals(GuiText.SUBMIT.toString())) {
  370. processQuestionResponse(textArea.getText());
  371. } else if (cmd.equals(GuiText.RESET.toString())) {
  372. textArea.setText("");
  373. } else {
  374. textArea.setText(cmd);
  375. }
  376. // Causes the display to be redrawn, to show any changes made.
  377. display.repaint();
  378. }
  379. }
  380.  
  381. private void startGame() {
  382. this.guesses = "";
  383. this.badGuesses = 0;
  384.  
  385. startButton.setEnabled(false);
  386. nextButton.setEnabled(true);
  387. previousButton.setEnabled(true);
  388. sendButton.setEnabled(true);
  389. submitButton.setEnabled(true);
  390. pauseButton.setEnabled(true);
  391. for (JButton alphabetButton : alphabetButtons) {
  392. alphabetButton.setEnabled(true);
  393. }
  394.  
  395. QuestionList.loadQuestions();
  396. QuestionList.selectRandomQuestion();
  397. questionLabel.setText(message);
  398. }
  399.  
  400. private void processQuestionResponse(String response) {
  401. char guess = response.toUpperCase().charAt(0);
  402. guesses = String.valueOf(guesses) + guess;
  403. if (wordIsComplete()) {
  404. alertMessage = "Correct answer! Click on \"Next\" to play again.";
  405. nextButton.setEnabled(true);
  406. for (JButton b : alphabetButtons)
  407. b.setEnabled(false);
  408. gameOver = true;
  409. } else if (answer.indexOf(guess) >= 0) {
  410. alertMessage = String.format("Yes, %s is in the word. Pick your next letter.", guess);
  411. } else {
  412. badGuesses = badGuesses + 1;
  413. if (badGuesses == totalGuesses) {
  414. alertMessage = String.format("Sorry, you're ran out of guesses! The answer was '%s'.", answer);
  415. nextButton.setEnabled(true);
  416. for (JButton b : alphabetButtons)
  417. b.setEnabled(false);
  418. gameOver = true;
  419. } else {
  420. alertMessage = String.format("Sorry, %s is not in the word. Pick your next letter.", guess);
  421. }
  422. }
  423. display.repaint();
  424. }
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement