Java定时器和绘图动画

标签 java image swing animation timer

我不知道如何解决这个问题。我正在做的事情是尝试将我拥有的怪物类别分解为不同的东西,例如一个用于玩家的类别,一个用于火球的类别等。在尝试分解该类别之前我一切正常,但现在我收到了错误。我想知道是否有人可以帮助我解决这个问题并向我解释如何不再重复这个错误。先感谢您。 编辑:错误出现:animationTimer = new Timer(animationDelay, this);

编辑:发现 1 个错误: 文件:C:\Users\jozef\Java\Dragon Ball Z\Player.java [行:45] 错误:类型不兼容:Player 无法转换为 java.awt.event.ActionListener 另外,我的格式正确,但是当我尝试将代码复制并粘贴到框中以发布到此处时,它不会将其视为代码,因此我必须缩进每一行以使其显示为代码而不是普通文本。

import java.awt.Graphics;
import java.awt.MediaTracker;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.event.ActionEvent;
import javax.swing.Timer;

public class Player {
    int x;
    int y;
    ImageIcon pictures[];
    int total;
    int current;
    boolean sideMove;
    int move;
    Timer animationTimer;
    int animationDelay = 80;

    public Player(int startX, int startY, ImageIcon image[], boolean sideMove, int move) {
        x = startX;
        y = startY;
        pictures = image;
        total = pictures.length;
        this.sideMove = sideMove;
        this.move = move;
        startAnimation();
    }

    public void draw(Graphics g) {
        if (pictures[current].getImageLoadStatus() == MediaTracker.COMPLETE) {
            Image img = pictures[current].getImage();
            g.drawImage(img, x, y, null);
            current = (current + 1) % total;
        }
        update();
    }

    public void update() {
        if (sideMove == true) {
            x += move;
        } else {
            y += move;
        }
    }

    public void startAnimation() {
        if (animationTimer == null) {
            current = 0;
            animationTimer = new Timer(animationDelay, this); // *** error ***
            animationTimer.start();
        } else if (!animationTimer.isRunning())
            animationTimer.restart();
    }

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

最佳答案

这里:

animationTimer = new Timer(animationDelay, this);

由于 Player 类没有实现 ActionListener this 无法作为有效参数传递给 Timer 构造函数。一个可能的解决方案是让您的 Player 类实现 ActionListener,为其提供适当的 actionPerformed 方法:

public class Player implements ActionListener {

     @Override
     protected void actionPerformed(ActionEvent e) {
         // your coded here
     }

     // .... rest of your code

或者更好的是,使用不同的 ActionListener,例如匿名内部类。

例如,

public void startAnimation() {
    if (animationTimer == null) {
        current = 0;
        animationTimer = new Timer(animationDelay, e -> timerActionPerformed(e));
        animationTimer.start();
    } else if (!animationTimer.isRunning()) {
        animationTimer.restart();
    }
}

private void timerActionPerformed(ActionEvent e) {
    // TODO repeated code goes here
}

侧面建议:

  • 您的绘画方法中的代码会更改 Player 对象的状态,这是您希望避免的。了解您只能部分控制何时或什至是否绘制对象,因此最好将它们分开。
  • 我自己,我会从 Player 类中取出 Timer ,并在更通用的整体控制类中使用 Timer 作为游戏循环或动画 Controller ,也许是 Game 类(或者任何你的“宇宙”类被称为),该类保存并控制所有逻辑实体,例如 Player 对象。

关于Java定时器和绘图动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792018/

相关文章:

java - Swing 与 SWT

java - 轻量级组件中剪辑区域的处理与文档不匹配?

java - 使用外部 JARS 创建 Java applet

image - 提高图像质量以通过 OpenCV 读取模糊数字

java - 如何清除应用程序引擎中的所有内存缓存。 Java环境

javascript - 如何将 HTML 文件转换为 GIF

java - JLabel 获取图像

Java 如何将menuBar添加到JFrame而不破坏我的布局

java - 在 Hybris Commerce 上的 AbstractOrder 中添加自定义值 - AddtoCartFunction

java - 多个项目使用相同的 API key