0

I want to show a gif in java swing.

For show gif I use from these code but in both of them the gif is not moving and it like as an image is static.

first code:

void showGif() {
    try {
        JPanel panel = new JPanel();
        BufferedImage bufferedImage = ImageIO.read(new File("address of gif");
        Icon icon = new ImageIcon(bufferedImage);
        JLabel label = new JLabel(icon);
        label.setVisible(true);
        panel.add(label);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

second code:

 void showGif() {
    try {
        ImageIcon imageIcon;
        BufferedImage bufferedImage = ImageIO.read(new File("address of gif"));
        imageIcon = new ImageIcon(bufferedImage);
        JLabel label = new JLabel(imageIcon);
        JPanel panel = new JPanel();
        label.setVisible(true);
        panel.add(label, TOP_ALIGNMENT);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

please help me to show gif correctly.

ali
  • 15
  • 3
  • Answer of that question have ImagePanel, but it not available in java 8 – ali Jun 05 '17 at 13:37
  • ImagePanel is a custom class presented as part of that question’s answer. It is not a Java SE class in any version of Java. – VGR Jun 05 '17 at 13:39

1 Answers1

1

You ImageIcon in this case will be showing just the first frame of the GIF.

You can add the GIF to a JLabel, which you can add to the JPanel. Check this out for further help.

Robo Mop
  • 3,485
  • 1
  • 10
  • 23