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.

145 lines
3.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. package ui;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.util.HashMap;
  8. import java.util.Stack;
  9. import javax.swing.JPanel;
  10. import playground.Playground;
  11. import java.awt.event.*;
  12. import org.apache.logging.log4j.Logger;
  13. import org.apache.logging.log4j.LogManager;
  14. /**
  15. * UI element that holds the graphic visualization of current {@link Playground}. Registers key and
  16. * mouse events.
  17. *
  18. */
  19. class GamePanel extends JPanel implements KeyListener, MouseListener {
  20. private static final long serialVersionUID = 1L;
  21. protected volatile boolean painting = false;
  22. private Playground playground = null;
  23. private HashMap<Integer, Integer> keys = new HashMap<Integer, Integer>();
  24. Stack<KeyEvent> keyEvents = new Stack<KeyEvent>();
  25. Stack<MouseEvent> mouseEvents = new Stack<MouseEvent>();
  26. private static Logger logger = LogManager.getLogger(GamePanel.class);
  27. GamePanel() {
  28. super();
  29. setPreferredSize(new Dimension(100, 100));
  30. addKeyListener(this);
  31. }
  32. public void setSize(int sizeX, int sizeY) {
  33. setPreferredSize(new Dimension(sizeX, sizeY));
  34. }
  35. @Override
  36. public void repaint() {
  37. super.repaint();
  38. }
  39. public void setPainting() {
  40. painting = true;
  41. }
  42. public void waitWhilePainting() {
  43. while (painting) {
  44. }
  45. }
  46. Stack<KeyEvent> getKeyEvents() {
  47. return keyEvents;
  48. }
  49. Stack<MouseEvent> getMouseEvents() {
  50. return mouseEvents;
  51. }
  52. public void setPlayground(Playground pg) {
  53. this.playground = pg;
  54. if (this.playground != null) {
  55. setPreferredSize(new Dimension(this.playground.getSizeX(), this.playground.getSizeX()));
  56. setSize(getPreferredSize());
  57. this.invalidate();
  58. }
  59. }
  60. public boolean stillPainting() {
  61. return painting;
  62. }
  63. @Override
  64. public void paintComponent(Graphics g) {
  65. super.paintComponent(g);
  66. logger.trace("my Playground: " + playground);
  67. if (playground != null) {
  68. playground.redraw((Graphics2D) g);
  69. }
  70. painting = false;
  71. }
  72. public HashMap<Integer, Integer> getCurrentKey() {
  73. return keys;
  74. }
  75. @Override
  76. public void keyTyped(KeyEvent e) {
  77. }
  78. @Override
  79. public void keyPressed(KeyEvent e) {
  80. logger.debug("pressed keyCode " + e.getKeyCode());
  81. logger.trace(e.paramString());
  82. this.keyEvents.push(e);
  83. }
  84. @Override
  85. public void keyReleased(KeyEvent e) {
  86. logger.debug("released keyCode " + e.getKeyCode());
  87. this.keyEvents.push(e);
  88. logger.trace(e.paramString());
  89. }
  90. @Override
  91. public void mouseClicked(MouseEvent e) {
  92. this.mouseEvents.push(e);
  93. }
  94. @Override
  95. public void mouseReleased(MouseEvent e) {
  96. // this.mouseEvents.push(e);
  97. }
  98. @Override
  99. public void mousePressed(MouseEvent e) {
  100. // this.mouseEvents.push(e);
  101. }
  102. @Override
  103. public void mouseEntered(MouseEvent e) {
  104. // this.mouseEvents.push(e);
  105. }
  106. @Override
  107. public void mouseExited(MouseEvent e) {
  108. // this.mouseEvents.push(e);
  109. }
  110. }