2

I'm trying to make a game in Java, and I decided to use GIFs as animations in a JFrame. However, the program keeps showing past frames of the GIF, so the whole image looks like a mess.

This is what the GIF should look like in the program:

Correct

However, the GIF ends up looking like this (this is a screenshot of what appears on the JFrame):

Wrong

And here is my code so far. I'm using NetBeans with Java 8.0.2.

package giftest;

public class MainWindow extends javax.swing.JFrame {

public MainWindow() {
    initComponents();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giftest/Chespin2.gif"))); // NOI18N
    jLabel1.setText("jLabel1");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(158, 158, 158)
            .addComponent(jLabel1)
            .addContainerGap(12, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(89, 89, 89)
            .addComponent(jLabel1)
            .addContainerGap(19, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        


public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MainWindow().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel jLabel1;
// End of variables declaration  

}
// End of Program

The effect can be reproduced with these lines of code:

import javax.swing.*;
import java.net.*;

public class AnimatedGifTest {

    AnimatedGifTest() throws MalformedURLException {
        URL url = new URL(
                "https://i.stack.imgur.com/y9O4G.gif");
        ImageIcon ii = new ImageIcon(url);
        JOptionPane.showMessageDialog(null, ii);
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                new AnimatedGifTest();
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

I'm using Java Swing, and I'm using jLabels and putting the GIF as the icon. Is there a way to fix this issue in NetBeans on Java 8.0.2? Thanks in advance!

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Is this an animated GIF? – markspace Mar 05 '20 at 18:05
  • Yes, this is an animated GIF – Neurobiological Mar 05 '20 at 18:21
  • This works for me, but [I’ve seen this problem on Stack Overflow before](https://stackoverflow.com/questions/58754566/java-swing-applicaion-gif-animation-leaving-a-trail). Looks like it wasn’t solved then, either. – VGR Mar 05 '20 at 18:28
  • 1
    After analyzing the image’s metadata, I found that GIFs with `disposalMethod="restoreToBackgroundColor"` draw properly, while those with `disposalMethod="restoreToPrevious"` exhibit the problem you’re seeing. This led me to https://stackoverflow.com/questions/26330550/java-gif-animation-not-repainting-correctly which may be a duplicate of your question, with a solution. – VGR Mar 05 '20 at 18:54
  • I can see the problem when running the shorter code edited into the question. It is an easy-to-use [mre]. Please consider deleting the longer code above it. – Andrew Thompson Mar 06 '20 at 03:04

0 Answers0