You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
6.8 KiB

  1. package ui;
  2. import java.util.*;
  3. import javax.swing.JFrame;
  4. import javax.swing.JMenu;
  5. import javax.swing.JMenuBar;
  6. import javax.swing.JMenuItem;
  7. import javax.swing.JPanel;
  8. import javax.swing.BoxLayout;
  9. import javax.swing.JButton;
  10. import javax.swing.WindowConstants;
  11. import playground.Playground;
  12. import java.awt.event.*;
  13. import org.apache.logging.log4j.Logger;
  14. import org.apache.logging.log4j.LogManager;
  15. /**
  16. * creates the game UI JFrame containing a canvas (see class {@link GamePanel})) for the levels to
  17. * paint the games. Has a menu for loading, saving games and an about menu item. two buttons allow
  18. * restarting the game or exit.
  19. *
  20. *
  21. */
  22. public class GameUI implements ActionListener {
  23. private static volatile int newAction = -1;
  24. /** the JFrame instance used in this window */
  25. protected JFrame frame = null;
  26. /** the panel holding all components, uses {@link BoxLayout} Y-Direction */
  27. protected JPanel panel = null;
  28. /**
  29. * the instance of {@link GamePanel} for Playgrounds to paint and refresh their level elements. It
  30. * is added as first component of {@link #panel}
  31. */
  32. private GamePanel canvas = null;
  33. /**
  34. * the button panel holding buttons, set on {@link #panel} at last, uses {@link BoxLayout}
  35. * X-Direction
  36. */
  37. protected JPanel buttonPanel = null;
  38. protected JMenuItem playItem;
  39. protected JMenuItem loadItem;
  40. protected JMenuItem saveItem;
  41. protected JMenuItem quitItem;
  42. protected JMenuItem aboutItem;
  43. protected JMenu gameMenu;
  44. protected JMenu helpMenu;
  45. protected JButton button;
  46. protected JButton button2;
  47. public static final int ACTION_NEW = 1;
  48. public static final int ACTION_LOAD = 2;
  49. public static final int ACTION_SAVE = 3;
  50. public static final int ACTION_RESET = 4;
  51. public static final int ACTION_QUIT = 5;
  52. public static final int ACTION_BUTTON = 6;
  53. public static final int ACTION_PAUSE = 6;
  54. public static final int ACTION_ABOUT = 7;
  55. private static Logger logger = LogManager.getLogger(GameUI.class);
  56. /**
  57. * as typical for GUI classes this constructor creates all the components and adds them to the
  58. * frame. It adds this instance as ActionListener for all components. See
  59. * {@link #actionPerformed(ActionEvent)} for details. It directly sets the frame visible as a
  60. * final step.
  61. *
  62. * <p>
  63. * If you want to extend this GUI, create a subclass and add new elements in constructor. It is
  64. * necessary to call {@link JFrame#revalidate()} on {@link #frame} attribute after changing/adding
  65. * components to {@link #panel} or {@link #canvas}, because the constructor here already sets
  66. * visibility to true and renders the IFrame.
  67. * </p>
  68. *
  69. * @param sizeX pixel dimension wanted in x direction
  70. * @param sizeY pixel dimension wanted in y direction
  71. */
  72. public GameUI(int sizeX, int sizeY) {
  73. // create a canvas on which the levels (Playgrounds) will be painted later when loaded and
  74. // started.
  75. this.canvas = new GamePanel();
  76. // create contentPane
  77. this.panel = new JPanel();
  78. this.panel.setLayout(new BoxLayout(this.panel, BoxLayout.Y_AXIS));
  79. // panel.setLayout(new FlowLayout());
  80. // panel.setLayout(new GridBagLayout());
  81. // panel.setLayout(new SpringLayout());
  82. this.panel.add(canvas);
  83. // create main window
  84. this.frame = new JFrame("Prog2 GameProject!");
  85. this.frame.setContentPane(panel);
  86. // Menu
  87. this.playItem = new JMenuItem("New Game");
  88. this.loadItem = new JMenuItem("Restore game");
  89. this.saveItem = new JMenuItem("Save game");
  90. this.quitItem = new JMenuItem("Exit game");
  91. this.playItem.addActionListener(this);
  92. this.loadItem.addActionListener(this);
  93. this.saveItem.addActionListener(this);
  94. this.quitItem.addActionListener(this);
  95. this.gameMenu = new JMenu("Game");
  96. this.gameMenu.add(playItem);
  97. this.gameMenu.add(loadItem);
  98. this.gameMenu.add(saveItem);
  99. this.gameMenu.addSeparator();
  100. this.gameMenu.add(quitItem);
  101. this.helpMenu = new JMenu("Help");
  102. this.aboutItem = new JMenuItem("About");
  103. this.helpMenu.add(this.aboutItem);
  104. // for extending the code change here to your own class/implementation of an ActionListener or
  105. // extend method this.actionPerformed
  106. this.aboutItem.addActionListener(this);
  107. JMenuBar mb = new JMenuBar();
  108. mb.add(this.gameMenu);
  109. mb.add(this.helpMenu);
  110. frame.setJMenuBar(mb);
  111. this.button = new JButton("(Re)Start");
  112. this.button.addActionListener(this);
  113. this.button2 = new JButton("Pause");
  114. this.button2.addActionListener(this);
  115. this.buttonPanel = new JPanel();
  116. this.buttonPanel.setLayout(new BoxLayout(this.buttonPanel, BoxLayout.X_AXIS));
  117. this.buttonPanel.add(this.button);
  118. this.buttonPanel.add(this.button2);
  119. this.panel.add(this.buttonPanel);
  120. // make it visible (render window)
  121. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  122. this.canvas.setVisible(true);
  123. frame.pack();
  124. frame.setVisible(true);
  125. }
  126. public HashMap<Integer, Integer> getCurrentKey() {
  127. return canvas.getCurrentKey();
  128. }
  129. public Stack<KeyEvent> getKeyEvents() {
  130. return this.canvas.getKeyEvents();
  131. }
  132. public Stack<MouseEvent> getMouseEvents() {
  133. return this.canvas.getMouseEvents();
  134. }
  135. public void repaint() {
  136. canvas.repaint();
  137. }
  138. public void setPlayground(Playground pg) {
  139. this.canvas.setPlayground(pg);
  140. this.frame.validate();
  141. this.frame.pack();
  142. }
  143. public boolean isPainting() {
  144. return canvas.stillPainting();
  145. }
  146. public void setPainting() {
  147. canvas.setPainting();
  148. }
  149. public void waitWhilePainting() {
  150. canvas.setPainting();
  151. canvas.repaint();
  152. canvas.waitWhilePainting();
  153. }
  154. public static int getNewAction() {
  155. return newAction;
  156. }
  157. public static void resetAction() {
  158. newAction = -1;
  159. }
  160. public void grabFocus() {
  161. canvas.grabFocus();
  162. }
  163. /**
  164. * interface implementation of ActionListener to respond to GUI element actions. It sets the
  165. * attribute of newAction which is read by GameLoop.runGame to check for GUI actions.
  166. */
  167. public void actionPerformed(ActionEvent ae) {
  168. if (ae.getSource() == this.quitItem) {
  169. System.exit(0);
  170. } else if (ae.getSource() == this.playItem) {
  171. logger.info("new game");
  172. newAction = ACTION_NEW;
  173. } else if (ae.getSource() == this.button) {
  174. logger.info("click");
  175. newAction = ACTION_NEW;
  176. } else if (ae.getSource() == this.button2) {
  177. logger.info("click2");
  178. newAction = ACTION_PAUSE;
  179. } else if (ae.getSource() == this.saveItem) {
  180. logger.info("save");
  181. newAction = ACTION_SAVE;
  182. } else if (ae.getSource() == this.loadItem) {
  183. logger.info("load");
  184. newAction = ACTION_LOAD;
  185. } else if (ae.getSource() == this.aboutItem) {
  186. logger.info("about");
  187. newAction = ACTION_ABOUT;
  188. AboutFrame about = new AboutFrame();
  189. about.show();
  190. }
  191. }
  192. }