Objektorientierte Programmierung WS 2013/2014 - Datei Maus.java
import java.awt.*; import java.awt.event.*; class Maus implements SpielRolle { private static final int SPEED = 2; private static KeyListener kl; private SpielBall ich; private Frame spielfeld; public Maus (SpielBall ichSelbst, Frame spf) { ich = ichSelbst; spielfeld = spf; if (kl != null) spielfeld.removeKeyListener(kl); kl = new KeyAdapter() { public void keyPressed (KeyEvent e) { // Invoked when a key has been pressed. switch (e.getKeyCode()) { case KeyEvent.VK_UP : ich.setMotion(0, -SPEED); break; case KeyEvent.VK_RIGHT : ich.setMotion(SPEED, 0); break; case KeyEvent.VK_DOWN : ich.setMotion(0, SPEED); break; case KeyEvent.VK_LEFT : ich.setMotion(-SPEED, 0); break; } if (ich.blocked()) ich.setMotion(0, 0); } }; spielfeld.addKeyListener(kl); } public void moveSpielBall () { ich.move(); } public void paintSpielBall (Graphics g) { ich.paint(g); g.setColor(Color.black); g.drawString("M", ich.x() - 5, ich.y() - 5); } }
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 05.12.2013