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;
13 import javax.swing.JPanel;
15 import de.lathanda.eos.base.MutableColor;
16 import de.lathanda.eos.base.Picture2D;
25 class GamePanel
extends JPanel
26 implements KeyEventDispatcher, MouseListener, MouseMotionListener {
31 private static final long serialVersionUID = -2303718998046834449L;
45 private int prefHeight;
46 private int prefWidth;
55 public GamePanel(Game game,
double width,
double height) {
56 this(game, 30, MutableColor.WHITE, width, height);
67 public GamePanel(Game game,
int fps,
double width,
double height) {
68 this(game, fps, MutableColor.WHITE, width, height);
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);
88 setForeground(MutableColor.BLACK.getColor());
89 setBackground(background.getColor());
91 addMouseListener(
this);
92 addMouseMotionListener(
this);
95 public Dimension getPreferredSize() {
96 return new Dimension(prefWidth, prefHeight);
102 public void mouseClicked(MouseEvent e) {
110 public void mousePressed(MouseEvent e) {
111 game.mouseDown(e.getButton());
118 public void mouseReleased(MouseEvent e) {
119 game.mouseUp(e.getButton());
126 public void mouseEntered(MouseEvent e) {
134 public void mouseExited(MouseEvent e) {
144 public void mouseDragged(MouseEvent e) {
145 game.mousePosition(p.pointFromPixel(e.getX(), e.getY()));
152 public void mouseMoved(MouseEvent e) {
153 game.mousePosition(p.pointFromPixel(e.getX(), e.getY()));
161 public boolean dispatchKeyEvent(KeyEvent k) {
163 case KeyEvent.KEY_PRESSED:
164 game.keyDown(k.getKeyCode());
166 case KeyEvent.KEY_RELEASED:
167 game.keyUp(k.getKeyCode());
179 public void addSprite(Sprite sprite) {
180 game.addSprite(sprite);
184 public void paintComponent(Graphics g) {
185 super.paintComponent(g);
186 AffineTransform af = ((Graphics2D)g).getTransform();
187 p.setGraphics((Graphics2D)g);
189 ((Graphics2D)g).setTransform(af);