如何在JPanel中添加GIF图像?

时间:2015-10-30 17:10:03

标签: java swing jpanel paintcomponent animated-gif

我想在我的面板中绘制一个gif图像,问题是我的代码绘制jpg图像很好,但我没有绘制gif图像。这就是我的代码的样子。

public class DrawingPanel extends JPanel {
Image image;

public DrawingPanel() {
    setPreferredSize(new Dimension(600,600));
    setBackground(Color.CYAN);
    image = new  ImageIcon("C:\\Users\\Hp\\Downloads\\loading.gif").getImage();
}

@Override
    protected void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponent(g);
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}

1 个答案:

答案 0 :(得分:0)

BufferedImage image;
image = ImageIO.read(new File("image name and path"));

使用ImageIO

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, 0, 0, null);    
}