gpt4 book ai didi

java - 用于多个箭头键按下的 KeyBinding

转载 作者:行者123 更新时间:2023-11-30 08:04:43 25 4
gpt4 key购买 nike

这段代码适合多次按下箭头键吗?现在正在工作。向右走,向左走。但是,如果我同时按下向左和向右箭头键,我希望该桨停止。我怎样才能做到这一点 ?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class GamePanel extends JPanel {

// ENUM
public enum HorizontalMovement {
NONE,
LEFT,
RIGHT
}

// CONSTANTS
private static final int WIDTH = 600;
private static final int HEIGHT = 500;
private static final int PADDLE_Y = 475;
private static final int PADDLE_W = 80;
private static final int PADDLE_H = 20;
private static final int PADDLE_ARC = 15;

// PROPERTIES
int x = 0;
private HorizontalMovement horizontalMovement = HorizontalMovement.NONE;
private Timer timer;

// CONSTRUCTOR
public GamePanel() {

setSize(WIDTH, HEIGHT);
setBackground(new Color(240, 255, 255));

// right pressed

KeyStroke ks1 = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false);

InputMap im1 = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am1 = getActionMap();

im1.put(ks1, "right.pressed");
am1.put("right.pressed", new MoveHorizontialAction(HorizontalMovement.RIGHT));


// right released

KeyStroke ks2 = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true);

InputMap im2 = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am2 = getActionMap();

im2.put(ks2, "right.relesed");
am2.put("right.relesed", new MoveHorizontialAction(HorizontalMovement.NONE));

// left pressed

KeyStroke ks3 = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false);

InputMap im3 = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am3 = getActionMap();

im3.put(ks3, "left.pressed");
am3.put("left.pressed", new MoveHorizontialAction(HorizontalMovement.LEFT));

// left released

KeyStroke ks4 = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true);

InputMap im4 = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am4 = getActionMap();

im4.put(ks4, "left.relesed");
am4.put("left.relesed", new MoveHorizontialAction(HorizontalMovement.NONE));


// each 15 ms
timer = new Timer(15, new TimeListener());
timer.start();

}

// PADDLE
@Override
protected void paintComponent(Graphics g) {

super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(new Color(201, 51, 51));
g.fillRoundRect(x, PADDLE_Y, PADDLE_W, PADDLE_H, PADDLE_ARC, PADDLE_ARC);
}

// Time Listener

protected class TimeListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {

if ( horizontalMovement == HorizontalMovement.RIGHT )
x = x + 2;

if ( horizontalMovement == HorizontalMovement.LEFT )
x = x - 2;

if ( x < 0 )
x = 0;
else if ( x > 520 )
x = 520;

repaint();
}

}

// ACTION FOR ActionMap
protected class MoveHorizontialAction extends AbstractAction {

private HorizontalMovement movement;

public MoveHorizontialAction(HorizontalMovement movement) {
this.movement = movement;
}

@Override
public void actionPerformed(ActionEvent e) {
horizontalMovement = movement;
}
}
}

主要代码:

import java.awt.*;
import javax.swing.*;

public class MainFrame extends JFrame{

public MainFrame() {

// size
setSize(600, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);


// remove borders
setUndecorated(true);
getRootPane().setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, new Color(63, 134, 145)));

getContentPane().add( new GamePanel());

setVisible(true);
}

public static void main(String[] args) {

new MainFrame();
}
}

最佳答案

仅当链接方向处于 NONE 状态时,或者应该处于 NONE 状态时,情况如何

如果按向左键,则会是:

NONE->LEFT, LEFT

然后按右键:

LEFT->NONE, RIGHT

所以它会停止

@Override
public void actionPerformed(ActionEvent e) {
if(horizontalMovement==HorizontalMovement.NONE ||
movement==HorizontalMovement.NONE){
horizontalMovement = movement;
}else{
horizontalMovement = HorizontalMovement.NONE;
}
}

未经测试,只是一个想法

编辑固定平滑

为了平滑运动,我使用了运动哈希集,其中包含所有 Activity 运动,因此如果它包含两个运动(右+左),则将使用两个运动,从而导致此情况:

if ( movements.contains(HorizontalMovement.RIGHT) )
x = x + 2;
if ( movements.contains(HorizontalMovement.LEFT) )
x = x - 2;

两个条件都满足,x 保持x=(x+2)-2

此处为完整代码(类 GamePanel、MainFrame,无需编辑):

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.HashSet;
public class GamePanel extends JPanel {

// ENUM
public enum HorizontalMovement {
NONE,
LEFT,
RIGHT
}

// CONSTANTS
private static final int WIDTH = 600;
private static final int HEIGHT = 500;
private static final int PADDLE_Y = 475;
private static final int PADDLE_W = 80;
private static final int PADDLE_H = 20;
private static final int PADDLE_ARC = 15;
private HashSet<HorizontalMovement> movements = new HashSet<HorizontalMovement>();

// PROPERTIES
int x = 0;
private Timer timer;

// CONSTRUCTOR
public GamePanel() {

setSize(WIDTH, HEIGHT);
setBackground(new Color(240, 255, 255));

// right pressed

KeyStroke ks1 = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false);

InputMap im1 = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am1 = getActionMap();

im1.put(ks1, "right.pressed");
am1.put("right.pressed", new MoveHorizontialAction(true,HorizontalMovement.RIGHT));

// right released
KeyStroke ks2 = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true);
InputMap im2 = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am2 = getActionMap();

im2.put(ks2, "right.relesed");
am2.put("right.relesed", new MoveHorizontialAction(false,HorizontalMovement.RIGHT));

// left pressed

KeyStroke ks3 = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false);

InputMap im3 = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am3 = getActionMap();

im3.put(ks3, "left.pressed");
am3.put("left.pressed", new MoveHorizontialAction(true,HorizontalMovement.LEFT));

// left released

KeyStroke ks4 = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true);

InputMap im4 = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am4 = getActionMap();

im4.put(ks4, "left.relesed");
am4.put("left.relesed", new MoveHorizontialAction(false,HorizontalMovement.LEFT));

// each 15 ms
timer = new Timer(15, new TimeListener());
timer.start();

}

// PADDLE
@Override
protected void paintComponent(Graphics g) {

super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(new Color(201, 51, 51));
g.fillRoundRect(x, PADDLE_Y, PADDLE_W, PADDLE_H, PADDLE_ARC, PADDLE_ARC);
}

// Time Listener

protected class TimeListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {

if ( movements.contains(HorizontalMovement.RIGHT) )
x = x + 2;

if ( movements.contains(HorizontalMovement.LEFT) )
x = x - 2;

if ( x < 0 )
x = 0;
else if ( x > 520 )
x = 520;

repaint();
}

}

// ACTION FOR ActionMap
protected class MoveHorizontialAction extends AbstractAction {

private HorizontalMovement movement;
private boolean add=true;
public MoveHorizontialAction(boolean add,HorizontalMovement movement) {
this.movement = movement;
this.add = add;
}

@Override
public void actionPerformed(ActionEvent e) {
if(add){
//System.out.println("Add: "+movement.name());
movements.add(movement);
}else{
//System.out.println("Rem: "+movement.name());
movements.remove(movement);
}
}
}
}

不要害怕询问有关此代码的问题

关于java - 用于多个箭头键按下的 KeyBinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31301600/

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