如何在java中绘制gif动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22240328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw a gif animation in java?
提问by java-love
I have some gif animated images (see sample image below)
and when I draw them with the graphics object, I am getting only the first image,
I know I can do this with JLabel (from other stackoverflow answers)
but I want to do that with the graphics object,
can anyone please tell me
an easyway how to draw the whole animation?
我有一些 gif 动画图像(见下面的示例图像)
,当我用图形对象绘制它们时,我只得到第一张图像,
我知道我可以用 JLabel(来自其他 stackoverflow 答案)做到这一点,
但我想这样做使用图形对象,
谁能告诉我如何绘制整个动画
的简单方法?
采纳答案by Paul Samsotha
"please tell me an easyway how to draw the whole animation?!"
“请告诉我一个简单的方法如何绘制整个动画?!”
It may have to to do with how you're reading in the image. If you use ImageIO.read, it won't work. If you read it as an ImageIcon
it seems to work
这可能与您在图像中的阅读方式有关。如果您使用 ImageIO.read,它将不起作用。如果你把它读成一个ImageIcon
它似乎工作
ImageIcon.getImage()
ImageIcon.getImage()
Image icon = new ImageIcon(new URL("http://i.stack.imgur.com/KSnus.gif")).getImage();
...
g.drawImage(icon, 20, 20, this);
Image with ImageIO
带有 ImageIO 的图像
Image icon = ImageIO.read(new URL("http://i.stack.imgur.com/KSnus.gif"));
...
g.drawImage(icon, 20, 20, this);