ImageIcon Erratically Loading URL Image
I've got an interesting issue.
I have a class that extends JPanel and paints its background to be an Image that it gets from a URL. I am using ImageIcon to build and paint the image.
Now, while this works fine on my personal computer, for some reason it doesn't work on my computer at work. I have tested it with a bunch of different images, but all yield the same result.
Here is the snippet of code for when I build the ImageIcon:
Code java:
try{
URL url = new URL("http://www.java2s.com/Tutorial/JavaImages/ButtonwithImageIcon.PNG");
ImageIcon anotherIcon = new ImageIcon(url);
currentImage = anotherIcon;
}catch(Exception ex){ex.printStackTrace(); }
And it is painted by saying:
Code java:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
currentImage.paintIcon(this,g,(int)x,(int)y);
...}
Like I said, this works on my personal computer, but it would seem that ImageIcon is just not working in general on my work computer. What would be causing this?
Re: ImageIcon Erratically Loading URL Image
Note that creating an ImageIcon from a URL does not throw an exception if the image cannot be loaded. If you are behind a firewall, or there is some other network issue in another location which prevents the image from loading you won't know given the lack of Exception handling in this situation (unless you validate the ImageIcon Image with/height or something similar). Use ImageIO.read(URL), which throws an IOException that you can catch and handle appropriately.
Re: ImageIcon Erratically Loading URL Image
You'd think they would have mentioned that in the API.
I am getting the following error:
Quote:
javax.imageio.IIOException: Can't get input stream from URL!
at javax.imageio.ImageIO.read(ImageIO.java:1369)
at MapPanel$1.run(MapPanel.java:184)
at MapPanel.loadImages(MapPanel.java:191)
at MapPanel.<init>(MapPanel.java:63)
at GUI.<init>(GUI.java:374)
at MapRouting$FullScreen.<init>(MapRouting.java:75)
at MapRouting.main(MapRouting.java:39)
Caused by: java.net.ProtocolException: Server redirected too many times (20)
at sun.net.
http://www.protocol.http.HttpURLConn...tion.java:1323)
at java.net.URL.openStream(URL.java:1010)
at javax.imageio.ImageIO.read(ImageIO.java:1367)
... 6 more