Java에서 Keypress를 들으면서 이미지를 이동시키는 방법
Java에서는 Swing Timer와 Key를 조합하여 사용할 수 있습니다. 키 누르기를 듣는 동안 이미지를 움직이게 하는 바인딩입니다. 방법은 다음과 같습니다.
예시 코드:
다음은 스윙 타이머와 키 바인딩을 사용하여 키 누르기를 듣는 동안 이미지를 이동시키는 방법을 보여주는 예제 코드입니다.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ImageMoveOnKeyPress extends JPanel implements ActionListener { private Image image; // The image to be moved private int x, y; // The current position of the image private Timer timer; // The Swing Timer to update the image position private KeyBindings keyBindings; // The Key Bindings to detect keypresses public ImageMoveOnKeyPress() { // Load the image image = Toolkit.getDefaultToolkit().getImage("image_path"); // Set the initial position of the image x = 50; y = 50; // Create a Swing Timer to update the image position timer = new Timer(10, this); // 10 milliseconds delay timer.start(); // Create Key Bindings to detect keypresses keyBindings = new KeyBindings(); getInputMap().put(KeyEvent.VK_LEFT, "left"); getInputMap().put(KeyEvent.VK_RIGHT, "right"); getInputMap().put(KeyEvent.VK_UP, "up"); getInputMap().put(KeyEvent.VK_DOWN, "down"); getActionMap().put("left", keyBindings.new MoveLeftAction()); getActionMap().put("right", keyBindings.new MoveRightAction()); getActionMap().put("up", keyBindings.new MoveUpAction()); getActionMap().put("down", keyBindings.new MoveDownAction()); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, x, y, null); } @Override public void actionPerformed(ActionEvent e) { // Update the image position based on keypress if (keyBindings.isLeftPressed()) { x -= 1; } else if (keyBindings.isRightPressed()) { x += 1; } else if (keyBindings.isUpPressed()) { y -= 1; } else if (keyBindings.isDownPressed()) { y += 1; } // Repaint the panel to display the moved image repaint(); } private class KeyBindings { private boolean leftPressed, rightPressed, upPressed, downPressed; public boolean isLeftPressed() { return leftPressed; } public void setLeftPressed(boolean leftPressed) { this.leftPressed = leftPressed; } public boolean isRightPressed() { return rightPressed; } public void setRightPressed(boolean rightPressed) { this.rightPressed = rightPressed; } public boolean isUpPressed() { return upPressed; } public void setUpPressed(boolean upPressed) { this.upPressed = upPressed; } public boolean isDownPressed() { return downPressed; } public void setDownPressed(boolean downPressed) { this.downPressed = downPressed; } private class MoveLeftAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { leftPressed = true; } } private class MoveRightAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { rightPressed = true; } } private class MoveUpAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { upPressed = true; } } private class MoveDownAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { downPressed = true; } } } public static void main(String[] args) { JFrame frame = new JFrame();
위 내용은 키 누름에 따라 Java에서 이미지를 이동하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!