2013-08-18 41 views
0

所以,我遇到了一個程序問題。我有一個透明的GIF。該動畫能夠正常工作,除了一次不顯示一張圖像之外,它可以將圖像堆疊在一起。我嘗試覆蓋ImageIcon類的paintIcon方法來清除畫布,但那也行不通。有什麼想法嗎? 我的代碼:在Java 7中,透明GIF不能正確動畫

​​

任何幫助,將不勝感激

+0

你能提供的GIF和您運行的是什麼操作系統,請致電 – MadProgrammer

+0

@MadProgrammer Ubuntu 12.04和Windows 7,都發生在兩者上。 [這是圖片](http://i1211.photobucket.com/albums/cc432/sk8torchic/normal_zpsa40697c9.gif) –

+0

好吧,事實證明,我錯了有關Java版本的問題。它是Java 6.這會改變什麼嗎? –

回答

0

可以重寫的ImageIcon的的paintIcon功能和手動清除畫布:

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); 
    } 
} 

這將很好地在繪製每一幀的屏幕。

+0

我實際上已經嘗試過,由於某種原因,它沒有工作。 –

0

工作正常,我...

的Windows 7,Java 7中

enter image description here

Original

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class AnimatedGifTest { 

    public static void main(String[] args) { 
    new AnimatedGifTest(); 
    } 

    public AnimatedGifTest() { 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
      ex.printStackTrace(); 
     } 

     JFrame frame = new JFrame("Testing"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new TestPane()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     } 
    }); 
    } 

    public class TestPane extends JPanel { 

    public TestPane() { 
     setLayout(new GridBagLayout()); 
     ImageIcon image = new ImageIcon("C:\\Users\\swhitehead\\Documents\\My Dropbox\\Ponies\\appleture_animated_gif_by_inkwell_pony-d4uggao.gif"); 
     JLabel label = new JLabel(image); 
     add(label); 
    }   
    }  
}