Results 1 to 10 of 10

Thread: Loading Images

  1. #1
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Loading Images

    Im trying to load an image into a program for the first time. I don't know where to save or keep the image, so currently I'm trying to load it off of my desktop, which i figure is wrong. I realize that an Image URL is used as an argument to create an instance of ImageIcon. Right now I'm using the code
    Java Code:
    java.net.URL imgURL = getClass().getResource(path);
    , with path being a string representing the path of the image in my desktop. Eclipse keeps telling me that the file can't be found. If I shouldn't keep the image icon in my desktop, then where should i put it to be able to upload it into a program? If that isn't the problem, what is causing the file to not be found?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Loading Images

    The line you posted will attempt to locate the image from the same location as the .class file.

    This class file may be a file on your disk somewhere, or it might be part of a jar file. Putting the image in this location is reasonable: and better than putting it on the desktop. You can check the exact location that the runtime is looking for the file at by using System.out.println().

    Java Code:
    System.out.println("About to get image");
    System.out.println("Using path=" + path);
    URL imgURL = getClass.getResource(path);
    System.out.println("url is " + imngURL);

  3. #3
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Re: Loading Images

    I don't understand 'where the .class file is saved.' How do i save an image file in a jar file?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Loading Images

    I don't understand 'where the .class file is saved.'
    Well, somewhere on your disk drive there is a class file that you create by compiling your code. Where ever that is is 'where the .class file is saved'. And it is also where you should put the image file. (The chances are that the code I posted will reveal this location)

    How do i save an image file in a jar file?
    The same way you put your class files into a jar archive. IDEs have there own way of doing this. Or follow the instructions in Oracle's Tutorial in the Packaging Programs in Jar Files section. Note: if you are not already using a jar file you ought to solve the problem of having the runtime locate your image first. Before you worry about deploying the program (including the image) as a jar.

  5. #5
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Re: Loading Images

    I saved the .png file in the bin of my java project in my workspace, the folder containing the compiled .class files. I tried loading the picture using the path of the image and it still said it couldn't be found. I am familiar with creating a runnable jar file from my classes but how do I save an image file with the jar file so that we can access it from there? I may be misunderstanding what you're saying, sorry.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Loading Images

    No - I don't think you're misunderstanding. The bin folder is a likely place for the class file, and that's where the image should go.

    What output do you get when you add those three lines I suggested? I'm wondering if the value you use for path is correct: it should be the filename and extension only. Like "foo.jpg". So calling it "path" is a bit misleading. But those System.out.println()s should tell you *exactly* where the runtime is looking for the image.

    Also you said "Eclipse keeps telling me that the file can't be found". Can you confirm that this is a runtime error? And what is the exact wording?

    If you are creating a jar file using an IDE the details depend on what IDE. But with Eclipse, for example, you can put the image file next to the source java file in the "Package Explorer" window. Then when you compile, the image gets copied into the bin folder. When you construct a jar file you can specify which files get included but, by default, everything in that bin folder (including the image) should get copied into the jar.

    It's a good idea to get the image loading working first, before you add the complexity of jar files.

  7. #7
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Re: Loading Images

    Aha! i got it! thank you very much! i was using the entire path instead of the file and extension. Im going to experiment with the jar file so that picture can be stored with the jar so that entire jar can be portable.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Loading Images

    Great, I'm glad you've got it sorted out.

    The API docs for getResource() do explain how the argument is used. They take a little reading (and lead you into class loaders), but it is worthwhile.

    On Windows watch out for case: getResource() is not fussy if the image is a file in the file system, but if the image is an entry inside a jar file the case of the name must match exactly.

  9. #9
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Re: Loading Images

    i tried to use buffered images because i realize that they can be used as images rather than simply icons. I tried using this code but i also had trouble with the finding the file.
    Java Code:
    BufferedImage image = ImageIO.read(new File("image name and path"));
    What should i change to be able to find the image?

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Loading Images

    The file constructor needs the actual path to where the image is. As with the URL the first thing to do if you don't get the image is to check where the runtime is actually looking and compare that with where you know the file is.

    Java Code:
    File file = new File("image name and path");
    System.out.println("Loading image from " + file.getAbsolutePath());
    BufferedImage image = ImageIO.read(file);
    Note that ImageIO also has a read() method that takes a URL argument. This form turns out to be much more convenient if (as I think you mentioned) you end up deploying the program in a jar file. The URL form of read() can be used with or without a jar while the File form is inappropriate when the image is inside a jar.

Similar Threads

  1. Problems with loading images in a jar
    By Reskaillev in forum New To Java
    Replies: 2
    Last Post: 10-10-2011, 09:34 PM
  2. Loading Images
    By TacoManStan in forum Java Gaming
    Replies: 9
    Last Post: 09-23-2011, 12:18 AM
  3. Loading images from an Array
    By pinkette in forum New To Java
    Replies: 5
    Last Post: 04-15-2011, 12:37 PM
  4. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 10:33 AM

Tags for this Thread

Posting Permissions

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