This discussion is archived
11 Replies Latest reply: Feb 19, 2012 1:49 PM by DarrylBurke RSS

Image Loading

colin-java Newbie
Currently Being Moderated
Hi...

I'm able to load (as java.awt.Image's) images inside the project (located with the source code) from both netbeans and after exporting to jar/jnlp.

But supposing I have a folder (res) of images in the folder that the projects jar file is in. I'm unable to load these images into the program.
I thought this would work...
return Toolkit.getDefaultToolkit().getImage("res/myImage.jpg");

But it doesn't, any ideas how to fix this?
  • 1. Re: Image Loading
    DarrylBurke Guru Moderator
    Currently Being Moderated
    What does your question have to do with Java FX 2, which is the subject of this forum?

    db
  • 2. Re: Image Loading
    EJP Guru
    Currently Being Moderated
    But supposing I have a folder (res) of images in the folder that the projects jar file is in. I'm unable to load these images into the program.
    I thought this would work...
    return Toolkit.getDefaultToolkit().getImage("res/myImage.jpg");
    Why did you think it would work? Unless the current directory when you execute contains the 'res' directory, it won't.
  • 3. Re: Image Loading
    colin-java Newbie
    Currently Being Moderated
    I'm pretty sure thats what I used in normal java, so assumed it would work here.

    So basically I have.... res and ABC.jar in same directory
    Should this work, or is it something in javafx that stops it?
  • 4. Re: Image Loading
    EJP Guru
    Currently Being Moderated
    So basically I have.... res and ABC.jar in same directory
    So?
    Should this work
    Only if the cwd is where the JAR is. Where the JAR is doesn't have any magical properties by itself.

    As Moderator, I am moving this to the AWT forum where it belongs.
  • 5. Re: Image Loading
    colin-java Newbie
    Currently Being Moderated
    [oops sorry edited this - EJP]
    ...
    So I guess that directory is the CWD, but yet it doesn't load
    ...

    Edited by: EJP on 17/02/2012 15:41
  • 6. Re: Image Loading
    DarrylBurke Guru Moderator
    Currently Being Moderated
    EJP wrote:
    As Moderator, I am moving this to the AWT forum where it belongs.
    Thank you.

    db
  • 7. Re: Image Loading
    EJP Guru
    Currently Being Moderated
    Which suggests that your guess is wrong.

    @Daryl I think you could cool off a touch ;-)
  • 8. Re: Image Loading
    DrClap Expert
    Currently Being Moderated
    colin-java wrote:
    So basically I have.... res and ABC.jar in same directory
    Should this work, or is it something in javafx that stops it?
    As EJP already said, it will only work if that "same directory" is the current working directory. If it worked in "normal Java" then that just means that the current working directory happened to be set so that it worked.
  • 9. Re: Image Loading
    colin-java Newbie
    Currently Being Moderated
    I never had this problem before, the images would load wherever the jar file was, as long as the res folder was in the same directory as jar file.

    I tried to printout the cwd when running the jar file using:
    String curDir = System.getProperty("user.dir");

    But I get an error

    Any idea how to get this to work?
  • 10. Re: Image Loading
    colin-java Newbie
    Currently Being Moderated
    Nevermind, I seem to have fixed it, I needed to make use of the MediaTracker
  • 11. Re: Image Loading
    DarrylBurke Guru Moderator
    Currently Being Moderated
    EJP wrote:
    Which suggests that your guess is wrong.

    @Daryl I think you could cool off a touch ;-)
    My question wasn't rhetoric. Since the OP had been posting questions in the FX 2 forum, I wanted to make an informed decision whether to move the thread or not.

    Nothing in my post implied that I wasn't cool. That was just your assumption.

    db

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points