有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JPanel中的java动态图像

我目前正在尝试移动我导入的这个图像,选择一些键绑定。我做了一些测试,增加了它的像素移动量,但它似乎只设置了它的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中所说的,但这是为了阻止它移动而不是重置位置。所以它不会移动并停止,有什么想法吗


共 (0) 个答案