Programmierung 2 - Praktikum
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.

243 lines
6.8 KiB

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