首页 > 解决方案 > 如何设置切换布尔变量状态的按键检测

问题描述

如何设置按键检测来切换布尔变量的状态,我的 GCTester 类的属性在paused哪里paused,初始状态为false

// GCTester demonstrates how we can override the GameCore class
// to create our own 'game'. We usually need to implement at
// least 'draw' and 'update' (not including any local event handling)
// to begin the process. You should also write your own 'init'
// method that will initialise event handlers etc. By default GameCore
// will handle the 'Escape' key to quit the game but you should
// override this with your own event handler.

public class GCTester extends GameCore {

    private Animation anim;
    private Sprite rock;
    long total;         // Total time elapsed
    boolean paused;

    // The obligatory main method that creates
    // an instance of our GCTester class and
    // starts it running
    public static void main(String[] args) {
        GCTester gct = new GCTester();
        gct.init();
        // Start in windowed mode with a 800x600 screen
        gct.run(false, 800, 600);
    }

    // Initialise the class, e.g. set up variables
    // animations, register event handlers
    public void init() {
        total = 0;
        Image player = loadImage("images/rock.png");
        anim = new Animation();
        anim.addFrame(player, 20);

        rock = new Sprite(anim);

        rock.setX(100);
        rock.setY(100);
        rock.setVelocityX(0.1f);
        rock.setVelocityY(0.1f);
        rock.show();
    }

    // Draw the current frame
    public void draw(Graphics2D g) {
        // A simple demo - note that this is not
        // very efficient since we fill the screen
        // on every frame.
        g.setColor(Color.black);
        g.fillRect(0, 0, 800, 600);
        g.setColor(Color.yellow);
        g.drawString("Time Expired:" + total, 30, 50);

        rock.draw(g);
    }

    // Update any sprites and check for collisions
    public void update(long elapsed) {
        if (paused) return;
        total += elapsed;
        if (total > 60000) stop();
        rock.update(elapsed);
    }

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_ESCAPE)
            stop(); // Call GameCore stop method to stop the game loop
        else {
            if (keyCode == KeyEvent.VK_S) rock.stop();
            if (keyCode == KeyEvent.VK_G) rock.setVelocityX(0.1f);

            e.consume();
        }
    }
}

标签: javaswingjava-2d

解决方案


您应该使用窗口组件在屏幕上显示您的应用程序。假设它是 a JFrame(或者它可以是 a JDialog)。创建一个JComponent将“侦听”关键操作的函数,将其添加到您的jFrame或您使用的任何其他上层窗口组件中:

JComponent controlComp = new JComponent() {};
controlComp.setBounds(0, 0, 800, 600); // your bounds
controlComp.setFocusTraversalKeysEnabled(false);
jFrame.add(controlComp);

ActionListener keyEscListener = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent) {
        paused = !paused;
        // your logic for Escape
    }
};

// tie KeyStroke and keyboard action to the controlComp
KeyStroke keyEsc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
controlComp.registerKeyboardAction(keyEscListener, keyEsc, JComponent.WHEN_FOCUSED);

此方法需要为您要侦听的每个键创建单独的 Listener 和 KeyStroke 对象


推荐阅读