Java 通过键盘管理对象移动
我是Java的初学者,我正在尝试制作一个简单的视频游戏。 此时,我确保一个球用一根线单独移动到面板中。 我要解决的问题是通过键盘改变球的方向。 我试图实现KeyListener或扩展KeyAdapter,但我不知道为什么它不起作用。。。 我在没有keyListener或Adapter的情况下发布代码,如果有人能告诉我如何管理这些动作,我将不胜感激Java 通过键盘管理对象移动,java,multithreading,game-physics,keylistener,Java,Multithreading,Game Physics,Keylistener,我是Java的初学者,我正在尝试制作一个简单的视频游戏。 此时,我确保一个球用一根线单独移动到面板中。 我要解决的问题是通过键盘改变球的方向。 我试图实现KeyListener或扩展KeyAdapter,但我不知道为什么它不起作用。。。 我在没有keyListener或Adapter的情况下发布代码,如果有人能告诉我如何管理这些动作,我将不胜感激 package threadball; import java.awt.Color; import java.awt.Graphics; impor
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;
}
}
}
在“公共类ThreadBall”中,我尝试在ThreadBall的构造函数中添加“keyListenerTest”
addKeyListener(新的keyListenerTest())
但它不起作用。尝试使用KeyEventDispatcher,它将侦听所有按键并向您报告:
public static void loadKeyboardManager(){
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher(){
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
//TODO your code
return false;
}
});
}
我希望这有帮助:)。我解决了…我必须在threadBall的构造函数中传递参数MainFrame pFrame,然后将keyListener添加到pFrame。 因此 } 在主框架中,我添加了一个新的螺纹球,并将“this”(框架)传递给它 我希望这个解决方案能帮助有我同样问题的人;)
public static void loadKeyboardManager(){
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher(){
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
//TODO your code
return false;
}
});
}
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();
public MainFrame() {
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new ThreadBall(this));
}