有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过键盘管理对象移动

我是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;
      }
   }
}

另外,x方向和y方向上的“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(新的keyListenerTest())

但它不起作用


共 (2) 个答案

  1. # 1 楼答案

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

    public static void loadKeyboardManager(){
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher(){
    
            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                //TODO your code                
                return false;
            }
        });
    }
    

    我希望这有帮助:)

  2. # 2 楼答案

    我决定。。。我必须在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();
    

    }

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

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

    我希望这个解决方案能帮助有同样问题的人