Results 1 to 4 of 4
  1. #1
    plot is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Gettings images online

    I want to be able to get an image online and display on my JPanel but for some reason my image wont load and just shows a blank square, but when I try to load an image from my hard drive it works just fine.

    Here is what I was trying to do:

    Java Code:
    BufferedImage image;
    try
    		{
    			image = ImageIO.read(SiteMainMenu.class.getResource("insert link here"));
    	
    			
    		}
    		catch(Exception e)
    		{
    			
    		}

    If anyone can help that would be great

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Gettings images online

    Well, this will not work and before I show you what will, I will explain it to you. The getResource() method returns files in the local JAR file. You want to load from a website. You would need to make an inputstream of that image from the website and load from there. I will demonstrate.

    Java Code:
    String url = "insert link here"; //This will be a String representation of the link to the image
            URL img = new URL(url); //Here we make a URL representation of the link. I know the names are not convention, deal with it.
            InputStream in = img.openStream(); //Using the openStream method in the URL class, we can make an inputstream of the URL
            BufferedImage bi = ImageIO.read(in); //This allows the inputstream to be converted into an image using the imageIO class
    Last edited by zFollette; 01-18-2014 at 03:29 AM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Gettings images online

    That of course will work, but ImageIO.read() is overloaded to accept a URL object directly. It will open an inputstream internally.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Gettings images online

    Quote Originally Posted by jim829 View Post
    That of course will work, but ImageIO.read() is overloaded to accept a URL object directly. It will open an inputstream internally.

    Regards,
    Jim
    I am aware. I am also aware that you do not have to make a String Object, because URL takes a string as a parameter. I was just trying to milk out the process to show exactly what was going on.

Similar Threads

  1. Replies: 0
    Last Post: 11-14-2012, 12:15 PM
  2. Replies: 0
    Last Post: 12-16-2010, 06:14 PM
  3. Replies: 4
    Last Post: 12-16-2010, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •