我原本有一个关于键绑定的回答,但经过一些测试,发现它们仍然存在相同的卡顿问题。
不要依赖操作系统的重复率。它在每个平台上可能都不同,用户也可能自定义它。
相反,使用计时器来安排事件。您可以在按下按键时启动计时器,在释放按键时停止计时器。
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.Map;
import java.util.HashMap;
import javax.imageio.ImageIO;
import javax.swing.*;
public class KeyboardAnimation implements ActionListener
{
private final static String PRESSED = "pressed ";
private final static String RELEASED = "released ";
private final static Point RELEASED_POINT = new Point(0, 0);
private JComponent component;
private Timer timer;
private Map<String, Point> pressedKeys = new HashMap<String, Point>();
public KeyboardAnimation(JComponent component, int delay)
{
this.component = component;
timer = new Timer(delay, this);
timer.setInitialDelay( 0 );
}
public void addAction(String keyStroke, int deltaX, int deltaY)
{
InputMap inputMap = component.getInputMap();
ActionMap actionMap = component.getActionMap();
String pressedKey = PRESSED + keyStroke;
KeyStroke pressedKeyStroke = KeyStroke.getKeyStroke( pressedKey );
Action pressedAction = new AnimationAction(keyStroke, new Point(deltaX, deltaY));
inputMap.put(pressedKeyStroke, pressedKey);
actionMap.put(pressedKey, pressedAction);
String releasedKey = RELEASED + keyStroke;
KeyStroke releasedKeyStroke = KeyStroke.getKeyStroke( releasedKey );
Action releasedAction = new AnimationAction(keyStroke, RELEASED_POINT);
inputMap.put(releasedKeyStroke, releasedKey);
actionMap.put(releasedKey, releasedAction);
}
private void handleKeyEvent(String keyStroke, Point moveDelta)
{
if (RELEASED_POINT == moveDelta)
pressedKeys.remove( keyStroke );
else
pressedKeys.put(keyStroke, moveDelta);
if (pressedKeys.size() == 1)
{
timer.start();
}
if (pressedKeys.size() == 0)
{
timer.stop();
}
}
public void actionPerformed(ActionEvent e)
{
moveComponent();
}
private void moveComponent()
{
int componentWidth = component.getSize().width;
int componentHeight = component.getSize().height;
Dimension parentSize = component.getParent().getSize();
int parentWidth = parentSize.width;
int parentHeight = parentSize.height;
int deltaX = 0;
int deltaY = 0;
for (Point delta : pressedKeys.values())
{
deltaX += delta.x;
deltaY += delta.y;
}
int nextX = Math.max(component.getLocation().x + deltaX, 0);
if ( nextX + componentWidth > parentWidth)
{
nextX = parentWidth - componentWidth;
}
int nextY = Math.max(component.getLocation().y + deltaY, 0);
if ( nextY + componentHeight > parentHeight)
{
nextY = parentHeight - componentHeight;
}
component.setLocation(nextX, nextY);
}
private class AnimationAction extends AbstractAction implements ActionListener
{
private Point moveDelta;
public AnimationAction(String keyStroke, Point moveDelta)
{
super(PRESSED + keyStroke);
putValue(ACTION_COMMAND_KEY, keyStroke);
this.moveDelta = moveDelta;
}
public void actionPerformed(ActionEvent e)
{
handleKeyEvent((String)getValue(ACTION_COMMAND_KEY), moveDelta);
}
}
public static void main(String[] args)
{
JPanel contentPane = new JPanel();
contentPane.setLayout( null );
Icon dukeIcon = null;
try
{
dukeIcon = new ImageIcon( "dukewavered.gif" );
}
catch(Exception e)
{
System.out.println(e);
}
JLabel duke = new JLabel( dukeIcon );
duke.setSize( duke.getPreferredSize() );
duke.setLocation(100, 100);
contentPane.add( duke );
KeyboardAnimation navigation = new KeyboardAnimation(duke, 24);
navigation.addAction("LEFT", -3, 0);
navigation.addAction("RIGHT", 3, 0);
navigation.addAction("UP", 0, -3);
navigation.addAction("DOWN", 0, 3);
navigation.addAction("A", -5, 0);
navigation.addAction("S", 5, 0);
navigation.addAction("Z", 0, -5);
navigation.addAction("X", 0, 5);
navigation.addAction("V", 5, 5);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add(contentPane);
frame.setSize(600, 600);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
此代码在Windows上进行了测试,其中事件的顺序为keyPressed,keyPressed,keyPressed... keyReleased。
但是,在Mac(或Unix)上,我认为事件的顺序是keyPressed,keyReleased,keyPressed,keyReleased... 因此,我不确定这段代码是否比您当前的代码更好地工作。