Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. package com.ssaurel.twentyquestions;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Toolkit;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextPane;
  14. import javax.swing.SwingUtilities;
  15. import javax.swing.text.SimpleAttributeSet;
  16. import javax.swing.text.StyleConstants;
  17. import javax.swing.text.StyledDocument;
  18.  
  19. // Main class for our Twenty Questions game with a Swing UI
  20. public class TwentyQuestions {
  21.  
  22. private Tree tree;
  23. private TreeNode currentNode;
  24. private JButton yesButton, noButton, restartButton;
  25. private JTextPane textPane; // for displaying the question
  26. private boolean started = false;
  27. // btns listener
  28. private ActionListener btnsListener = new ActionListener() {
  29.  
  30. @Override
  31. public void actionPerformed(ActionEvent e) {
  32. Object source = e.getSource();
  33.  
  34. if (source == yesButton)
  35. yes();
  36. else if (source == noButton)
  37. no();
  38. else if (source == restartButton)
  39. restart();
  40.  
  41. }
  42. };
  43.  
  44. public static void main(String[] args) {
  45. TwentyQuestions twentyQuestions = new TwentyQuestions();
  46. twentyQuestions.tree = new Tree();
  47. twentyQuestions.tree.loadTree("/Users/ssaurel/eworkspace/TwentyQuestions/animals_questions.txt");
  48.  
  49. SwingUtilities.invokeLater(new Runnable() {
  50. @Override
  51. public void run() {
  52. twentyQuestions.buildUI();
  53. }
  54. });
  55. }
  56.  
  57. private void buildUI() {
  58. // we build the UI
  59. JFrame frame = new JFrame("Twenty Questions on SSaurel");
  60. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  61. Container contentPane = frame.getContentPane();
  62.  
  63. // add buttons
  64. JPanel buttonsPanel = new JPanel();
  65.  
  66. yesButton = new JButton("Yes");
  67. yesButton.addActionListener(btnsListener);
  68. buttonsPanel.add(yesButton, BorderLayout.LINE_START);
  69.  
  70. restartButton = new JButton("Start");
  71. restartButton.addActionListener(btnsListener);
  72. buttonsPanel.add(restartButton, BorderLayout.LINE_START);
  73.  
  74. noButton = new JButton("No");
  75. noButton.addActionListener(btnsListener);
  76. buttonsPanel.add(noButton, BorderLayout.LINE_END);
  77.  
  78. contentPane.add(buttonsPanel, BorderLayout.PAGE_END);
  79.  
  80. // add text area
  81. textPane = new JTextPane();
  82. textPane.setEditable(false);
  83. updateText("Think to an animal, then click on Start");
  84.  
  85. // we define some style for the text pane
  86. SimpleAttributeSet bSet = new SimpleAttributeSet();
  87. StyleConstants.setAlignment(bSet, StyleConstants.ALIGN_CENTER);
  88. StyleConstants.setFontSize(bSet, 22);
  89. StyledDocument doc = textPane.getStyledDocument();
  90. doc.setParagraphAttributes(0, doc.getLength(), bSet, false);
  91.  
  92. contentPane.add(textPane, BorderLayout.CENTER);
  93.  
  94. frame.setMinimumSize(new Dimension(500, 250));
  95.  
  96. // we center the JFrame
  97. Dimension objDimension = Toolkit.getDefaultToolkit().getScreenSize();
  98. int coordX = (objDimension.width - frame.getWidth()) / 2;
  99. int coordY = (objDimension.height - frame.getHeight()) / 2;
  100. frame.setLocation(coordX, coordY);
  101.  
  102. // we display the window
  103. frame.pack();
  104. frame.setVisible(true);
  105. }
  106.  
  107. // we need to write code for callbacks methods yes, no and restart
  108. private void yes() {
  109. // we navigate in the tree by moving the current node on the yes branch
  110. if (started && currentNode != null) {
  111. currentNode = currentNode.yes;
  112.  
  113. if (currentNode != null) {
  114. if (currentNode.isQuestion()) {
  115. updateText(currentNode.data);
  116. } else {
  117. updateText("Would you think to " + currentNode.data + "?");
  118. }
  119. } else {
  120. updateText("I found :)");
  121. }
  122. }
  123. }
  124.  
  125. private void no() {
  126. // we navigate in the tree by moving the current node on the no branch
  127. if (started && currentNode != null) {
  128. currentNode = currentNode.no;
  129.  
  130. if (currentNode != null) {
  131. if (currentNode.isQuestion()) {
  132. updateText(currentNode.data);
  133. } else {
  134. updateText("Would you think to " + currentNode.data + "?");
  135. }
  136. } else {
  137. updateText("I lose :(");
  138. }
  139. }
  140. }
  141.  
  142. private void restart() {
  143. if (started) {
  144. started = false;
  145. updateText("Think to an animal, then click on Start");
  146. } else {
  147. started = true;
  148. updateText("Think to an animal, then click on Start");
  149. currentNode = tree.root;
  150. updateText(currentNode.data);
  151. }
  152. }
  153.  
  154. private void updateText(String txt) {
  155. textPane.setText("\n" + txt);
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement