2012-02-29 63 views
5

我剛開始學習編程小程序和一般的java GUI,我試圖做一個簡單的遊戲。目前,我有主類(遊戲)和玩家類(玩家),我讓玩家在屏幕上畫畫,但我努力讓關鍵事件起作用,任何幫助表示讚賞!KeyListener事件顯然沒有在Java小程序中觸發

播放器圖像顯示非常好,但是當我按下某個按鍵時,位置不會改變。

編輯:錯誤的代碼發佈給玩家,更新

Game.java:

public class Game extends Applet { 

    Player player; 

    @Override 
    public void init() { 
     player = new Player(this); 
     this.setSize(800, 600); 
     addKeyListener(player); 
    } 

    @Override 
    public void paint(Graphics g) {   
     player.paint(g); 
    } 



} 

Player.java:

public class Player implements KeyListener { 

    Game game; 
    Image image; 
    int x; 
    int y; 

    public Player(Game game) { 
     this.game = game; 
     x = 0; 
     y = 0; 
     image = game.getImage(game.getCodeBase(), "images/Player.png"); 
    } 

    public void paint(Graphics g) { 
     g.drawImage(image, x, y, game); 
    } 

    @Override 
    public void keyTyped(KeyEvent e) { 
     return; 
    } 

    @Override 
    public void keyPressed(KeyEvent e) { 
     switch (e.getKeyCode()) { 
      case KeyEvent.VK_A: 
       x -= 1; 
       break; 
      case KeyEvent.VK_D: 
       x += 1; 
       break; 
      case KeyEvent.VK_S: 
       y += 1; 
       break; 
      case KeyEvent.VK_W: 
       y -= 1; 
       break; 
     } 
     game.repaint(); 
    } 

    @Override 
    public void keyReleased(KeyEvent e) { 
     return; 
    } 
} 
+2

1)*「我剛開始學習編程的小程序,和一般的Java GUI」 *避免applet和學習使用框架。它們更簡單。 2)這個千年,使用Swing而不是AWT。 3)applet大小由HTML設置,不要在'init()'中調用'setSize()'。 – 2012-02-29 19:17:12

回答

8

面板可能要麼沒有焦點,或不可重點。


試試這個代碼。我將按鍵更改爲左/右/上/下箭頭。爲了簡單起見,我把它作爲一個基於AWT的applet。

// <applet code='Game' width=400 height=100></applet> 
import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Game extends Applet implements KeyListener { 

    Player player; 

    @Override 
    public void init() { 
     player = new Player(getImage(32)); 
     addKeyListener(this); 
     setFocusable(true); 
     requestFocusInWindow(); 
    } 

    @Override 
    public void paint(Graphics g) { 
     player.paint(g); 
    } 

    public Image getImage(int size) { 
     Image img = createImage(size,size); 
     Graphics g = img.getGraphics(); 
     g.setColor(Color.RED); 
     g.fillRect(0,0,size,size); 

     g.dispose(); 

     return img; 
    } 

    @Override 
    public void keyTyped(KeyEvent e) { 
     return; 
    } 

    @Override 
    public void keyPressed(KeyEvent e) { 
     switch (e.getKeyCode()) { 
      case KeyEvent.VK_LEFT: 
       player.x -= 1; 
       break; 
      case KeyEvent.VK_RIGHT: 
       player.x += 1; 
       break; 
      case KeyEvent.VK_DOWN: 
       player.y += 1; 
       break; 
      case KeyEvent.VK_UP: 
       player.y -= 1; 
       break; 
     } 
     repaint(); 
    } 

    @Override 
    public void keyReleased(KeyEvent e) { 
     return; 
    } 
} 

class Player { 

    Image image; 
    int x; 
    int y; 

    public Player(Image image) { 
     x = 0; 
     y = 0; 
     this.image = image; 
    } 

    public void paint(Graphics g) { 
     g.drawImage(image, x, y, null); 
    } 

} 
+2

對於'setFocusable(true);'+1 – 2012-12-12 00:54:34