1

I'm trying to display a GIF with a transparent background in a JFrame. My initial code is simple.

import java.awt.*;
import javax.swing.*;
public class GUIProject 
{
    public static void main(String [] args)
    {
        JFrame pokemonFrame = new JFrame();
        pokemonFrame.setSize(800, 800);
        ImageIcon imageLabel = new ImageIcon("/Users/Hannah/eclipse-workspace/Graphics/pokemon images/pikasprite.gif");
        JLabel label = new JLabel(imageLabel);
        pokemonFrame.setLayout(new FlowLayout());
        pokemonFrame.add(label);
        pokemonFrame.setVisible(true);  
    }
}

Unfortunately, I'm running into a problem that is the exact same as what this post entails. Essentially, the past frames of the gif remain on the screen as the gif loops, creating a kind of ghosting effect. I'm using the exact same image as the post I linked uses. I have tried to implement the solution the selected answer gave, but it didn't work for me, probably because it's too advanced for me to understand.

I'm just a beginner, but I'd really like to fix this issue for a project I'm working on for my class.

I'd like some help implementing this code or finding another solution to the issue I'm having. (I'm sorry if this question is improper! I've never used Stack Overflow myself, but I've lurked around before.)

Community
  • 1
  • 1
hleichinger
  • 11
  • 1
  • 3
  • `I'm running into a problem that is the exact same as what this post entails` - tell us what the problem is. We should not need to read another question to find out what the problem is from your point of view. In any case, displaying a transparent gif as an ImageIcon on a JLabel works fine for me. That is the background is cleared before each images changes (I assume that is what your problem is?). I'm using JDK8 on Windows 7. Post your actual image and an image of the problem on the forum as was done in the linked to question, so we can see the exact problem. – camickr Jun 10 '19 at 00:12
  • Thank you very much for your input! I'll edit my post. Yes, that is the problem. I'm using the exact same image as the forum post that I linked. I'm not certain how to display the image that my JFrame is incorrectly displaying. Oh, and I'm using the latest version of JDK (I assume 8?) on OS Mojave. – hleichinger Jun 10 '19 at 00:21
  • 1
    *I'm using the exact same image as the forum post* - that gif doesn't work for me. So I would suggest the problem is the gif, not the code. Try another animated gif. – camickr Jun 10 '19 at 00:45
  • That's interesting. I've used three different gifs in total. Two are of the same 3D model Pikachu (one is doing an idle animation, on his attacking), and one is a 2D Pikachu sprite. The sprite gif is screwed up in a completely different way, though - each frame is corrupted in a way so that white pixels cover different parts of the sprite in each frame. It's very confusing, and I'm not sure how to fix it. – hleichinger Jun 10 '19 at 00:49

1 Answers1

0

Here is the code I used:

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

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        JLabel label = new JLabel( new ImageIcon("bart_simpson.gif") );
        add( label );

        setBackground( Color.YELLOW );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.pack();
//      frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

And here is the image I used:

enter image description here

I believe that is a transparent image because you can change the background of the panel and the background shows through the image.

camickr
  • 321,443
  • 19
  • 166
  • 288