在按下键绑定时,Java不断移动Sprite

时间:2015-01-17 19:31:07

标签: java swing jpanel sprite key-bindings

我创建了一个程序来创建一个正方形并在JPanel上绘制它。还有KeyBindings控制方块的移动(w向上)(s向下)(a为左)(d为右)。我的问题是,当我按下按键时,它会移动一个实例,犹豫不决,然后继续移动。有没有办法防止运动的犹豫。

import javax.swing.JFrame;


public class MovingBox extends JFrame{

Panel panel;

public static void main(String args[]){

    MovingBox mb = new MovingBox();
}

public MovingBox(){

    super("Moving Box");
    setSize(800, 600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    panel = new Panel();
    add(panel);

    setVisible(true);
}
}

这是我的Panel类。

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;


public class Panel extends JPanel{

Rectangle box;

public Panel(){

    box = new Rectangle(100, 100, 50, 50);

    getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
            KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "move left");
    getActionMap().put("move left", new MoveAction(3, 3));

    getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
            KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "move right");
    getActionMap().put("move right", new MoveAction(1, 3));

    getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
            KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "move up");
    getActionMap().put("move up", new MoveAction(0, 3));

    getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
            KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "move down");
    getActionMap().put("move down", new MoveAction(2, 3));
}

public void paintComponent(Graphics window){

    super.paintComponent(window);

    window.fillRect((int)box.getX(), (int)box.getY(), (int)box.getWidth(), (int)box.getHeight());
}

private class MoveAction extends AbstractAction{

    int direction;
    int speed;

    public MoveAction(int direction, int speed){

        this.direction = direction;
        this.speed = speed; 
    }

    public void actionPerformed(ActionEvent e){

        if(direction == 0){

            box.setLocation((int)box.getX(), (int)box.getY() - speed);

        }else if(direction == 1){

            box.setLocation((int)box.getX() - speed, (int)box.getY());

        }else if(direction == 2){

            box.setLocation((int)box.getX(), (int)box.getY() + speed);

        }else if(direction == 3){

            box.setLocation((int)box.getX() + speed, (int)box.getY());
        }

        repaint();
    }
}
}

非常感谢任何帮助。谢谢。

2 个答案:

答案 0 :(得分:0)

要获得平稳的移动,您需要创建一个处理移动的线程。

为每个键(w,a,s,d)创建一个布尔变量,并在按下该键时将其设置为true,并在释放该键时将其设置为false。只是一个简单的关键听众。 别忘了将监听器添加到jframe并检查jframe是否具有焦点。

private class InputListener implements KeyListener{
    @Override
    public void keyPressed(KeyEvent pressed) {

        //arrow keys
        if(pressed.getKeyCode() == 37){
            arrowLeft = true;
        }else if(pressed.getKeyCode() == 38){
            arrowUp = true;
        }else if(pressed.getKeyCode() == 39){
            arrowRight = true;
        }else if(pressed.getKeyCode() == 40){
            arrowDown = true;
        }

        //System.out.println(pressed.getKeyCode());
    }
    @Override
    public void keyReleased(KeyEvent released) {
        if(released.getKeyCode() == 37){
            arrowLeft = false;
        }
        if(released.getKeyCode() == 38){
            arrowUp = false;
        }
        if(released.getKeyCode() == 39){
            arrowRight = false;
        }
        if(released.getKeyCode() == 40){
            arrowDown = false;
        }
    }
    @Override
    public void keyTyped(KeyEvent arg0) {}
}

我用箭头键进行移动,所以对于你需要更改键码的移动。你需要的第二件事是如果按下一个键就移动方块的后台线程。它根据速度和时间计算运动,使其完全平滑。

long currTime = 0;
long lastTime = 0;

new Thread() {
    @Override
      public void run() {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        while (true) {
           currTime = System.currentTimeMillis();
           long time = currTime - lastTime;

           int movePixel = (int) (speed * time);

           if (movePixel < 1 ) {
                            currPlayerTime -= time;
           }else{
               if(arrowLeft){
                     //move left by movePixel pixel
               }else if(arrowRight){
                     //move right by movePixel pixel
               }
               if(arrowUp){

               }else if(arrowDown){

               }     
             }

            lastTime = currTime;
        }
    }
}.start();

这可能不是最简单,最快速的解决方案,但它绝对是最好的:)

答案 1 :(得分:0)

  

有没有办法阻止运动的犹豫。

您可以使用Swing Timer来安排动画。查看Motion Using the Keyboard了解工作示例。