显示动画BG在Swing [英] Show an animated BG in Swing
问题描述
这是动画(循环)GIF可以显示在的JLabel
或HTML(在格式文本组件,如的JEditorPane
),并可以看到循环。
An animated (cycling) GIF can be displayed in a JLabel
or in HTML (in formatted text components like JEditorPane
) and be seen to cycle.
但要加载图像画作为容器的背景下,我想一般使用 ImageIO.read()
或 Toolkit.getImage ()
(当我感到怀念起上个千年后者)。在骑自行车的图像加载图像的结果既不是方法,通常只是第一帧。
But to load an image to paint as the background of a container, I would typically use either ImageIO.read()
or Toolkit.getImage()
(the latter when I'm feeling nostalgic for the last millennium). Neither method of loading an image results in a cycling image, it is typically just the first frame.
如何加载的动画的一个背景图片?
How to load an animated image for a background?
推荐答案
使用的ImageIcon
可能是做的最简单的事情。几件事情要记住:
Using ImageIcon
is probably the most straightforward thing to do. A couple of things to keep in mind:
-
的ImageIcon(URL)
本身利用Toolkit.getImage(URL)的
。的getImage()
可以使用缓存或共享图像 - 您可以使用Toolkit.createImage(URL)
而不是preFER数据。
ImageIcon(URL)
itself makes use ofToolkit.getImage(URL)
. You may prefer usingToolkit.createImage(URL)
instead -getImage()
may use cached or shared image data.
的ImageIcon
利用了一个媒体跟踪
来有效地等待,直到图像完全加载。
ImageIcon
makes use of a MediaTracker
to effectively wait until the image is completely loaded.
所以,您的问题可能不是使用工具包
(的ImageIO
是一个不同的野兽),而是事实上,你不渲染满载的形象。尝试一个有趣的事情是:
So, your issue may not be the use of Toolkit
(ImageIO
is a different beast), but rather the fact that you're not rendering a fully loaded image. One interesting thing to try would be:
Image image = f.getToolkit().createImage(url);
//...
ImagePanel imagePanel = new ImagePanel(image);
imagePanel.prepareImage(image, imagePanel);
//...
我的挥杆/ AWT / J2D可能有点模糊,但这个想法是,既然你的 ImagePanel
是的ImageObserver
,它可被异步关于图像信息通知。在 Component.imageUpdate()
方法应该调用重绘
需要。
My Swing/AWT/J2D may be a bit fuzzy, but the idea is that since your ImagePanel
is an ImageObserver
, it can be notified asynchronously about image information. The Component.imageUpdate()
method should invoke repaint
as needed.
编辑:
由于在评论中指出,不要求 prepareImage
通话 - 一个工作示例如下所示。最关键的是,该覆盖的paintComponent
方法调用 Graphics.drawImage
,它提供了 ImageObserver的
挂钩。在 imageUpdate的
方法(在 java.awt.Component中
实施)将连续与调用 ImageObserver.FRAMEBITS
标志设置。
As noted in the comments, the call to prepareImage
is not required - a working example is included below. The key is that the overridden paintComponent
method invokes Graphics.drawImage
, which provides the ImageObserver
hook. The imageUpdate
method (implemented in java.awt.Component
) will continuously be invoked with the ImageObserver.FRAMEBITS
flag set.
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class ImagePanel extends JPanel {
private final Image image;
public ImagePanel(Image image) {
super();
this.image = image;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.image, 0, 0, getWidth(), getHeight(), this);
}
public static void main(String[] args) throws MalformedURLException {
final URL url = new URL("http://pscode.org/media/starzoom-thumb.gif");
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Image");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationByPlatform(true);
Image image = f.getToolkit().createImage(url);
ImagePanel imagePanel = new ImagePanel(image);
imagePanel.setLayout(new GridLayout(5, 10, 10, 10));
imagePanel.setBorder(new EmptyBorder(20, 20, 20, 20));
for (int ii = 1; ii < 51; ii++) {
imagePanel.add(new JButton("" + ii));
}
f.setContentPane(imagePanel);
f.pack();
f.setVisible(true);
}
});
}
}
这篇关于显示动画BG在Swing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!