EOS 2  1.1.0
Einfache Objektbasierte Sprache
GamePanel.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.game;
2 import java.awt.Dimension;
3 import java.awt.Graphics;
4 import java.awt.Graphics2D;
5 import java.awt.KeyEventDispatcher;
6 import java.awt.KeyboardFocusManager;
7 import java.awt.event.KeyEvent;
8 import java.awt.event.MouseEvent;
9 import java.awt.event.MouseListener;
10 import java.awt.event.MouseMotionListener;
11 import java.awt.geom.AffineTransform;
12 
13 import javax.swing.JPanel;
14 
15 import de.lathanda.eos.base.MutableColor;
16 import de.lathanda.eos.base.Picture2D;
17 
25 /*package*/ class GamePanel extends JPanel
26  implements KeyEventDispatcher, MouseListener, MouseMotionListener {
27 
31  private static final long serialVersionUID = -2303718998046834449L;
35  private Game game;
36 
40  private Picture2D p;
44  double fps;
45  private int prefHeight;
46  private int prefWidth;
47 
55  public GamePanel(Game game, double width, double height) {
56  this(game, 30, MutableColor.WHITE, width, height);
57  }
58 
67  public GamePanel(Game game, int fps, double width, double height) {
68  this(game, fps, MutableColor.WHITE, width, height);
69  }
70 
80  public GamePanel(Game game, int fps, MutableColor background, double width, double height) {
81  KeyboardFocusManager.getCurrentKeyboardFocusManager()
82  .addKeyEventDispatcher(this);
83  this.p = new Picture2D(width, height, this);
84  prefHeight = p.mm2pixel(height);
85  prefWidth = p.mm2pixel(width);
86  this.game = game;
87  this.fps = fps;
88  setForeground(MutableColor.BLACK.getColor());
89  setBackground(background.getColor());
90  // add events
91  addMouseListener(this);
92  addMouseMotionListener(this);
93  }
94  @Override
95  public Dimension getPreferredSize() {
96  return new Dimension(prefWidth, prefHeight);
97  }
102  public void mouseClicked(MouseEvent e) {
103  // no action
104  }
105 
110  public void mousePressed(MouseEvent e) {
111  game.mouseDown(e.getButton());
112  }
113 
118  public void mouseReleased(MouseEvent e) {
119  game.mouseUp(e.getButton());
120  }
121 
126  public void mouseEntered(MouseEvent e) {
127  // no action
128  }
129 
134  public void mouseExited(MouseEvent e) {
135  // no action
136  }
137 
138  // MouseMotionListener
144  public void mouseDragged(MouseEvent e) {
145  game.mousePosition(p.pointFromPixel(e.getX(), e.getY()));
146  }
147 
152  public void mouseMoved(MouseEvent e) {
153  game.mousePosition(p.pointFromPixel(e.getX(), e.getY()));
154  }
155 
161  public boolean dispatchKeyEvent(KeyEvent k) {
162  switch (k.getID()) {
163  case KeyEvent.KEY_PRESSED:
164  game.keyDown(k.getKeyCode());
165  break;
166  case KeyEvent.KEY_RELEASED:
167  game.keyUp(k.getKeyCode());
168  break;
169  }
170 
171  return false;
172  }
179  public void addSprite(Sprite sprite) {
180  game.addSprite(sprite);
181  }
182 
183  @Override
184  public void paintComponent(Graphics g) {
185  super.paintComponent(g);
186  AffineTransform af = ((Graphics2D)g).getTransform();
187  p.setGraphics((Graphics2D)g);
188  game.render(p);
189  ((Graphics2D)g).setTransform(af);
190  }
191 }
Impressum