0

I have a problem with displaying my gif with a transparent background because when I start this program, my gif shows but all pictures of gif stayed on the canvas. Knows somebody some solution, please?

enter image description here enter image description here

This is my code:

package zkouska3;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class mainl extends JFrame{
    private Image TestImage;

    private BufferedImage bf;

    public static void main(String[] args) {
        new mainl();
    }

    public mainl() {
        setSize(500, 500);
        imageLoader();
        setVisible(true);
    }
    public void imageLoader() {
        try {
            TestImage = new ImageIcon(this.getClass().getResource("pandulak.gif")).getImage();
        }catch(Exception e) {

        }
    }
    @Override
    public void paint(Graphics g){
         g.drawImage(TestImage, 0, 0, 300, 300, this);
}

}
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Belafon
  • 3
  • 1
  • 1
  • 2
  • My guess is, you're going to have to play around with the "dispose method", which basically tells the renderer of the gif what it should do between each frame (incrementally add them or clear them). You can look at [this](https://stackoverflow.com/questions/26330550/java-gif-animation-not-repainting-correctly/26331052#26331052) and [this](https://stackoverflow.com/questions/36671767/java-swing-gif-partly-transparent-when-its-not-supposed-to-be/36683021#36683021) and [this](https://stackoverflow.com/questions/36682135/java-animated-gifs-go-automatically-partially-transparent/36682907#36682907) – MadProgrammer Dec 20 '19 at 20:40

2 Answers2

0

You've overridden the paint method in your JFrame. This means that the JFrame itself no longer gets painted (or to put it more simple, the background of your image never gets painted). Insert a call to super.paint(g) in the paint method above where you paint the image, so that the background gets painted over the previous gif image every time a new gif image shows:

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(TestImage, 0, 0, 300, 300, this);
}
Pieter12345
  • 1,713
  • 1
  • 11
  • 18
  • I've just tried both cases locally and with your code + my modification, it works as expected (using a .gif with a transparent background). I've also reproduced your problem when not calling `super.paint(g);`. – Pieter12345 Dec 21 '19 at 01:39
0

here another one How to fix .gif with corrupted alpha channel (stuck pixels) collected with Graphicsmagick?

Your gif is disposal = 3 that means it needs previous image as it renders incrementally. The problem is the image is with black background and not white ...

Here are the disposals possible:

     if (disposal==0) s="no animation";
else if (disposal==1) s="leave image as is";
else if (disposal==2) s="clear with background";
else if (disposal==3) s="restore previous image";
else                  s="reserved";

When I render it with my decoder it looks like this:

[![capture][1]][1]

So there are 2 possible things at play here:

  1. transparency

maybe this should be handled as transparent image with background but even decent image viewer (like FastStone Image Viewer) shows the same thing so I doubt this is the case...

  1. extentions

This is the most likely cause. Nowadays WEB browsers (for few years now) depend on undocumented custom made extentions added to GIFs extention packets (and not part of any GIF specs) and ignores the GIF file format completely for some aspects of rendering (like looping). Simply because all of them use the same image lib for decoding GIFs which is simply coded badly (or by design)...

for more info see:

So my guess is the GIF of yours have some extention packet telling brownser to use different disposal method then the one stored in GIF header. So simply your GIF is buggy and only buggy GIF decoder can render it properly ...

So your decoder ignores the background color of GIF hence rendering incorrectly as the incremental render does not work with non black color background ...

And yes those white lines are with gaps ... its not aliasing ... [1]: https://i.stack.imgur.com/6Kbbp.gif

Spektre
  • 49,595
  • 11
  • 110
  • 380