-1

Well basically I tried to display a .gif using a url but it's given me a null pointer exception and I'm not really sure why since my url is correct and there's no other problems with my code(At least none that I can see).

   import javax.swing.*;
   import java.net.*;
public class image {

public image() {

}

public static void main(String[] args) {


    URL url = image.class.getResource("<http://cdn.osxdaily.com/wp-content/uploads/2013/07/dancing-banana.gif>");
    ImageIcon imageIcon = new ImageIcon(url);
    JLabel label = new JLabel(imageIcon);
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    panel.add(label);
    frame.add(panel);
    frame.setTitle("Title");  
    frame.setSize(700,500);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);


         }

    }
user4601488
  • 93
  • 1
  • 2
  • 8

1 Answers1

5
URL url = image.class.getResource("<http://cdn.osxdaily.com/wp-content/uploads/2013/07/dancing-banana.gif>");

is not how you reference an image from a web resources. You would use this method to load resources that are embedded within your application (within the context of the applications classpath)

URL url = new URL("http://cdn.osxdaily.com/wp-content/uploads/2013/07/dancing-banana.gif");

would probably work better...

Remember, that downloading and loading the image may take some time, you may want to use a MediaTracker to track the progress, this would allow you to provide feedback to the user and know when to update the screen with the image once it's available, for example.

Before anyone asks, I choose not to use ImageIO to load an animated gif, because that is just a lot more work (as demonstrated here - not for the faint hearted). In this case, the MediaTracker could be used to check for errors

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • +1 Interesting... I thought `ImageIcon imageIcon = new ImageIcon(url)` would block until the data is reseaved and treated. – Dima Maligin Feb 27 '15 at 00:06
  • 2
    @DimaMaligin No, this is actually one of the issues with this class (and why I prefer `ImageIO`), but having said that, the class was designed way back when we use to have 14.4k and 28.8k modems! Oh the joy. So they were designed to off load the physically loading of the images into the background and allow the code to continue running. This is why we have things like `MediaTracker` and `ImageObserver`, these provide information about the progress of images as they are been loaded. I'm not a fan of `ImageIcon` in this way, because it can fail silently, hence the extra need for `MediaTracker` – MadProgrammer Feb 27 '15 at 00:10
  • @MadProgrammer And what other objects can load .gif,png, or any images in general that's faster than ImageIO? – user4601488 Feb 27 '15 at 00:16
  • I never use `ImageIcon` instead I usually do `BufferedImage bi = ImageIO.read(whateva);` can you confirm the safeness of this? – Dima Maligin Feb 27 '15 at 00:16
  • It's not that `ImageIcon` is faster than `ImageIO`, they just do the work differently. I prefer `ImageIO` as it can load a wider range of images, won't return till the image is realised or if an error occurs, it's easier to manage. `ImageIcon`, however, is the easiest way to load an animated `gif`, as you don't need to try and manage the frames yourself. Personally, I prefer `ImageIO` – MadProgrammer Feb 27 '15 at 00:21
  • @DimaMaligin If you're really determined, you can use `ImageIO` to read animated gif's, as demonstrated [here](http://stackoverflow.com/questions/22188940/gif-image-doesnt-moves-on-adding-it-to-the-jtabbed-pane/22190844#22190844), but the difference between 4-5 lines and 100+ lines for code seems obvious which one would be preferable in most cases – MadProgrammer Feb 27 '15 at 00:28
  • @MadProgrammer That a bit of an overkill, but I'm pretty sure it can be done with less code. – Dima Maligin Feb 27 '15 at 00:39
  • @DimaMaligin Animating an animated gif through the use of `ImageIO`, you might get a few lines of less code, but the basic premise would remain the same ;) - Remember, it was trying to get as close to `ImageIcon` as possible ;) – MadProgrammer Feb 27 '15 at 00:43
  • @DimaMaligin I want proper dispose methods! – MadProgrammer Feb 27 '15 at 00:57
  • @MadProgrammer well, you hevent implemented them yous self. and im going for less code. – Dima Maligin Feb 27 '15 at 01:00
  • @MadProgrammer will see, maybe it'll turn out to be handy and reusable. – Dima Maligin Feb 27 '15 at 01:04
  • @MadProgrammer Well, this GIF thing have proven to be far more interesting than I assumed. 1 hour through I decided to abandon `ImageIO` completely and implement my own `Gif` class, by `extending` on `JComponent`, that will `repaint` it self, so you can basically use it just like any other `Swing` element. When I said abandon `ImageIO` I men't it. So decided to decode the whole thing my self(not sure if this was a good or bad idea). I'm having some trouble reading in the code stream, most documentation on the compressed stream is not 100% accurate. Any chance you now of a reliable source? – Dima Maligin Mar 01 '15 at 01:50
  • @MadProgrammer I would post my code but I don't think It'll get good response if I just spam it as an answer. – Dima Maligin Mar 01 '15 at 01:51
  • @DimaMaligin No idea, I'd be looking at the rfc, but it could be out-of-date. The code I used was part diagnostics and part presentation – MadProgrammer Mar 01 '15 at 02:01
  • @MadProgrammer Well, I have [this](http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp) as my source, I read all the data in, including the codes for the ImageData, according to the info from the article the last code should be EOI, but its not. there is a section there that says the ImageData is packed in blocks, and ends with the `00` byte, but it doesn't say if any action is needed between the data blocks. I guess I'll have to figure it out... ohh well.. seems like a good practice. – Dima Maligin Mar 01 '15 at 02:13
  • @DimaMaligin Seems like madness...but that's what makes it fun ;) – MadProgrammer Mar 01 '15 at 02:17
  • @MadProgrammer i will post my result when ill get it to work. btw it will include proper disposal methods. all the design is already set up. i only need to decode the freacking frames, which i will. its just a matter of time. – Dima Maligin Mar 01 '15 at 02:23