Java GIF动画无法正确重新绘制 [英] Java GIF animation not repainting correctly

查看:187
本文介绍了Java GIF动画无法正确重新绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为GIF图像制作动画。动画有效,但效果不佳。



它显示如下(非动画截图):



在图片中,尾巴像这样摇晃:



如您所见,图像不能很好地重现。我不想使用JLabel但是没有正常工作,所以我跟着表示:


恢复到上一个​​。解码器需要恢复图形覆盖的区域,以及渲染图形之前的内容。


图像的位置I以上提供的使用 restoreToBackgroundColor ,根据 GRAPHICS INTERCHANGE FORMAT版本89a 表示:


恢复为背景颜色。图形使用的区域必须恢复为背景颜色


您可以使用以下代码自行检查...

  public static class AnimatedGif {

public enum DisposalMethod {

RESTORE_TO_BACKGROUND,
RESTORE_TO_PREVIOUS,
DO_NOT_DISPOSE,
未检验到

public static DisposalMethod find(String text){

DisposalMethod dm = UNSPECIFIED;
System.out.println(text);

switch(text){
caserestoreToBackgroundColor:
dm = RESTORE_TO_BACKGROUND;
休息;
caserestoreToPrevious:
dm = RESTORE_TO_PREVIOUS;
休息;
}

返回dm;

}
}

private List< ImageFrame>帧;
private int frame;

public AnimatedGif(JComponent player,URL url)抛出IOException {
frames = new ArrayList<>(25);
try(InputStream is = url.openStream(); ImageInputStream stream = ImageIO.createImageInputStream(is)){
Iterator readers = ImageIO.getImageReaders(stream);
if(!readers.hasNext()){
抛出新的RuntimeException(找不到图像阅读器);
}
ImageReader reader =(ImageReader)readers.next();
reader.setInput(stream); //不要省略这一行!
int n = reader.getNumImages(true); //不要使用false!
System.out.println(numImages =+ n);
for(int i = 0; i< n; i ++){
BufferedImage image = reader.read(i);
ImageFrame imageFrame = new ImageFrame(image);

IIOMetadata imd = reader.getImageMetadata(i);
节点树= imd.getAsTree(javax_imageio_gif_image_1.0);
NodeList children = tree.getChildNodes();

for(int j = 0; j< children.getLength(); j ++){
Node nodeItem = children.item(j);
NamedNodeMap attr = nodeItem.getAttributes();
switch(nodeItem.getNodeName()){
caseImageDescriptor:
ImageDescriptor id = new ImageDescriptor(
getIntValue(attr.getNamedItem(imageLeftPosition)),
getIntValue(attr.getNamedItem(imageTopPosition)),
getIntValue(attr.getNamedItem(imageWidth)),
getIntValue(attr.getNamedItem(imageHeight)),
getBooleanValue(attr.getNamedItem( interlaceFlag)));
imageFrame.setImageDescriptor(id);
休息;
caseGraphicControlExtension:
GraphicControlExtension gc = new GraphicControlExtension(
DisposalMethod.find(getNodeValue(attr.getNamedItem(disposalMethod))),
getBooleanValue(attr.getNamedItem( userInputFlag)),
getBooleanValue(attr.getNamedItem(transparentColorFlag)),
getIntValue(attr.getNamedItem(delayTime))* 10,
getIntValue(attr.getNamedItem( transparentColorIndex)));
imageFrame.setGraphicControlExtension(gc);
休息;
}
}
frames.add(imageFrame);
}
} finally {
}
}

protected String getNodeValue(Node node){
return node == null? null:node.getNodeValue();
}

protected int getIntValue(节点节点){
return node == null? 0:getIntValue(node.getNodeValue());
}

protected boolean getBooleanValue(Node node){
return node == null? false:getBooleanValue(node.getNodeValue());
}

protected int getIntValue(String value){
return value == null? 0:Integer.parseInt(value);
}

protected boolean getBooleanValue(String value){
return value == null? false:Boolean.parseBoolean(value);
}

公共类ImageFrame {

private BufferedImage image;
private ImageDescriptor imageDescriptor;
private GraphicControlExtension graphicControlExtension;

public ImageFrame(BufferedImage image){
this.image = image;
}

protected void setImageDescriptor(ImageDescriptor imageDescriptor){
this.imageDescriptor = imageDescriptor;
}

protected void setGraphicControlExtension(GraphicControlExtension graphicControlExtension){
this.graphicControlExtension = graphicControlExtension;
System.out.println(graphicControlExtension.getDisposalMethod());
}

public GraphicControlExtension getGraphicControlExtension(){
return graphicControlExtension;
}

public BufferedImage getImage(){
return image;
}

public ImageDescriptor getImageDescriptor(){
return imageDescriptor;
}

}

公共类GraphicControlExtension {

private DisposalMethod disposalMethod;
private boolean userInputFlag;
private boolean transparentColorFlag;
private int delayTime;
private int transparentColorIndex;

public GraphicControlExtension(DisposalMethod disposalMethod,boolean userInputFlag,boolean transparentColorFlag,int delayTime,int transparentColorIndex){
this.disposalMethod = disposalMethod;
this.userInputFlag = userInputFlag;
this.transparentColorFlag = transparentColorFlag;
this.delayTime = delayTime;
this.transparentColorIndex = transparentColorIndex;
}

public int getDelayTime(){
return delayTime;
}

public DisposalMethod getDisposalMethod(){
return disposalMethod;
}

public int getTransparentColorIndex(){
return transparentColorIndex;
}

public boolean isTransparentColorFlag(){
return transparentColorFlag;
}

public boolean isUserInputFlag(){
return userInputFlag;
}

}

公共类ImageDescriptor {

private int imageLeftPosition;
private int imageTopPosition;
private int imageHeight;
private int imageWeight;
private boolean interlaced;

public ImageDescriptor(int imageLeftPosition,int imageTopPosition,int imageHeight,int imageWeight,boolean interlaced){
this.imageLeftPosition = imageLeftPosition;
this.imageTopPosition = imageTopPosition;
this.imageHeight = imageHeight;
this.imageWeight = imageWeight;
this.interlaced =隔行扫描;
}

public int getImageHeight(){
return imageHeight;
}

public int getImageLeftPosition(){
return imageLeftPosition;
}

public int getImageTopPosition(){
return imageTopPosition;
}

public int getImageWeight(){
return imageWeight;
}

public boolean isInterlaced(){
return interlaced;
}

}

}

此代码来自 .gif图像在将其添加到JTabbed窗格时不会移动


I'm trying to animate a GIF image. The animation works, but it's not painting well.

It shows like this (non-animated screenshot):

In the image, the tail wags like this:

As you can see, the image does not repaint well. I don't want to use JLabels but that did not work correctly, so I followed this question when my image did not animate.

My code is like this:

public void draw(JPanel canvas, Graphics2D g2d, int x, int y) {
    getFrontImage().paintIcon(canvas, g2d, x, y);
}

Where the image is retrieved and saved like this:

ImageIcon gif = new ImageIcon(getClass().getResource(filename));

In the JPanel canvas I have made a paint method and a timer thread that repaints every 10ms. That works for everything except for the GIF. Anyone who could help me with this?

--- Edit

I'm sorry for the misunderstanding, I've updated the image to the one I'm actually using. I hope it's not too much of a hassle to get the answer right...

解决方案

Okay, so after much mucking about, I was able to, finally, change the disposal method for the frame to restoreToBackgroundColor. Basically, what this means is that the animation is not an incremental change, but a complete frame replacement...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class AnimatedGifTest1 {

    public static void main(String[] args) {
        new AnimatedGifTest1();
    }

    public AnimatedGifTest1() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PaintPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintPane extends JPanel {

        private ImageIcon image;

        public PaintPane() {
            image = new ImageIcon(getClass().getResource("/ertcM02.gif"));
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return image == null ? new Dimension(200, 200) : new Dimension(image.getIconWidth(), image.getIconHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); // This is very important!
            int x = (getWidth() - image.getIconWidth()) / 2;
            int y = (getHeight() - image.getIconHeight()) / 2;
            image.paintIcon(this, g, x, y);
        }

    }



}

Updated...

So, I was finally able to have a look at the disposal method for the gif you are using, which is set to restoreToPrevious, which, according to GRAPHICS INTERCHANGE FORMAT Version 89a means:

Restore to previous. The decoder is required to restore the area overwritten by the graphic with what was there prior to rendering the graphic.

Where as the image I've provided above uses restoreToBackgroundColor, which according to to GRAPHICS INTERCHANGE FORMAT Version 89a means:

Restore to background color. The area used by the graphic must be restored to the background color

You can check it yourself using the following code...

public static class AnimatedGif {

    public enum DisposalMethod {

        RESTORE_TO_BACKGROUND,
        RESTORE_TO_PREVIOUS,
        DO_NOT_DISPOSE,
        UNSPECIFIED;

        public static DisposalMethod find(String text) {

            DisposalMethod dm = UNSPECIFIED;
            System.out.println(text);

            switch (text) {
                case "restoreToBackgroundColor":
                    dm = RESTORE_TO_BACKGROUND;
                    break;
                case "restoreToPrevious":
                    dm = RESTORE_TO_PREVIOUS;
                    break;
            }

            return dm;

        }
    }

    private List<ImageFrame> frames;
    private int frame;

    public AnimatedGif(JComponent player, URL url) throws IOException {
        frames = new ArrayList<>(25);
        try (InputStream is = url.openStream(); ImageInputStream stream = ImageIO.createImageInputStream(is)) {
            Iterator readers = ImageIO.getImageReaders(stream);
            if (!readers.hasNext()) {
                throw new RuntimeException("no image reader found");
            }
            ImageReader reader = (ImageReader) readers.next();
            reader.setInput(stream);            // don't omit this line!
            int n = reader.getNumImages(true);  // don't use false!
            System.out.println("numImages = " + n);
            for (int i = 0; i < n; i++) {
                BufferedImage image = reader.read(i);
                ImageFrame imageFrame = new ImageFrame(image);

                IIOMetadata imd = reader.getImageMetadata(i);
                Node tree = imd.getAsTree("javax_imageio_gif_image_1.0");
                NodeList children = tree.getChildNodes();

                for (int j = 0; j < children.getLength(); j++) {
                    Node nodeItem = children.item(j);
                    NamedNodeMap attr = nodeItem.getAttributes();
                    switch (nodeItem.getNodeName()) {
                        case "ImageDescriptor":
                            ImageDescriptor id = new ImageDescriptor(
                                            getIntValue(attr.getNamedItem("imageLeftPosition")),
                                            getIntValue(attr.getNamedItem("imageTopPosition")),
                                            getIntValue(attr.getNamedItem("imageWidth")),
                                            getIntValue(attr.getNamedItem("imageHeight")),
                                            getBooleanValue(attr.getNamedItem("interlaceFlag")));
                            imageFrame.setImageDescriptor(id);
                            break;
                        case "GraphicControlExtension":
                            GraphicControlExtension gc = new GraphicControlExtension(
                                            DisposalMethod.find(getNodeValue(attr.getNamedItem("disposalMethod"))),
                                            getBooleanValue(attr.getNamedItem("userInputFlag")),
                                            getBooleanValue(attr.getNamedItem("transparentColorFlag")),
                                            getIntValue(attr.getNamedItem("delayTime")) * 10,
                                            getIntValue(attr.getNamedItem("transparentColorIndex")));
                            imageFrame.setGraphicControlExtension(gc);
                            break;
                    }
                }
                frames.add(imageFrame);
            }
        } finally {
        }
    }

    protected String getNodeValue(Node node) {
        return node == null ? null : node.getNodeValue();
    }

    protected int getIntValue(Node node) {
        return node == null ? 0 : getIntValue(node.getNodeValue());
    }

    protected boolean getBooleanValue(Node node) {
        return node == null ? false : getBooleanValue(node.getNodeValue());
    }

    protected int getIntValue(String value) {
        return value == null ? 0 : Integer.parseInt(value);
    }

    protected boolean getBooleanValue(String value) {
        return value == null ? false : Boolean.parseBoolean(value);
    }

    public class ImageFrame {

        private BufferedImage image;
        private ImageDescriptor imageDescriptor;
        private GraphicControlExtension graphicControlExtension;

        public ImageFrame(BufferedImage image) {
            this.image = image;
        }

        protected void setImageDescriptor(ImageDescriptor imageDescriptor) {
            this.imageDescriptor = imageDescriptor;
        }

        protected void setGraphicControlExtension(GraphicControlExtension graphicControlExtension) {
            this.graphicControlExtension = graphicControlExtension;
            System.out.println(graphicControlExtension.getDisposalMethod());
        }

        public GraphicControlExtension getGraphicControlExtension() {
            return graphicControlExtension;
        }

        public BufferedImage getImage() {
            return image;
        }

        public ImageDescriptor getImageDescriptor() {
            return imageDescriptor;
        }

    }

    public class GraphicControlExtension {

        private DisposalMethod disposalMethod;
        private boolean userInputFlag;
        private boolean transparentColorFlag;
        private int delayTime;
        private int transparentColorIndex;

        public GraphicControlExtension(DisposalMethod disposalMethod, boolean userInputFlag, boolean transparentColorFlag, int delayTime, int transparentColorIndex) {
            this.disposalMethod = disposalMethod;
            this.userInputFlag = userInputFlag;
            this.transparentColorFlag = transparentColorFlag;
            this.delayTime = delayTime;
            this.transparentColorIndex = transparentColorIndex;
        }

        public int getDelayTime() {
            return delayTime;
        }

        public DisposalMethod getDisposalMethod() {
            return disposalMethod;
        }

        public int getTransparentColorIndex() {
            return transparentColorIndex;
        }

        public boolean isTransparentColorFlag() {
            return transparentColorFlag;
        }

        public boolean isUserInputFlag() {
            return userInputFlag;
        }

    }

    public class ImageDescriptor {

        private int imageLeftPosition;
        private int imageTopPosition;
        private int imageHeight;
        private int imageWeight;
        private boolean interlaced;

        public ImageDescriptor(int imageLeftPosition, int imageTopPosition, int imageHeight, int imageWeight, boolean interlaced) {
            this.imageLeftPosition = imageLeftPosition;
            this.imageTopPosition = imageTopPosition;
            this.imageHeight = imageHeight;
            this.imageWeight = imageWeight;
            this.interlaced = interlaced;
        }

        public int getImageHeight() {
            return imageHeight;
        }

        public int getImageLeftPosition() {
            return imageLeftPosition;
        }

        public int getImageTopPosition() {
            return imageTopPosition;
        }

        public int getImageWeight() {
            return imageWeight;
        }

        public boolean isInterlaced() {
            return interlaced;
        }

    }

}

This code comes from .gif image doesn't moves on adding it to the JTabbed pane

这篇关于Java GIF动画无法正确重新绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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