KeyBindings的问题

时间:2014-04-14 03:26:21

标签: java swing awt key-bindings

我已按照指示从KeyListeners切换到KeyBindings,但他们似乎仍无所作为。我的键绑定设置为允许左右箭头键调用setDx()中的paddle.java方法,该方法指示move()方法移动划水盘。

gamePanel.java

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

public class gamePanel extends JPanel implements ActionListener {

paddle Paddle;
boolean ingame = true;
int delay = 1000;
Timer timer;
JLabel text = new JLabel("stuff here");
InputMap im = this.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = this.getActionMap();

public gamePanel() {
    setBackground(Color.WHITE);

    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RightArrow");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LeftArrow");

    add(text);
    text.setBounds(100, 100, 200, 300);

    timer = new Timer(delay, this);
    Paddle = new paddle();
    timer.start();
}

@Override
public void paint(Graphics g) {
    super.paint(g);

    if (ingame) {
        g.drawImage(Paddle.getImage(), Paddle.getX(), Paddle.getY(),
                Paddle.getWidth(), Paddle.getHeight(), this);

    }
}

@Override
public void actionPerformed(ActionEvent ae) {
    Object obj = ae.getSource();

    if (obj == timer) {
        Paddle.move();
        validate();
        repaint();
    }
}

public class ArrowAction extends AbstractAction {

    private String cmd;

    public ArrowAction(String cmd) {
        this.cmd = cmd;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (cmd.equalsIgnoreCase("LeftArrow")) {
            Paddle.setDx(-20);
        } else if (cmd.equalsIgnoreCase("RightArrow")) {
            Paddle.setDx(20);
        } 
    }
}

/*
 @Override
 public void keyPressed(KeyEvent ke) {
 int KeyCode = ke.getKeyCode();
 if (KeyCode == KeyEvent.VK_LEFT) {
 text.setText("key pressed");
 Paddle.setDx(-20);
 }

 if (KeyCode == KeyEvent.VK_RIGHT) {
 Paddle.setDx(20);
 }
 }


 @Override
 public void keyReleased(KeyEvent ke) {
 int KeyCode = ke.getKeyCode();

 if (KeyCode == KeyEvent.VK_LEFT) {
 Paddle.setDx(0);
 }

 if (KeyCode == KeyEvent.VK_RIGHT) {
 Paddle.setDx(0);
 }
 }

 @Override
 public void keyTyped(KeyEvent ke) {

 }
 */
}

Paddle.java

import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;

public class paddle {

    int dx = 0;
    int x, y;
    int height, width;
    Image image;

    public paddle() {

        ImageIcon ii = new ImageIcon("src/Paddle.png");
        image = ii.getImage();
        width = image.getWidth(null);
        height = image.getHeight(null);
        //dx = 20;
        resetState();

    }

    public void setDx(int z) {
        dx = z;
    }

    public void move() {
        x += dx;
        if (x <= 2) {
            x = 2;
        }
        if (x >= (640 - getWidth())) {
            x = (640 - getWidth());
        }
    }

    public void resetState() {
        x = 250;
        y = 375;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getX() {
        return x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    Image getImage() {
        return image;
    }

    Rectangle getRect() {
        return new Rectangle(x, y, image.getWidth(null), image.getHeight(null));
    }
}

2 个答案:

答案 0 :(得分:3)

如果没有调用KeyListener方法,我怀疑它是因为正确的组件没有焦点。有时很难管理哪个组件特别关注游戏,所以我建议切换到使用不需要组件聚焦的key bindings

答案 1 :(得分:2)

您永远不会添加ActionMap

的相应操作
am.put("LeftArrow", new ArrowAction("LeftArrow"));
am.put("RightArrow", new ArrowAction("RightArrow"));

此外,如果您repaint() actioPerformed ArrowActionrepaint(),则不会立即更新,直到Timer为止由{{1}}调用,这不是很长,但仍然是一个微不足道的延迟。