如何在Java中根据按键使图像移动?
如何在 Java 中聆听按键时使图像移动
问题:
我正在学习 Java编程,我想创建一个游戏,在按下按键时图像左右移动。我已经可以绘制图像并监听按键,但是当窗口监听按键时,如何使图像在窗口内来回移动?例如,如果我按空格键,激光应该从屏幕底部发射。
答案:
是的,可以使图像移动使用 Swing Timer 和 Key Bindings 在窗口内来回移动,同时监听按键。下面是一个示例:
import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.swing.*; public class AnimationWithKeyBinding { private static void createAndShowUI() { AnimationPanel panel = new AnimationPanel(); // the drawing JPanel JFrame frame = new JFrame("Animation With Key Binding"); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } @SuppressWarnings("serial") class AnimationPanel extends JPanel { public static final int SPRITE_WIDTH = 20; public static final int PANEL_WIDTH = 400; public static final int PANEL_HEIGHT = 400; private static final int MAX_MSTATE = 25; private static final int SPIN_TIMER_PERIOD = 16; private static final int SPRITE_STEP = 3; private int mState = 0; private int mX = (PANEL_WIDTH - SPRITE_WIDTH) / 2; private int mY = (PANEL_HEIGHT - SPRITE_WIDTH) / 2; private int oldMX = mX; private int oldMY = mY; private boolean moved = false; // an array of sprite images that are drawn sequentially private BufferedImage[] spriteImages = new BufferedImage[MAX_MSTATE]; public AnimationPanel() { // create and start the main animation timer new Timer(SPIN_TIMER_PERIOD, new SpinTimerListener()).start(); setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT)); setBackground(Color.white); createSprites(); // create the images setupKeyBinding(); } private void setupKeyBinding() { int condition = JComponent.WHEN_IN_FOCUSED_WINDOW; InputMap inMap = getInputMap(condition); ActionMap actMap = getActionMap(); // this uses an enum of Direction that holds ints for the arrow keys for (Direction direction : Direction.values()) { int key = direction.getKey(); String name = direction.name(); // add the key bindings for arrow key and shift-arrow key inMap.put(KeyStroke.getKeyStroke(key, 0), name); inMap.put(KeyStroke.getKeyStroke(key, InputEvent.SHIFT_DOWN_MASK), name); actMap.put(name, new MyKeyAction(this, direction)); } } // create a bunch of buffered images and place into an array, // to be displayed sequentially private void createSprites() { for (int i = 0; i < spriteImages.length; i++) { spriteImages[i] = new BufferedImage(SPRITE_WIDTH, SPRITE_WIDTH, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = spriteImages[i].createGraphics(); g2.setColor(Color.red); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); double theta = i * Math.PI / (2 * spriteImages.length); double x = SPRITE_WIDTH * Math.abs(Math.cos(theta)) / 2.0; double y = SPRITE_WIDTH * Math.abs(Math.sin(theta)) / 2.0; int x1 = (int) ((SPRITE_WIDTH / 2.0) - x); int y1 = (int) ((SPRITE_WIDTH / 2.0) - y); int x2 = (int) ((SPRITE_WIDTH / 2.0) + x); int y2 = (int) ((SPRITE_WIDTH / 2.0) + y); g2.drawLine(x1, y1, x2, y2); g2.drawLine(y1, x2, y2, x1); g2.dispose(); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(spriteImages[mState], mX, mY, null); } public void incrementX(boolean right) { oldMX = mX; if (right) { mX = Math.min(getWidth() - SPRITE_WIDTH, mX + SPRITE_STEP); } else { mX = Math.max(0, mX - SPRITE_STEP); } moved = true; } public void incrementY(boolean down) { oldMY = mY; if (down) { mY = Math.min(getHeight() - SPRITE_WIDTH, mY + SPRITE_STEP); } else { mY = Math.max(0, mY - SPRITE_STEP); } moved = true; } public void tick() { mState = (mState + 1) % MAX_MSTATE; } private class SpinTimerListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { tick(); int delta = 20; int width = SPRITE_WIDTH + 2 * delta; int height = width; // make sure to erase the old image if (moved) { int x = oldMX - delta; int y = oldMY - delta; repaint(x, y, width, height); } int x = mX - delta; int y = mY - delta; // draw the new image repaint(x, y, width, height); moved = false; } } } enum Direction { UP(KeyEvent.VK_UP), DOWN(KeyEvent.VK_DOWN), LEFT(KeyEvent.VK_LEFT), RIGHT(KeyEvent.VK_RIGHT); private int key; private Direction(int key) { this.key = key; } public int getKey() { return key; } } // Actions for the key binding @SuppressWarnings("serial") class MyKeyAction extends AbstractAction { private AnimationPanel draw; private Direction direction; public MyKeyAction(AnimationPanel draw, Direction direction) { this.draw = draw; this.direction = direction; } @Override public void actionPerformed(ActionEvent e) { switch (direction) { case UP: draw.incrementY(false); break; case DOWN: draw.incrementY(true); break; case LEFT: draw.incrementX(false); break; case RIGHT: draw.incrementX(true); break; default: break; } } }
在此示例中,AnimationPanel 类扩展了 JPanel 并处理绘图和动画。 setupKeyBinding() 方法设置箭头键的键绑定,MyKeyAction 类处理按键事件并相应地移动图像。
通过使用键绑定和 Swing Timer 的组合,您可以对象在监听按键的同时在窗口内来回移动。
以上是如何在Java中根据按键使图像移动?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本启动Spring...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

Redis缓存方案如何实现产品排行榜列表的需求?在开发过程中,我们常常需要处理排行榜的需求,例如展示一个�...
