如何通过按键进入ActionMap进行连续运动?

时间:2014-12-17 10:54:30

标签: java swing paintcomponent key-bindings

我的代码中存在问题。我试图做游戏蛇,但我停止了连续运动到drawpanel的ActionMap。我尝试在循环中使用drawPanel.repaint,但只有当它到达结尾时它才显示我,但我想要在ActionMap中给出所有运动。 我怎么能这样做?或者任何人都有另一种解决方案吗?

    DrawPanel drawPanel = new DrawPanel();

    public Snake(){           
        InputMap inputMap = drawPanel.getInputMap();//JPanel.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = drawPanel.getActionMap();                       
        inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
        actionMap.put("rightAction", rightAction);            
        inputMap.put(KeyStroke.getKeyStroke("LEFT"), "leftAction");
        actionMap.put("leftAction", leftAction);            
        inputMap.put(KeyStroke.getKeyStroke("UP"), "upAction");
        actionMap.put("upAction", upAction);            
        inputMap.put(KeyStroke.getKeyStroke("DOWN"), "downAction");
        actionMap.put("downAction", downAction);

        add(drawPanel);            
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private class DrawPanel extends JPanel {

        protected void paintComponent(Graphics g) {                
            super.paintComponent(g);
            g.setColor(Color.GRAY);
                    g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.GREEN);
            g.fillRect(x, y, 10, 10);    
        }
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }             
    }

    Action downAction = new AbstractAction(){
        public void actionPerformed(ActionEvent e) { 
            while (y<390)
            {              
                y +=+10;             
                drawPanel.repaint(); 
            }                
          /*  if (y<390)
                y +=+10; 
            else
                y = 390; 
            drawPanel.repaint();   
            */     
          }
    };

    Action upAction = new AbstractAction(){
        public void actionPerformed(ActionEvent e) {
            if (y>0)
                y -=10;
            else
                y = 0;
            drawPanel.repaint();
        }
    };

      Action leftAction = new AbstractAction(){
        public void actionPerformed(ActionEvent e) {
            if (x>0)
                x -=10;
            else
                x = 0;
            drawPanel.repaint();
        }
    };

    Action rightAction = new AbstractAction(){
        public void actionPerformed(ActionEvent e) {
            if (x<390)
                x +=10;
            else
                x = 390;
            drawPanel.repaint();
        }
    };    

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new Snake();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:2)

herehere所示,您可以在javax.swing.TimeractionPerformed()中唤起自己Action个实例的ActionListener方法。您可以调整计时器delay以调整动画速度。

答案 1 :(得分:0)

你应该使用一个总是重新绘制屏幕的游戏循环,而不是按下按钮时。而且,你应该保持按钮的统计数据。像“isDownPressing”之类的东西。并且,您应该在“paintComponent”函数中进行计算。顺便说一句,按下按钮需要回调,然后释放按钮。