0

When I try to use a GIF in a JLabel either resized or normal, it smears and produces an odd effect:

https://i.stack.imgur.com/lxyep.gif

The GIF I'm using:

https://i.stack.imgur.com/FiY43.gif

Layout of the JPanel it is attached to is null, and images that are accessed are part of a jar file.

Here's the code for creating and adding the label:

public JLabel j_Anim(String path, int width, int height, int xpos, int ypos, boolean applyscaling) throws Exception {
    Image finimage;
    if (!applyscaling) {
        finimage = new ImageIcon(Main.class.getResource(path)).getImage();
    } else {
        finimage = (new ImageIcon(Main.class.getResource(path)).getImage()).getScaledInstance(width, height, Image.SCALE_DEFAULT);
    }
    JLabel im = new JLabel(new ImageIcon(finimage));
    group.panel.add(im);
    im.setBounds(xpos, ypos, width, height);
    return im;
}

I'm fairly new to java, so any help would be greatly appreciated!

EDIT: Minimal Reproducable Example:

import javax.swing.*;
import java.awt.*;

class Main extends JFrame {

    public static void main(String[] args) {
        Main ma = new Main();
    }

    Main() {
        JPanel p = new JPanel();
        this.setSize(300, 300);
        this.add(p);
        JLabel im = new JLabel(new ImageIcon(this.getClass().getResource("img/walk.gif")));
        p.add(im);
        this.setVisible(true);
        this.pack();
    }
}

EDIT 2: This behaviour is only happening with gifs that have transparent backgrounds, standard solid-background gifs work fine. However, the issue is still unresolved because I need to use a gif with a transparent background. Thanks for the help so far!

EDIT 3: The issue was solved by changing the gif's disposal method to background. I used this cmd script with imagemagick to generate a custom gif with the specified disposal method from a spritesheet:

set WIDE=50
set HIGH=75

set XCOORD=0
set YCOORD=0

set FRAMES=8

convert spritesheet.png ^
   -set option:distort:viewport %[fx:%FRAMES%*%WIDE%]x%HIGH% ^
   -set option:slider %[fx:%YCOORD%*(w/%WIDE%)+%XCOORD%] ^
   -crop %WIDE%x%HIGH% +append +repage ^
   -distort affine "%[slider],0 0,0" ^
   -crop %WIDE%x%HIGH% +repage ^
   -set delay 10 -loop 0 -set dispose Background result.gif

pause
exit
  • 2
    Don't try to set the bounds of the label. Just create the label and add it to a JPanel that is using a layout manager so the label is displayed at its preferred size. Get that working first. Then if you need to randomly set the location of the label you would use `abel.setSize( label.getPreferredSize() )` to make sure the label has the proper size. You can then use label.setLocation(…) to position the label. Post a proper [mre] if you need more help because we can't guess the context of how the posted code is used. – camickr Jun 06 '20 at 18:54
  • 1
    General comments: 1) you don't need the setSize(). That is the job of the layout manager. I only mentioned that comment if you are using a null layout and random positioning of the label. 2) Components should be added to the frame BEFORE making the frame visible. 3) you should pack() the frame after adding component and before making the frame visible to invoke the layout manager. 4) ther is no need to create two ImageIcons. Just load the image directly into the ImageIcon. – camickr Jun 06 '20 at 20:01
  • After fixing all those problems I still get the same behaviour as you do. I have never had a problem with an animatied gif before, so I suggest you try a different gif. Maybe try the gif (and suggestions) from: https://stackoverflow.com/questions/49826647/java-problems-with-gif-in-label to see if you still have a problem. – camickr Jun 06 '20 at 20:09
  • Many GIF are poorly encoded. Browsers go to extraordinary lengths to display them properly. I'd recommend loading the GIF into editing software and checking and correcting it. – Andrew Thompson Jun 07 '20 at 11:43

0 Answers0