为什么gif动画在paintComponent()中使用它时没有动画? [英] Why gif animation doesn't animate when using it in paintComponent()?

查看:268
本文介绍了为什么gif动画在paintComponent()中使用它时没有动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 paintComponent() JPanel 的背景上绘制一个gif动画图像。
显示gif但不动画。
我使用java 1.5,我知道我可以使用带有图标的标签。

I'm using paintComponent() to paint a gif animated image at the backgound of JPanel. It shows up the gif but doesn't animate. I use java 1.5 and i know that i can use label with icon.

是否有人知道为什么以及如何修复它?

    private static class CirclePanel extends JPanel {

    ImageIcon imageIcon = new ImageIcon(BarcodeModel.class.getResource("verify.gif"));
    Point point = f.getLocation();
    protected void paintComponent(Graphics g) {
        Graphics gc = g.create();
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
        g2d.setColor(Color.BLUE);
        g2d.drawImage(imageIcon.getImage(), getWidth() / 2, getHeight() / 2, null);
        g2d.drawRect(0, 0, getWidth(), getHeight());
        g2d.setStroke(new BasicStroke(10f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER));
        g2d.setFont(g.getFont().deriveFont(Font.BOLD | Font.ITALIC,15f));
        g2d.drawString("Wait Please ...",getWidth()/2-imageIcon.getIconHeight()/3,getHeight()/2+imageIcon.getIconHeight()+15);

        g2d.dispose();

}

这是gif图片。

This is the gif image.

已编辑:只需将图像观察者添加到g2d.drawImage()方法即可。

Edited: just add image observer to the g2d.drawImage() method.

 g2d.drawImage(imageIcon.getImage(), getWidth() / 2, getHeight() / 2, this);


推荐答案

原因是标准的Java ImageIO API只加载gif的第一张图片。
如何解决? Google用于Java的Gif Loader,它可以加载gif的每个图像。然后你必须在正确的时间画出正确的图像。另一种方法是每次动画一帧时都有不同的png文件。

The reason is that the standard Java ImageIO API only loads the first image of the gif. How to fix? Google for a Gif Loader for Java, which loads every image of the gif. Then you have to paint the right image at the right time. An alternative way would be to have different png files representing each time one frame of the animation.

更新:嗯...实际上,之后做一些研究,它看起来像你做的方式实际上加载动画GIF的所有帧。原因是ImageIcon的方法 getImage()总是返回第一个图像。

Update: Well... Actually, after doing some research, it looks like the way you did it actually loads all the frames of the animated gif. The reason for it is that the ImageIcon's method getImage() always returns the first image.

要修复它,你可以尝试这个(我不确定它是否会起作用......)

To fix it, you can try this (I'm not sure if it will work...)

而不是使用 Grahpics.drawImage(),使用 ImageIcon.paintIcon()。像这样:

Instead of using Grahpics.drawImage(), use ImageIcon.paintIcon(). Like this:

imageIcon.paintIcon(this, g2d, getWidth() / 2 - imageIcon.getIconWidth() / 2, getHeight() / 2);

这篇关于为什么gif动画在paintComponent()中使用它时没有动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆