gpt4 book ai didi

java - 只需按一下按键即可连续移动?

转载 作者:太空宇宙 更新时间:2023-11-04 07:35:46 25 4
gpt4 key购买 nike

我正在尝试用java编写一个程序,该程序涉及通过按一次按键使对象不断移动。想想 Pacman,你按下一次,Pacman 就会继续上升,直到你按下另一个键。如果可能的话,我想让代码保持简单。我原来的 Action (一次按键=一次 Action )是这样的:

public class AL extends KeyAdapter {
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_A){
x -= 5;
}
if(keyCode == e.VK_D){
x += 5;
}
if(keyCode == e.VK_W){
y -= 5;
}
if(keyCode == e.VK_S){
y += 5;
}

}

值中的 x 和 y 是椭圆的位置。这工作得很好,但我希望它在我只按一次键后继续移动,而不是必须按住它才能保持移动。我尝试了一个带有 boolean 参数的 while 循环,该参数在 true 时移动,在 false 时不移动,但是一旦我激活循环,​​它就会卡住程序。这是该段代码的示例:

public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_LEFT && moveL==false){
moveL=true;
moveR=false;
moveU=false;
moveD=false;
while(moveL){
x--;
}
}

请帮我解决这个问题,我已经尝试并四处寻找了好几天了。我很感谢你们能提供的任何帮助。谢谢。

最佳答案

基本概念围绕着“增量”或“变化”值的想法。然后,通过递增或递减状态值,将该值应用于您想要更改的状态。

由于 Swing 的性质,您无法阻止事件调度线程,否则最终会阻止处理传入事件(例如绘制和按键事件)。

同样,您永远不应该尝试从 EDT 之外的任何线程更新任何 UI 组件(或可能影响 UI 的状态变量)。

虽然您可以应用一些技巧来满足这些要求,但最简单的方法是使用 javax.swing.Timer,它会在 EDT 内定期触发 actionPerformed 事件。

发生这种情况时,您将按照规定的数量“更新”所有元素并重新绘制屏幕。

enter image description here enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class PacManTest {

public static void main(String[] args) {
new PacManTest();
}

public PacManTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MazePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class PacMan {

private int x;
private int y;

private int deltaX;
private int deltaY;

private BufferedImage sprite;

public PacMan() {
try {
sprite = ImageIO.read(new File("PacMan.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
}

public void move(int x, int y) {
deltaX = x;
deltaY = y;
}

public void update(MazePane pane) {
x += deltaX;
y += deltaY;
if (x + sprite.getWidth() > pane.getWidth()) {
x = pane.getWidth() - sprite.getWidth();
} else if (x < 0) {
x = 0;
}
if (y + sprite.getHeight() > pane.getHeight()) {
y = pane.getHeight() - sprite.getHeight();
} else if (y < 0) {
y = 0;
}
}

public void paint(MazePane pane, Graphics2D g2d) {
Graphics2D g = (Graphics2D) g2d.create();

float angle = 0;
if (deltaX != 0) {
angle = deltaX > 0 ? 0 : 180;
} else if (deltaY != 0) {
angle = deltaY > 0 ? 90 : 270;
}
AffineTransform t = new AffineTransform();
t.translate(x, y);
t.rotate(Math.toRadians(angle), sprite.getWidth() / 2, sprite.getHeight() / 2);
g.setTransform(t);
g.drawImage(sprite, 0, 0, pane);
g.dispose();
}

}

public class MazePane extends JPanel {

private PacMan pacMan;
public MazePane() {
pacMan = new PacMan();
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pacMan.update(MazePane.this);
repaint();
}
});
timer.start();
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "down");

am.put("left", new MoveAction(pacMan, -4, 0));
am.put("right", new MoveAction(pacMan, 4, 0));
am.put("up", new MoveAction(pacMan, 0, -4));
am.put("down", new MoveAction(pacMan, 0, 4));
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
pacMan.paint(this, g2d);
g2d.dispose();
}

public class MoveAction extends AbstractAction {

private int deltaX;
private int deltaY;
private PacMan pacMan;

public MoveAction(PacMan pacMan, int deltaX, int deltaY) {
this.deltaX = deltaX;
this.deltaY = deltaY;
this.pacMan = pacMan;
}

@Override
public void actionPerformed(ActionEvent e) {
pacMan.move(deltaX, deltaY);
}

}
}
}

我还建议您花时间了解 Key Bindings , KeyListener 遭受焦点问题,哪些键绑定(bind)能够解决...

关于java - 只需按一下按键即可连续移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16911722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com