java - 在按键时启动计时器,java

标签 java timer keypress

我想,当我单击“d”按钮时,启动一个计时器。这是为了动画玩家行走。当我按下按键时计时器没有启动,我该怎么办?

我的代码是这样的:

public class Game extends JPanel implements KeyListener {
//Player variables
private BufferedImage playerStanding;
private BufferedImage playerWalking;
private BufferedImage playerFrame;
private boolean walking = false;
private final int PLAYER_HEIGHT = 100;
private final int PLAYER_WIDTH = 100;
private final int INITIAL_X = 0;
private final int INITIAL_Y = 500;
private int x = INITIAL_X;
private int y = INITIAL_Y;
//The timer I want to start on keypress-> "d"
private Timer playerAnimationTimer;



public Game() {
    setPreferredSize(new Dimension(800, 800));
    setBackground(Color.CYAN);
    //Player
    try {
        playerStanding = ImageIO.read(getClass().getResource("player1.gif"));
        playerWalking = ImageIO.read(getClass().getResource("player2.gif"));
        playerFrame = playerStanding;


    }
    catch (IOException ex) {
        ex.printStackTrace();
    }

    playerAnimationTimer = new Timer(500, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            walking = !walking;
            playerFrame = walking ? playerWalking : playerStanding;
            x += 10;
            if (x > getWidth() - PLAYER_WIDTH) {
                x = INITIAL_X;
            }
            repaint();
        }
    });
    playerAnimationTimer.setRepeats(true);
}

public Dimension setPreferredSize() {
    return new Dimension(800, 800);
}



@Override
public void paintComponent(Graphics graphics) {
    super.paintComponent(graphics);

    Graphics2D graphics2D = (Graphics2D) graphics;
    if (playerFrame != null) {
        graphics2D.drawImage(playerFrame, x, y, PLAYER_WIDTH, PLAYER_HEIGHT, this);
    }


    graphics2D.dispose();
}

@Override
public void keyTyped(KeyEvent e) {
    //This doesn't work
    playerAnimationTimer.start();
}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {

}
}
//The class to hold the gamepanel
public class StartGame extends JFrame implements ActionListener {
private static JButton startGame = new JButton();

StartGame() {
    setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
    setSize(200, 100);
    setVisible(true);
    setBackground(Color.BLUE);
    setLocationRelativeTo(null);

    startGame.setText("Play!");
    startGame.setSize(100, 25);
    startGame.addActionListener(this);
    add(startGame);
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == startGame) {
        this.setVisible(false);
        new GameWindow();
    }
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new StartGame();
        }
    });
}
}

如何让计时器在单击“d”按钮时启动?

最佳答案

您的 KeyListener 不起作用,因为您从未将 KeyListener 添加到任何具有焦点的组件中,而焦点是 KeyListener 工作所必需的。

我建议您改用按键绑定(bind)作为一种更干净、更安全的方式来捕获所需的按键。

顺便说一句,永远不要处置从 JVM 提供给您的 Graphics 对象。

为了获得更好的答案,请编辑您的代码以使其符合 mcve标准。您不应该使用任何图像文件,并且它应该不加更改地为我们编译和运行。

关于java - 在按键时启动计时器,java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21801180/

相关文章:

java - 如何在 SOLR 连接中从两个核心获取所有数据

java - 代号一中的 HTML 解析,无需使用 XML 解析器

java - 如何通过网络管理计时器/计时器任务

python - 如何在 Linux 终端上进行按键检测,python 中的低级样式

java - 使用 System.arraycopy 将 double[] 转换为 Double[] 和 Viceversa,Java

java - 如何内存长度为 n 的递归路径搜索

r - R Shiny 的倒数计时器?

java - 如何在连接四中添加定时器? java

python - 使用 python(后台守护进程)检查是否按下了键

javascript - 触发多个按键来刺激键盘快捷键jquery