Search code examples
javagiflag

Laggy gif in java when displaying


So basically I am trying to render an animated gif but, it looks stuttery, and laggy. I have no clue what the issue might be.

public class TestDraw {

    private static JFrame frame;

    public static void main(String[] args) {
        try {
            frame = new JFrame();
            frame.setBounds(0, 0, 1000, 1000);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.addComponentListener(null);
            Image ability = new ImageIcon(PNGDef.class.getResource("characters/ability.gif")).getImage();
            Icon icon = new ImageIcon(ability);
            try {
                frame.getContentPane().add(new JLabel(icon));
            } catch (Exception e) {
            }
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 

Here is the gif in question


Solution

  • @VGR is correct. This is pretty much identical to the behaviour I get when viewing the gif in a browser:

    import java.awt.*;
    import javax.swing.*;
    
    public class F extends JFrame {
        private void setGui() {
            try {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Container cp = getContentPane();
                cp.add(new JLabel(new ImageIcon("test.gif")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        F f = new F();
                        f.setGui();
                        f.pack();
                        f.setVisible(true);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }