0

so I was messing around with a GUI and I wanted to challenge myself to display a gif. I ended up being able to do it but now I can't make the gif fit the entire frame instead of just a portion. I made the frame so you can't change the size and to be the same size as the monitor.

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

public class gif {
public static void main (String[] args) throws MalformedURLException {

    URL url = new URL("http://stream1.gifsoup.com/view2/1605810/paranoid-android-o.gif");
    Icon icon = new ImageIcon(url);
    JLabel label = new JLabel(icon);

    JFrame frame = new JFrame();

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    label.setSize(screenSize);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.getContentPane().add(label);
    frame.setTitle("what?");
    frame.setSize(screenSize);
    frame.setResizable(false);
    frame.setVisible(true);

    }
}

1 Answers1

2

now I can't make the gif fit the entire frame instead of just a portion

The JLabel will size itself based upon the ImageIcon inside. You can resize the image to the desired scale. For example:

BufferedImage image = ImageIO.read(url);
Image i2 = image.getScaledInstance(screenSize.width, screenSize.height, Image.SCALE_SMOOTH);
JLabel label = new JLabel(new ImageIcon(i2));
....

This being said, the input Image is an gif, so if animation exists in the Image you will have to take that into consideration. See this answer for resizing animated gifs.

Community
  • 1
  • 1
copeg
  • 8,290
  • 19
  • 28
  • I realize i could change it easily if it weren't animated, the problem with all the tutorials on how to do this already is i don't understand the syntax since they use a different method of putting the gif onto the frame – Spencer Hopkins Sep 02 '16 at 20:51