Java: Animated GIF with transparent window

I'm trying to use a simple jlabel to display animation GIF on a transparent jdialog:

JDialog dialog = new JDialog();
AWTUtilities.setWindowOpaque(dialog,false);

JLabel label = new JLabel();
ImageIcon ii = new ImageIcon("animation.gif");
label.setIcon(ii);

JPanel panel = new JPanel();
panel.setBackground(new Color(0,0));
panel.add(label);
dialog.add(panel);

dialog.setVisible(true);

This is almost feasible It displays animation smoothly and does have transparency The problem is that all animation frames are overwritten instead of getting a cleared canvas and current frame every frame step I assume that jlabel's canvas is not cleared in each redraw step Who knows how I can solve this problem?

Editor: I came up with a solution I had to override the painticon function of imageicon and manually clear the canvas:

class ClearImageIcon extends ImageIcon{
    public ClearImageIcon(String filename){super(filename);}

    @Override
    public synchronized void paintIcon(Component c,Graphics g,int x,int y) {
        Graphics2D g2 = (Graphics2D)g.create();
        g2.setBackground(new Color(0,0));
        g2.clearRect(0,getIconWidth(),getIconHeight());
        super.paintIcon(c,g2,x,y);
    }
}

In this way, each frame can be well drawn on the screen

Solution

This http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html Contains gradientpaint, which is similair to add image or imageicon

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
分享
二维码
< <上一篇
下一篇>>