通过键盘管理对象移动

时间:2014-06-26 14:44:24

标签: java multithreading game-physics keylistener

我是Java的初学者,我正在尝试制作一个简单的视频游戏。 此刻,我确保一个球用螺纹独自移动到面板中。 我要解决的问题是通过键盘改变球的方向。 我试图实现KeyListener或扩展KeyAdapter但我不知道为什么它不起作用... 我发布没有keyListener或Adapter的代码,如果有人能告诉我如何管理这些动作,我真的很感激。

package threadball;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JPanel;

public class ThreadBall extends JPanel {


  int xDirection, yDirection;

  private Rectangle ball;
  private BallThread ballThread;

  private class BallThread implements Runnable {

      private int sleep = 5;
      private Thread thread;

      @Override
      public void run() {
          try {
              while (true) {
                  moveBall();
                  repaint();
                  Thread.sleep(sleep);
              }
          } catch (InterruptedException ex) {
          }
      }

      public void start() {
          stop();
          thread = new Thread(this);
          thread.start();
      }

      public void stop() {
          if (thread != null && thread.isAlive()) {
              thread.interrupt();
          }
      }
  }

  public ThreadBall() {

      this.setBackground(Color.white);
      this.xDirection = 1;
      this.yDirection = 1;
      this.ball = new Rectangle(20, 20);
      ball.x = 150;
      ball.y = 0;

      this.ballThread = new BallThread();
      this.ballThread.start();
  }

  @Override
  protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.red);
      g.fillOval(this.ball.x, this.ball.y, this.ball.width, this.ball.height);
  }

  public void moveBall() {
      this.ball.x += this.xDirection;
      this.ball.y += this.yDirection;

      if (this.ball.x <= 0) {
          this.xDirection = 1;
      } else if (this.ball.x >= this.getWidth()) {
          this.xDirection = -1;
      }

      if (this.ball.y <= 0) {
          this.yDirection = 1;
      } else if (this.ball.y >= this.getHeight()) {
          this.yDirection = -1;
      }
   }
}

P.S。 xDirection和yDirection中的'1'表示x向右移动1个像素,y向下移动。 -1 - &gt; x朝向1像素左侧,y朝向上方。

我的问题是我试图做那样的事情:

    private class keyListenerTest extends KeyAdapter{
    public void keyPressed(KeyEvent e){
        int keyCode = e.getKeyCode();

        if(keyCode == KeyEvent.VK_LEFT){
            xDirection = -1;
        }
        if(keyCode == KeyEvent.VK_RIGHT){
            xDirection = 1;
        }
        if(keyCode == KeyEvent.VK_UP){
            yDirection = -1;
        }
        if(keyCode == KeyEvent.VK_DOWN){
            yDirection = 1;
        }
      }
   }

在“公共类ThreadBall”中,我试图在ThreadBall的构造函数中添加“keyListenerTest”     addKeyListener(new keyListenerTest());

但它不起作用。

2 个答案:

答案 0 :(得分:0)

尝试使用KeyEventDispatcher,它会监听所有按键并向您报告:

public static void loadKeyboardManager(){
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher(){

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            //TODO your code                
            return false;
        }
    });
}

我希望这会有所帮助:)。

答案 1 :(得分:0)

我解决了......我必须在threadBall的构造函数中传递参数MainFrame pFrame,然后将keyListener添加到pFrame。 因此

public ThreadBall(MainFrame pFrame) {

  pFrame.addKeyListener(new keyListener());
  this.setBackground(Color.white);
  this.xDirection = 1;
  this.yDirection = 1;
  this.ball = new Rectangle(20, 20);
  ball.x = 150;
  ball.y = 0;

  this.ballThread = new BallThread();
  this.ballThread.start();

}

在主框架中我添加了一个新的ThreadBall,将“this”(框架)传递给它。

   public MainFrame() {
    this.setSize(500, 500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(new ThreadBall(this));
}

我希望这个解决方案可以帮助那些遇到同样问题的人;)。