Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: ImageIcon Erratically Loading URL Image

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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:
        			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:
    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?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ImageIcon Erratically Loading URL Image

    You'd think they would have mentioned that in the API.

    I am getting the following error:
    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
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Loading array
    By joshft91 in forum Collections and Generics
    Replies: 3
    Last Post: January 19th, 2011, 11:30 AM
  2. Loading Images and Edit it
    By shadihrr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 25th, 2010, 08:39 PM
  3. Loading in images
    By eXcellion in forum Java Theory & Questions
    Replies: 3
    Last Post: January 17th, 2010, 12:13 PM
  4. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM
  5. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 04:39 AM