Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用KeyBindings Java在按住键时停止暂停_Java_Swing_Applet_Key Bindings - Fatal编程技术网

使用KeyBindings Java在按住键时停止暂停

使用KeyBindings Java在按住键时停止暂停,java,swing,applet,key-bindings,Java,Swing,Applet,Key Bindings,我正在尝试用Java编写一个Pong小程序。当用户按住向上或向下时,他们的划桨应该平稳移动,但事实并非如此。它移动,然后暂停,然后再次开始移动。有没有办法阻止这种短暂的停顿 主要类别: public class Main extends JApplet { public DrawPanel dp = new DrawPanel(400, 400); public void init(){ add(dp); setSize(400, 400);

我正在尝试用Java编写一个Pong小程序。当用户按住向上或向下时,他们的划桨应该平稳移动,但事实并非如此。它移动,然后暂停,然后再次开始移动。有没有办法阻止这种短暂的停顿

主要类别:

public class Main extends JApplet {

    public DrawPanel dp = new DrawPanel(400, 400);

    public void init(){
        add(dp);
        setSize(400, 400);
        requestFocusInWindow();

        Action moveDown = new AbstractAction(){
            public void actionPerformed(ActionEvent e){
                dp.player.y += 10;
                dp.repaint();
            }
        };

        dp.getInputMap().put(KeyStroke.getKeyStroke("pressed DOWN"), "move down");
        dp.getActionMap().put("move down", moveDown);       
    }
}
DrawPanel类别:

public class DrawPanel extends JPanel {

    public Paddle player;
    public Paddle opponent;

    public DrawPanel(int height, int width){
        int y = (height / 2) - (height / 10);
        int w = 15;
        int h = height / 5;

        player = new Paddle(2, y, 15, h, Color.white);
        opponent = new Paddle(width - (w+2), y, 15, h, Color.white);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.BLACK);

        g.setColor(player.color);
        g.fillRect(player.x, player.y, player.width, player.height);

        g.setColor(opponent.color);
        g.fillRect(opponent.x, opponent.y, opponent.width, opponent.height);
    }   
}
桨类:

public class Paddle {

    public int x, y, width, height;
    public Color color;

    public Paddle(int _x_, int _y_, int w, int h, Color c){
        x = _x_;
        y = _y_;
        width = w;
        height = h;
        color = c;
    }
}

潜在的问题是按键的“实例”概念和移动的“持续时间”概念之间的阻抗不匹配

不要试图在视图中平滑(无论如何,这与游戏逻辑的细节无关),而是使用更适合的api增强游戏模型。F.i.添加启动/停止逻辑,并将这些方法绑定到按键按下/按键释放:

public class Paddle {

     public void startMoveDown() {
         // here goes the logic, f.i. starting a timer
         // in the actionPerformed of that timer:
         ...  moveUnitDown(); 
     }
     public void stopMoveDown() {
        //... 
     }

     protected void moveUnitDown() {
         y+=unit;
         // ideally, have listeners and notify them the change of y
     } 
}

// in the view:
Action startMoveDown = new AbstractAction(){
    public void actionPerformed(ActionEvent e){
        player.startMoveDown();
   }
};

dp.getInputMap().put(KeyStroke.getKeyStroke("pressed DOWN"), "start move down");
dp.getActionMap().put("start move down", startMoveDown); 

// in the view:
Action stopMoveDown = new AbstractAction(){
    public void actionPerformed(ActionEvent e){
        player.stopMoveDown();
   }
};

dp.getInputMap().put(KeyStroke.getKeyStroke("released DOWN"), "stop move down");
dp.getActionMap().put("stop move down", stopMoveDown); 

试着看一看。基本上,这是在鼠标按下/释放上升起一个标志,并允许更新引擎继续响应这个标志,正如我所需要的,看看计时器解决方案,我想这是我将去的无关:不要改变一个组件的状态,而绘画(在你的画板,这是设置背景),也可以考虑键绑定,插图。我在按键时启动计时器,在按键释放时停止计时器,谢谢