PageRenderTime 216ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/Assignment1/src/asgn1Question/GamePanel.java

https://bitbucket.org/critnal/java
Java | 289 lines | 185 code | 39 blank | 65 comment | 13 complexity | ae6c973dce435c6df5638739fb9b64d7 MD5 | raw file
  1. package asgn1Question;
  2. import java.awt.Component;
  3. import java.awt.Font;
  4. import java.awt.GridBagConstraints;
  5. import java.awt.GridBagLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.BorderFactory;
  9. import javax.swing.JButton;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JTextArea;
  14. import javax.swing.JTextField;
  15. import asgn1Solution.HiringGame;
  16. import java.util.Random;
  17. /**
  18. * The actual GUI components used to run the game and also stores the current
  19. * game state.
  20. *
  21. * @author INB370
  22. * @version 1.0
  23. */
  24. @SuppressWarnings("serial") // We don't care about binary i/o here
  25. public class GamePanel extends JPanel implements ActionListener {
  26. // Constants
  27. public static final int DefaultMaxApplicants = 20;
  28. public static final long DefaultSeed = 100;
  29. // GUI Components
  30. private JButton newGameButton;
  31. private JButton acceptButton;
  32. private JButton rejectButton;
  33. private JTextArea display;
  34. private JScrollPane textScrollPane;
  35. private JLabel seedLabel;
  36. private JTextField seedText;
  37. private JLabel maxApplicantsLabel;
  38. private JTextField maxApplicantsText;
  39. // Game state
  40. private IHiringGame game;
  41. private IApplicant currentApplicant;
  42. /**
  43. * Creates a new game panel and initializes all of the contained GUI
  44. * components.
  45. */
  46. public GamePanel() {
  47. // Initialize the GUI Components
  48. initializeComponents();
  49. game = new HiringGame();
  50. showNewGameControls();
  51. }
  52. /**
  53. * This method is called when any button in the GUI is clicked.
  54. *
  55. * The method calls an appropriate handler based on which button was
  56. * clicked.
  57. *
  58. * @param evt Action details
  59. */
  60. public void actionPerformed(ActionEvent evt) {
  61. // Get event source
  62. Object src = evt.getSource();
  63. // Consider the alternatives - not all active at once.
  64. if (src == newGameButton) {
  65. handleNewGame();
  66. } else if (src == acceptButton) {
  67. handleAcceptButton();
  68. } else if (src == rejectButton) {
  69. handleRejectButton();
  70. }
  71. }
  72. /**
  73. * Handler for New Game - wrapper of try catch and responses
  74. */
  75. private void handleNewGame() {
  76. try {
  77. long seed = Long.parseLong(seedText.getText().trim());
  78. int maxApplicants = Integer.parseInt(maxApplicantsText.getText()
  79. .trim());
  80. game.newGame(maxApplicants, new Random(seed));
  81. currentApplicant = game.getNextApplicant();
  82. updateDisplay();
  83. } catch (NumberFormatException e) {
  84. display.setText(e.toString());
  85. } catch (HiringException e) {
  86. display.setText(e.toString());
  87. } catch (Exception e) {
  88. display.setText("Unandled Exception: " + e.toString());
  89. throw new RuntimeException(e);
  90. }
  91. }
  92. /**
  93. * Handler Accept - wrapper of try catch and responses
  94. */
  95. private void handleAcceptButton() {
  96. try {
  97. game.acceptApplicant();
  98. updateDisplay();
  99. } catch (HiringException e) {
  100. display.setText(e.getMessage());
  101. } catch (Exception e) {
  102. display.setText("Unandled Exception: " + e.getMessage());
  103. throw new RuntimeException(e);
  104. }
  105. }
  106. /**
  107. * Handler for Reject - wrapper of try catch and responses
  108. */
  109. private void handleRejectButton() {
  110. try {
  111. if (!game.isAccepted()) {
  112. currentApplicant = game.getNextApplicant();
  113. }
  114. updateDisplay();
  115. } catch (HiringException e) {
  116. display.setText(e.getMessage());
  117. } catch (Exception e) {
  118. display.setText("Unandled Exception: " + e.toString());
  119. throw new RuntimeException(e);
  120. }
  121. }
  122. /**
  123. * Handler for Update - wrapper of try catch and responses
  124. */
  125. private void updateDisplay() {
  126. try {
  127. if (game.isAccepted()) {
  128. String str = "Selected Applicant:\n\n"
  129. + currentApplicant.toString() + "\n\nwas ";
  130. if (game.isBestApplicant()) {
  131. str += "the BEST. You Win!";
  132. } else {
  133. str += "not the best available. You Lose.\n\nBest applicant was: \n\n"
  134. + game.getBestApplicant();
  135. }
  136. display.setText(str);
  137. showNewGameControls();
  138. } else {
  139. display.setText("Current applicant is:\n\n"
  140. + currentApplicant.toString()
  141. + "\n\nDo you wish to accept or reject?");
  142. showCurrentGameControls();
  143. }
  144. } catch (HiringException e) {
  145. display.setText(e.getMessage());
  146. } catch (Exception e) {
  147. display.setText("Unandled Exception: " + e.toString());
  148. throw new RuntimeException(e);
  149. }
  150. }
  151. /**
  152. * Auxiliary for widget layout
  153. */
  154. private void initializeComponents() {
  155. GridBagLayout layout = new GridBagLayout();
  156. this.setLayout(layout);
  157. GridBagConstraints constraints = new GridBagConstraints();
  158. constraints.anchor = GridBagConstraints.CENTER;
  159. constraints.weightx = 100;
  160. constraints.weighty = 100;
  161. constraints.fill = GridBagConstraints.BOTH;
  162. // Text Area and Scroll Pane
  163. display = new JTextArea();
  164. display.setEditable(false);
  165. display.setLineWrap(true);
  166. display.setFont(new Font("Arial", Font.BOLD, 24));
  167. display.setBorder(BorderFactory.createEtchedBorder());
  168. textScrollPane = new JScrollPane(display);
  169. addToPanel(textScrollPane, constraints, 1, 0, 3, 1);
  170. // Buttons
  171. constraints.fill = GridBagConstraints.NONE;
  172. constraints.anchor = GridBagConstraints.CENTER;
  173. constraints.weightx = 10;
  174. constraints.weighty = 20;
  175. acceptButton = new JButton("Accept Applicant");
  176. acceptButton.addActionListener(this);
  177. addToPanel(acceptButton, constraints, 1, 10, 1, 1);
  178. rejectButton = new JButton("Reject Applicant");
  179. rejectButton.addActionListener(this);
  180. addToPanel(rejectButton, constraints, 3, 10, 1, 1);
  181. constraints.weighty = 10;
  182. newGameButton = new JButton("Start New Game");
  183. newGameButton.addActionListener(this);
  184. addToPanel(newGameButton, constraints, 1, 9, 1, 2);
  185. // Labels
  186. constraints.fill = GridBagConstraints.NONE;
  187. constraints.anchor = GridBagConstraints.CENTER;
  188. constraints.weighty = 10;
  189. seedLabel = new JLabel("RNG Seed:");
  190. addToPanel(seedLabel, constraints, 2, 9, 1, 1);
  191. maxApplicantsLabel = new JLabel("Max Applicants:");
  192. addToPanel(maxApplicantsLabel, constraints, 3, 9, 1, 1);
  193. // Text Fields
  194. constraints.fill = GridBagConstraints.NONE;
  195. constraints.anchor = GridBagConstraints.CENTER;
  196. constraints.weightx = 80;
  197. seedText = new JTextField("" + DefaultSeed, 5);
  198. seedText.setEditable(true);
  199. addToPanel(seedText, constraints, 2, 10, 1, 1);
  200. maxApplicantsText = new JTextField("" + DefaultMaxApplicants, 5);
  201. maxApplicantsText.setEditable(true);
  202. addToPanel(maxApplicantsText, constraints, 3, 10, 1, 1);
  203. repaint();
  204. }
  205. /**
  206. * display control wrapper
  207. */
  208. private void showNewGameControls() {
  209. toggleControls(true);
  210. }
  211. /**
  212. * display control wrapper
  213. */
  214. private void showCurrentGameControls() {
  215. toggleControls(false);
  216. }
  217. /**
  218. * display control wrapper
  219. */
  220. private void toggleControls(boolean newGame) {
  221. newGameButton.setVisible(newGame);
  222. acceptButton.setVisible(!newGame);
  223. rejectButton.setVisible(!newGame);
  224. seedLabel.setVisible(newGame);
  225. seedText.setVisible(newGame);
  226. maxApplicantsLabel.setVisible(newGame);
  227. maxApplicantsText.setVisible(newGame);
  228. repaint();
  229. }
  230. /**
  231. *
  232. * A convenience method to add a component to given grid bag layout
  233. * locations. Code due to Cay Horstmann
  234. *
  235. * @param c the component to add
  236. * @param constraints the grid bag constraints to use
  237. * @param x the x grid position
  238. * @param y the y grid position
  239. * @param w the grid width
  240. * @param h the grid height
  241. */
  242. private void addToPanel(Component c, GridBagConstraints constraints, int x,
  243. int y, int w, int h) {
  244. constraints.gridx = x;
  245. constraints.gridy = y;
  246. constraints.gridwidth = w;
  247. constraints.gridheight = h;
  248. add(c, constraints);
  249. }
  250. }