在JPanel中移动图像

时间:2017-05-02 07:38:21

标签: java swing

我目前正试图通过选择一些键绑定来移动我导入的图像。我已经做了一些测试,增加了它所经过的像素数量,但它似乎只设置了它的x和y值不用它来衡量速度。

这是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Ship1 extends JPanel implements ActionListener, KeyListener {

    private final static String IMAGE_NAME = "ship1_";
    protected ImageIcon ship1[];
    private final int TOTAL_IMAGES = 16;
    private int currentIMAGE = 0;
    private final int ANIMATION_DELAY = 100;
    private int width;
    private int height;
    private int x = 0;
    private int y = 0;
    private int velX = 0;
    private int velY = 0;

    private Timer animationTimer;

    public Ship1() {

        ship1 = new ImageIcon[TOTAL_IMAGES];

        for (int count = 0; count < ship1.length; count++) {
            ship1[count] = new ImageIcon(getClass().getResource("Images/Ship1_/" + IMAGE_NAME + count + ".png"));

            width = ship1[count].getIconWidth();
            height= ship1[count].getIconHeight();
        }

        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public void paintComponent (Graphics g) {
        super.paintComponent(g);

        ship1[currentIMAGE].paintIcon(this, g, x, y);

        if (animationTimer.isRunning()) {
            currentIMAGE = (currentIMAGE + 1) % TOTAL_IMAGES;
        }
    }

    public void startAnimation() {
        if (animationTimer == null) {
            currentIMAGE = 0;
            animationTimer = new Timer(ANIMATION_DELAY, new TimerHandler());

            animationTimer.start();
        } else {
            if (!animationTimer.isRunning()) {
                animationTimer.restart();
            }
        }
    }

    public void stopAnimation() {
        animationTimer.stop();
    }

    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }

    private class TimerHandler implements ActionListener {

        public void actionPerformed(ActionEvent actionEvent) {
            repaint();
        }
    }

    public void actionPerformed(ActionEvent e){
        if (x < 0) {
            velX = 0;
            x = 0;
        }
        if (x > 850) {
            velX = 0;
            x = 850;
        }
        if (y < 0) {
            velY = 0;
            y = 0;
        }
        if (y > 650) {
            velY = 0;
            y = 650;
        }

        x = x + velX;
        y = y + velY;

    }

    public void keyPressed (KeyEvent e){
        int c = e.getKeyCode();

        if (c == KeyEvent.VK_LEFT) {
            velX = -1;
            velY = 0;
        }
        if (c == KeyEvent.VK_UP) {
            velX = 0;
            velY = -1;
        }
        if (c == KeyEvent.VK_RIGHT) {
            velX = 1;
            velY = 0;
        }
        if (c == KeyEvent.VK_DOWN) {
            velX = 0;
            velY = 1;
        }
    }

    public void keyTyped(KeyEvent e) {

    }

    public void keyReleased(KeyEvent e) {
            velX = 0;
            velY = 0;
    }

}

因此,当按下按键时,它会移动到[x] [y]位置,但在释放后会重置为[0] [0],就像它在keyReleased中所说的那样,但这意味着要阻止它移动而不是重置位置。所以它不会四处移动并停止,任何想法?

1 个答案:

答案 0 :(得分:0)

好的,所以在摆弄之后我解决了它。

我改变了这个:

public void keyPressed (KeyEvent e){
        int c = e.getKeyCode();

        if (c == KeyEvent.VK_LEFT) {
            velX = -1;
            velY = 0;
        }
        if (c == KeyEvent.VK_UP) {
            velX = 0;
            velY = -1;
        }
        if (c == KeyEvent.VK_RIGHT) {
            velX = 1;
            velY = 0;
        }
        if (c == KeyEvent.VK_DOWN) {
            velX = 0;
            velY = 1;
        }
    }

到此:

public void keyPressed (KeyEvent e){
        int c = e.getKeyCode();

        if (c == KeyEvent.VK_LEFT) {
            velX = -1;
            velY = 0;
        }
        if (c == KeyEvent.VK_UP) {
            velX = 0;
            velY = -1;
        }
        if (c == KeyEvent.VK_RIGHT) {
            velX = 1;
            velY = 0;
        }
        if (c == KeyEvent.VK_DOWN) {
            velX = 0;
            velY = 1;
        }
        x = x + velX;
        y = y + velY;
    }

最后一位现在更新图像以便移动。