- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试用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
事件。
发生这种情况时,您将按照规定的数量“更新”所有元素并重新绘制屏幕。
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/
我有一个只是 HTML 页面的应用程序。这会从另一台服务器调用 API 来获取/发送数据。这是我的问题。 我们如何维护登录用户的 session ?那就是,怎样才能阻止他在不登录的情况下访问其他页面进
这个问题在这里已经有了答案: Margin on child element moves parent element (18 个答案) 关闭 6 年前。 好吧,现在我遇到了这个问题,我只想调整“c
话说人生苦短,我用Python。 如果学python不是为了下载这些视频,那将毫无意义! 啊呸,老色批 咳咳,我们开始正题。 一、事前前奏 首先没装Python、pycharm的铁子先自己安装一下,我
问题陈述: 给定两张图像,例如下面布拉德·皮特的两张图像,请判断图像中是否包含同一个人。困难在于,我们每个人只有一张引用图像,并且如何确定任何其他传入图像是否包含同一个人或没有。 一些研究: 有几种不
我们的新闻源遇到了问题 - 它本质上是一个 Likebox 插件,可以节省更新网站和 Facebook 页面的麻烦。 Facebook 页面使用 iframe 填充新闻源。如果网站的浏览者未登录 Fa
任何人都可以给我一些关于如何使 javascript 计数计时器在 5 分钟后停止的小提示,我尝试过的代码低于我使用的代码,但我只想只用 5 分钟它在 5 分钟后停止。 var sec =
有没有办法为某些内容增加 View 表,或者如果不存在,则添加第一个 View 而不运行 2 个查询。这是我当前有效的代码。 function increment_views($id = null)
是否可以获取用户从天气应用程序中输入的位置(例如“马德里”)的 7 天天气预报,然后将其保存以供以后科学使用?在最好的情况下,我希望稍后有一个 if 语句,如下所示: #define sunny 1
根据外媒 9to5google 的消息,小米电视棒现已通过 FCC 的审批,可能很快在美国上市。 IT之家了解到,这款 FCC 认证的小米电视棒的型号为 MITVMDZ24AA,是两款小米 M
Visual Studio 2010 - 发布版 - 打开一个 xaml 文件大约需要 5 秒钟。 它在某些机器上执行,而不是所有机器! .cs 文件立即打开。它只发生在 XAML 上。 我们尝试了以
我正在解决“HackerRank”页面上的一个问题,特别是名为“追加和删除”的问题,但我无法使所有情况都正确。 https://www.hackerrank.com/challenges/append
我正在尝试从头开始构建幻灯片放映。我只想使用 javascript,而不使用 jquery。 到目前为止,当我单击“上一页”按钮时,它只会向左 move 50px 一次。如何让它每次单击“上一个”按钮
我有一个 Ionic 应用程序,我希望它连接到我的套接字。这在 SignalR 预览中工作得很好,它基本上仍然有效,但由于某种原因需要 2 分钟才能连接...... 连接时我也遇到一些错误: 这是我的
我对 Silverlight 和 MVVM/Prism 模式相当陌生,所以这可能是一个愚蠢的问题。 我有一个 View ,其中包含自定义控件。这些自定义控件实际上也是 View ,并且有 ViewMo
我在 python 上使用一些 DataFrame,我遇到了一种情况,我必须在相同的 if 条件下同时更改 2 列。我解决了我的问题,但我并没有只用一个 if 条件。 我试图搜索一些相关内容,但我只在
为什么scanf()在读取double时需要“%lf”中的l ,当 printf() 可以使用“%f”时,无论其参数是 double 还是 float ? 示例代码: double d; scanf(
为什么scanf()在读取double时需要“%lf”中的l ,当 printf() 可以使用“%f”时,无论其参数是 double 还是 float ? 示例代码: double d; scanf(
我是一名优秀的程序员,十分优秀!