我是Java的初学者,正在尝试制作一个简单的视频游戏。
此刻,我确保一个球带有Thread单独进入面板。
我要解决的问题是通过键盘更改球的方向。
我试图实现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-> 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());

但这是行不通的。

最佳答案

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

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

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


我希望这有帮助 :)。

10-05 19:15