0

I want to show an animated gif in my application. I followed the code found here: Show animated GIF

When I run my code I get the MalformedURLException error and my application will not run. Here is what I have that's not working.

The method that calls createVisuals():

private void defaultGUI() {

    frame.setTitle("Class Map");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
    frame.setBounds(0, 0, frameWidth, frameWidth/2);
    frame.getContentPane().setBackground(Color.WHITE);
    frame.setUndecorated(true);

    try {
        Visuals.createVisuals();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
}

The method that shows the gif:

public class Visuals {

public static void createVisuals() throws MalformedURLException{

    URL cwURL = new URL("src\\images\\classmap_colorwheel-gif.gif");
    Icon cwGif = new ImageIcon(cwURL);
    JLabel cwIcon = new JLabel(cwGif);
    GUI.frame.getContentPane().add(cwIcon);
}

What am I not doing correctly?

EDIT: laksys pointed out that my URL constuction was wrong and gave a reference to fix it. The problem was that I was not giving the full file location alongside adding File: to the begining of the URL.

URL cwURL = new URL("src\\images\\classmap_colorwheel-gif.gif");

URL cwURL = new URL("File:C:/Users/01526460/Desktop/ClassMap/src/images/classmap_colorwheel-gif.gif");

This caused the exception to disappear, however the gif is not running properly. Only one frame of the gif sequence loads while the others only partially load. The gif is also looping faster than it should.

EDIT 2: I figured out that the gif was not looping properly because of the way I made it, not because of Java. I used Photoshop CS6 to create a frame animation. When I ran the frame animation at 0 second delay between frames it looked fine in Photoshop. However, when the 0 second delay is interpreted through Java, the gif is actually trying to go 0 seconds between frames. If anyone else encounters this problem make sure the delay between your frames is not set to 0. Java does not automatically control the frame rate of gifs (like many browsers do).

Community
  • 1
  • 1
Hypello
  • 3
  • 4

1 Answers1

1

I think your url construction is wrong. It may have protocol, host, port etc., please ref this

laksys
  • 3,228
  • 4
  • 27
  • 38
  • Yes. This helped and I got it to work...sortof. It is no longer causing an error, however when it displays the image sequence it only entirely loads one frame. The other frames only show a portion of themselves. The gif is also moving through its sequence faster than it should. – Hypello Jul 22 '14 at 16:34