开发者

Java: Animated GIF with transparent window

I'm trying to开发者_C百科 display an animated gif on a transparent JDialog using a simple JLabel:

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,0,0));
panel.add(label);
dialog.add(panel);

dialog.setVisible(true);

This almost works. It displays the animation smoothly and it does have transparency. The problem is that all the animation frames are overlaid instead of getting a cleared canvas and the current frame every frame step. I assume that the JLabel's canvas doesn't get cleared every repaint step. Does anyone know how I can fix this?

Edit: I figured out a solution. I had to override the ImageIcon's paintIcon function 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,0,0));
        g2.clearRect(0, 0, getIconWidth(), getIconHeight());
        super.paintIcon(c, g2, x, y);
    }
}

this draws every frame nicely on to the screen.


this http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html contains GradientPaint, that's is similair as add Image or ImageIcon

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜