-1

I just load an animated GIF images as an ImageIcon. The image of my Swing program running on Window 2000 PC Note that I am using Java/JDK 1.4.x release.

Here is code, but it is not working:

/**
  * DevDaily.com
* A sample program showing how to use an animated gif image
  * in a Java Swing application.
  */
package giftest;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class MainFrame extends JFrame {
    JPanel contentPane;
    JLabel imageLabel = new JLabel();
    JLabel headerLabel = new JLabel();

    public MainFrame() {
        try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
            contentPane = (JPanel) getContentPane();
            contentPane.setLayout(new BorderLayout());
            setSize(new Dimension(400, 300));
            setTitle("Your Job Crashed!");
            // add the header label
            headerLabel.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 16));
            headerLabel.setText("   Your job crashed during the save process!");
            contentPane.add(headerLabel, java.awt.BorderLayout.NORTH);
            // add the image label
            ImageIcon ii = new ImageIcon(this.getClass().getResource(
                    "snoopy_dancing.gif"));            imageLabel.setIcon(ii);
            contentPane.add(imageLabel, java.awt.BorderLayout.CENTER);
            // show it
            this.setLocationRelativeTo(null);

            this.setVisible(true)
        } catch (Exception exception) {
            exception.printStackTrace();
        }
}
    public static void main(String[] args) {
        new MainFrame();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    "*i am using Java/JDK 1.4.x release*" - What... you do realize that there's already Java 8 out? Please upgrade **immediately**. A Java version **this** old is not supported anymore and is prone to security holes. – QBrute Feb 16 '17 at 18:01
  • 1
    1) That code does not compile due to a missing `;`. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) *"but it is not working"* Describe what you expected to see, and what you saw instead. 4) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 5) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Feb 16 '17 at 20:29

1 Answers1

2

This code (which is functionally equivalent to the code seen above) works on Windows 10 using Java 1.8.

Check it runs as you expected on your machine. If so, it would seem that snoopy_dancing.gif is not in the location that the code expects.

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

public class MainFrame extends JFrame {

    JPanel contentPane;
    JLabel imageLabel = new JLabel();
    JLabel headerLabel = new JLabel();

    public MainFrame() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            contentPane = (JPanel) getContentPane();
            contentPane.setLayout(new BorderLayout());
            setSize(new Dimension(400, 300));
            setTitle("Your Job Crashed!");
            // add the header label
            headerLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
            headerLabel.setText("Your job crashed during the save process!");
            contentPane.add(headerLabel, BorderLayout.NORTH);
            // add the image label
            ImageIcon ii = new ImageIcon(
                    // this.getClass().getResource("snoopy_dancing.gif")); 
                    new URL("https://i.stack.imgur.com/OtTIY.gif"));
            imageLabel.setIcon(ii);
            contentPane.add(imageLabel, BorderLayout.CENTER);
            // show it
            this.setLocationRelativeTo(null);

            this.setVisible(true);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new MainFrame();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I can't seem to find what you changed from the original code to make it work (except for proper indentation ;)). – Matthieu Jan 06 '19 at 01:42
  • 1
    OP: *"I can't seem to find what you changed from the original code to make it work"* Me: *"This code (which is **functionally equivalent** to the code seen above)"* OP: *"to make it work"* Me: *"Check it runs as you expected on your machine. If so, it would seem that **`snoopy_dancing.gif` is not in the location that the code expects**."* So try changing *`// this.getClass().getResource("snoopy_dancing.gif"));`* (commented in code) to `this.getClass().getResource("snoopy_dancing.gif"));` (bcm active code), remove the **next** line loading a GIF from a known location, and report what happens. – Andrew Thompson Jan 06 '19 at 02:01
  • @Matthieu Are you having trouble with a similar (loading & displaying an animated GIF) example? – Andrew Thompson Jan 06 '19 at 02:08
  • No, I was just poking around GIF animation and got to your answer. As you're quite a reference in Swing I was trying to understand what could possibly have gone wrong :) – Matthieu Jan 06 '19 at 08:22
  • @Matthieu Yeah I'm quite .. something, though not sure it is a 'reference in Swing'. – Andrew Thompson Jan 06 '19 at 08:57