Java – display animation BG in swing

Animated (looped) gifs can be displayed in jlabel or HTML (formatted text components, such as jeditorpane) and seen looping

But to load an image as the background of the container to draw, I usually use imageio Read() or toolkit Getimage() (when I feel nostalgic for the past thousand years) The method of neither loading images nor generating circular images is usually only the first frame

How do I load an animated image of the background?

Solution

Using imageicon is probably the most direct thing A few things to remember:

Imageicon (URL) itself uses toolkit getImage(URL). You may prefer to use toolkit Createimage (URL) – getimage () may use cached or shared image data. > Imageicon uses MediaTracker to effectively wait for images to be fully loaded

So your problem may not be using the toolkit (imageio is a different beast), but the fact that you don't render a complete image One interesting thing to try is:

Image image = f.getToolkit().createImage(url);
//...
ImagePanel imagePanel = new ImagePanel(image);
imagePanel.prepareImage(image,imagePanel);
//...

My swing / AWT / j2d may be a little vague, but the idea is that since your imagepanel is imageobserver, you can notify image information asynchronously Component. The imageupdate() method should call redraw as needed

Edit:

As noted in the comments, there is no need to call prepareimage – a working example is included below The key is that the overwritten paintcomponent method calls graphics DrawImage, which provides the imageobserver hook The imageupdate method (implemented in Java. AWT. Component) will continue to use imageobserver Framebits flag setting

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,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));
                imagePanel.setBorder(new EmptyBorder(20,20,20));
                for (int ii = 1; ii < 51; ii++) {
                    imagePanel.add(new JButton("" + ii));
                }

                f.setContentPane(imagePanel);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>