0

What I want to do is:

  • Load an image that it is a gif, jpeg etc ... coming from a URL (but it is only the gif that I do not arrive but it must work with the others)

  • Put it in a JLabel in IconImage (although it suits me if it is not in the icon of the label as long as it is in the label)

  • Make it auto resize by taking all the space of the label but keeping its ratio

  • And finaly make this.add (label)

I already managed to do it with any type of image but the gifs was freeze to the first image

I searched for more solutions but could not find any that worked for me.

So, if someone could help me from the very beginning. I have the URLs and can share, if required.

Exemple of my code

//that work but images is not resize

JLabel label = new JLabel(); label.setIcon(new ImageIcon(new URL(image.getPath()))); this.add(label);

//images don't appear

URL url = new URL(image.getPath()); JLabel label = new JLabel(); java.awt.Image img = Toolkit.getDefaultToolkit().getImage(url); img = this.scaleImage(img,Toolkit.getDefaultToolkit().getScreenSize().width/6); label.setIcon(new ImageIcon(img));

  • Can you post what you've tried so far? – Rob Oct 05 '17 at 19:45
  • possible duplicate: https://stackoverflow.com/questions/10622303/loading-animated-gif-in-jlabel-weirdness – cello Oct 05 '17 at 20:01
  • //that work but images is not resize ` `JLabel label = new JLabel(); label.setIcon(new ImageIcon(new URL(image.getPath()))); this.add(label);` //images don't appear
    `URL url = new URL(image.getPath()); JLabel label = new JLabel(); java.awt.Image img = Toolkit.getDefaultToolkit().getImage(url); img = this.scaleImage(img, Toolkit.getDefaultToolkit().getScreenSize().width/6); label.setIcon(new ImageIcon(img)); this.add(label);` @Rob
    – Wind_Blade Oct 05 '17 at 20:26

1 Answers1

0

Try this, you may need to put it in a try-catch

BufferedImage image = ImageIO.read(new URL(url));
JLabel label = new JLabel(new ImageIcon(image));
Dinh
  • 759
  • 5
  • 16
  • @RSDing I get an error javax.imageio.IIOException: Can't get input stream from URL! at javax.imageio.ImageIO.read(ImageIO.java:1395) – Wind_Blade Oct 06 '17 at 08:37
  • Which URL are you using – Dinh Oct 06 '17 at 08:46
  • Ah no I just relaunched it to work ... short the problem is that the gifs and other "video" format are freeze on the first frame and moreover it returns a null if the format is a .webm – Wind_Blade Oct 06 '17 at 14:04