📜  如何在 jpanel 上制作 gif 动画 (1)

📅  最后修改于: 2023-12-03 15:24:13.609000             🧑  作者: Mango

如何在 JPanel 上制作 GIF 动画

本篇介绍如何在 Java Swing 中的 JPanel 上制作 GIF 动画。

准备工作

首先需要准备一个 GIF 动画文件,可以使用制作工具自行制作,也可以从网络上下载。

接下来需要安装 Java 开发环境,并添加 Swing 库。

制作过程
导入所需库
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;

public class GifAnimation extends JPanel {

    private BufferedImage[] frames;
    private int frameCount;
    private int currentFrame;
    private long animationTime;
    private int width;
    private int height;

    public GifAnimation(String fileName, int frameCount, long delayTime) {
 
        this.frameCount = frameCount;
        this.animationTime = delayTime * frameCount;
        this.frames = new BufferedImage[frameCount];

        try {
            URL url = getClass().getResource(fileName);
            for (int i = 0; i < frameCount; i++) {
                frames[i] = ImageIO.read(url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.width = frames[0].getWidth();
        this.height = frames[0].getHeight();
    }
}

首先导入了需要的库,包括 Swing 库和 Java AWT 库,其中 BufferedImage 是 Java AWT 库中用来存储和操作图像的类,ImageIO 则是 Java AWT 库中用来处理图像输入和输出的类。

加载 GIF 动画
try {
    URL url = getClass().getResource(fileName);
    for (int i = 0; i < frameCount; i++) {
        frames[i] = ImageIO.read(url);
    }
} catch (Exception e) {
    e.printStackTrace();
}

使用 ImageIO 类的 read() 方法从文件中读取 GIF 动画帧的图像数据,并存储在 BufferedImage 数组中。

绘图
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(frames[currentFrame], 0, 0, null);
    currentFrame = (int) ((System.currentTimeMillis() - animationTime) % frameCount);
}

在 JPanel 的 paintComponent() 方法中,首先调用 super.paintComponent(g) 方法清除 JPanel 上的旧图像。接着将 Graphics 对象转换为 Graphics2D 对象,并使用 BufferedImage 数组中的当前帧绘制图像。

完整示例
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;

public class GifAnimation extends JPanel {

    private BufferedImage[] frames;
    private int frameCount;
    private int currentFrame;
    private long animationTime;
    private int width;
    private int height;

    public GifAnimation(String fileName, int frameCount, long delayTime) {
 
        this.frameCount = frameCount;
        this.animationTime = delayTime * frameCount;
        this.frames = new BufferedImage[frameCount];

        try {
            URL url = getClass().getResource(fileName);
            for (int i = 0; i < frameCount; i++) {
                frames[i] = ImageIO.read(url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.width = frames[0].getWidth();
        this.height = frames[0].getHeight();
    }

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

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(frames[currentFrame], 0, 0, null);
        currentFrame = (int) ((System.currentTimeMillis() - animationTime) % frameCount);
    }

    public static void main(String[] args) {
        String fileName = "animation.gif";
        int frameCount = 10;
        long delayTime = 100;

        GifAnimation gifAnimation = new GifAnimation(fileName, frameCount, delayTime);

        JFrame frame = new JFrame("GIF Animation");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(gifAnimation);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        while (true) {
            gifAnimation.repaint();
            try {
                Thread.sleep(delayTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

以上就是如何在 JPanel 上制作 GIF 动画的介绍。