Results 1 to 10 of 10

Thread: Loading Images

  1. #1
    jakeob22 is offline Member
    Join Date
    May 2014
    Posts
    8
    Rep Power
    0

    Default Loading Images

    Hi! Recently, I've been loading images that I needed from my computer like this:

    Java Code:
    Image image = new ImageIcon("C:/Users/jakeob/Desktop/Project/image.png").getImage();
    Obviously, when I export the project and send it to somebody the graphic can't be found because the graphic isn't on their computer. Is there a simple way to have the image load from within the project so I can export it with the code? I use Eclipse if that helps. Thanks so much! :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Loading Images

    If you put everything in a jar file, you can access the images as resources using the Class class's getResource methods.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jakeob22 is offline Member
    Join Date
    May 2014
    Posts
    8
    Rep Power
    0

    Default Re: Loading Images

    Thanks, I'll try that now!

  4. #4
    jakeob22 is offline Member
    Join Date
    May 2014
    Posts
    8
    Rep Power
    0

    Default Re: Loading Images

    I tried this this and got a null pointer exception:

    logo = new ImageIcon(Images.class.getResource("image.png")).g etImage();

    Did I do it wrong? :P

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Loading Images

    Where is the image file located? That code looks for it at the root level.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jakeob22 is offline Member
    Join Date
    May 2014
    Posts
    8
    Rep Power
    0

    Default Re: Loading Images

    It's in the src file.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Loading Images

    With the posted code, it needs to be in a folder at the root level that is on the classpath.
    Is the src folder on the classpath?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jakeob22 is offline Member
    Join Date
    May 2014
    Posts
    8
    Rep Power
    0

    Default Re: Loading Images

    I'm still sorta learning. My folder order is Project>src>me>Jakeob>Code>Images.java

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Loading Images

    The path passed to the getResource() method requires that the images be in a folder that is on the classpath. If the class file and the image file are in the same folder, the code should work.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    jakeob22 is offline Member
    Join Date
    May 2014
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Loading Images Issue
    By Daunlessk in forum New To Java
    Replies: 9
    Last Post: 11-06-2012, 04:09 AM
  2. Loading Images
    By wired-in=p in forum New To Java
    Replies: 9
    Last Post: 02-08-2012, 11:53 PM
  3. Loading Images
    By TacoManStan in forum Java Gaming
    Replies: 9
    Last Post: 09-22-2011, 11:18 PM
  4. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 09:33 AM

Posting Permissions

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