运行一段时间后KeyInput将无法工作(JCreator,Applet)

时间:2014-05-26 17:25:14

标签: java awt paint keylistener jcreator

据说每当你按一个键时,它应该访问代码,但它没有,我或我的老师都不知道为什么。这是代码:

class KeyInput implements KeyListener{
Actor player;
Graphics gBuffer;

public KeyInput(Actor player, Graphics gBuffer){
    JTextField typingArea = new JTextField(20);
    typingArea.addKeyListener(this);
    this.player = player;
    this.gBuffer = gBuffer;
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT) {
        player.modActor(1);
        gBuffer.drawString("ENTER",150,100);
    }
    if (key == KeyEvent.VK_RIGHT) {
        player.modActor(2);
        gBuffer.drawString("ENTER",150,100);
    }
    if (key == KeyEvent.VK_UP) {
        player.modActor(3);
        gBuffer.drawString("ENTER",150,100);
    }
    if (key == KeyEvent.VK_DOWN) {
        player.modActor(4);
        gBuffer.drawString("ENTER",150,100);
    }
}

}

这是在节目期间播放的时间:

   public void paint(Graphics g){ //main method to be called to play(g);
    do{
        drawBackground(gBuffer);
        gBuffer.setFont(new Font("Calibri",Font.BOLD,20));
        gBuffer.setColor(Color.CYAN);
        gBuffer.drawString("Score: " + (int)score,18,20);
        gBuffer.drawString("Score: " + (int)score,22,20);
        gBuffer.drawString("Score: " + (int)score,20,22);
        gBuffer.drawString("Score: " + (int)score,20,18);
        gBuffer.setColor(Color.BLACK);
        gBuffer.drawString("Score: " + (int)score,20,20);
        play(gBuffer);
        g.drawImage(virtualMem,0,0,this);
        if(lose==false){
            gBuffer.setFont(new Font("Calibri",Font.BOLD,50));
            gBuffer.setColor(Color.CYAN);
            gBuffer.drawString("GAME OVER",42,200);
            gBuffer.drawString("GAME OVER",38,200);
            gBuffer.drawString("GAME OVER",40,202);
            gBuffer.drawString("GAME OVER",40,198);
            gBuffer.setColor(Color.BLACK);
            gBuffer.drawString("GAME OVER",40,200);
            gBuffer.drawString("Final Score: " + (int)score,20,260);
        }
    }while(lose);

}

关键输入需要移动我拥有的actor,但它根本不会移动,是的,我每次都会在applet中单击。什么事情都在发生?

编辑:我现在已经放弃了keyListener并尝试了keyBindings,但我对他们很新,可能有些不对劲,请帮助我...

public void init() {
    panel = new JPanel();
    this.setSize(400,700);
    this.setFocusTraversalKeysEnabled(false);
    this.addKeyListener(this);
    player = new Actor(180,500);
    virtualMem = createImage(400,800);
    gBuffer = virtualMem.getGraphics();

    panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "up");
    panel.getActionMap().put("up", new AbstractAction(){
        public void actionPerformed(ActionEvent e) {
         gBuffer.drawString("IT WORKS", 150, 150);
         repaint();
        }
    });

    panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "down");
    panel.getActionMap().put("down", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
         gBuffer.drawString("IT WORKS", 150, 150);
         repaint();
        }
    });

    panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "left");
    panel.getActionMap().put("left", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
         gBuffer.drawString("IT WORKS", 150, 150);
         repaint();
        }
    });

    panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "right");
    panel.getActionMap().put("right", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
         gBuffer.drawString("IT WORKS", 150, 150);
         repaint();
        }
    });

}

时间仍然相同(播放)。当我点击某些东西时,它也不会访问这些方法。

1 个答案:

答案 0 :(得分:2)

恐怕也许paint()内部的无限循环干扰了事件管理(请记住,有两个事情可以完成)。你为什么不在每个重大事件上发出repaint()(例如,按键)?