Results 1 to 14 of 14
  1. #1
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Simple image path question

    Hello, I'm trying to create a JButton that uses a ImageIcon.
    I want to store the image I have created in a resources folder but I'm not sure where to put the resource folder. Should it be just inside my project folder or inside the src folder? Also is this the correct way to specify the file path: JButton playB = new JButton(new ImageIcon("src/gamePackage/Resources/Play.png")); ?

    I did of course first try searching the net for an answer but I couldn't find anything that told me where to put the folder or where it starts searching for it from where you specify the file path.
    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Simple image path question

    Are you going to put your code in a jar file? If so, I'd put the image in the jar file with your code so all your programs resources would be in that one file.

    Or is this a question about how folders and files and file paths work on your OS?

  3. #3
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: Simple image path question

    When I'm finished writing the program I'm going to export it as a mac OS X application bundle, so I don't think I'm using jar files at all.
    What I need to know is where to place the images so that when I export what I've written in eclipse as an actual application, the images are sort of packed inside it so that you don't actually need the images in a separate folder anymore.

    The folders I have for this project are like this:
    Project folder: bin, src
    src: gamePackage
    gamePackage: all my different classes

    Where must I put my images or is there something else that I need to do for it to work.
    Sorry if I'm not making my question clear, but I hope you understand what I mean.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple image path question

    I think you will be creating a jar file for this at some point soon, so I wouldn't dismiss information about this out of hand.

  5. #5
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: Simple image path question

    Ok, I won't. About the path finding thing: if I say JButton playB = new JButton(new ImageIcon("Play.png"));
    Where does it start looking for "Play.png" from?
    Thanks a lot for your help thus far!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Simple image path question

    In Windows it would look in the "current directory". No idea about a mac.
    To see where the program's current directory is, create a File object with the path/filename you are using and printout the File object's absolute path.

  7. #7
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: Simple image path question

    Ok, no idea how to do that but I'm sure it wont be too hard to find out, thanks very much for the suggestion. Once I find the current directory I just change the path like this: JButton playB = new JButton(new ImageIcon("src/gamePackage/Resources/Play.png")); is that correct?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Simple image path question

    The path that printed out would be for the filename you put in the File classes constructor.
    Here's a sample from my system:
    Java Code:
          File aFile = new File("someimage.png");
          System.out.println("path=" + aFile.getAbsolutePath()); 
          //path=D:\JavaDevelopment\Testing\ForumQuestions6\someimage.png
    That tells you where "someimage.png" is located from this program's point of vlew.
    You do NOT need to change the path/filename to be that path shown in the print out.
    This is the location of the current directory when I executed the program:
    D:\JavaDevelopment\Testing\ForumQuestions6\
    Last edited by Norm; 09-18-2011 at 09:48 PM.

  9. #9
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: Simple image path question

    Ok I ran that code and the path ends in the project folder where the bin and src folders are and where my image is located currently, but when I run the program from eclipse its not finding the image. It seems no matter where I put the image, eclipse can't find it. I've got images to work before but then I selected "Use project folder as root fore sources and class files" instead of "Create separate folders for sources and class files" when I created the project.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Simple image path question

    run the program from eclipse
    What prints out for the absolute path of the image file when you run in eclipse?

    You should use the same path in both constructors: File and ImageIcon

  11. #11
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: Simple image path question

    path=/Users/sebastian/Desktop/Sebastian/Java/Tests/Image icon test/Play.png

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Simple image path question

    What path did you use and was it the same path in both constructors: File and ImageIcon

    Post the code for the statements with the two constructors if you're not sure what I'm asking.

    I assume that the path is correct and that the file is in the folder shown in your last post.

  13. #13
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: Simple image path question

    I made a new project and did everything exactly the same, but strangely it worked fine. It seems I must have messed something up earlier in that project when I was experimenting...
    It probably won't work when I export it, but at least its working when I run it from eclipse which is fine for now. Thank you very much for your help, I learned some new things :)

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Simple image path question

    Glad you've solved your problem.

Similar Threads

  1. question about abstract path name
    By jperson in forum New To Java
    Replies: 1
    Last Post: 06-28-2011, 11:24 PM
  2. Real Path for Uploading Image
    By jatinkansagara in forum Java Servlet
    Replies: 4
    Last Post: 06-22-2011, 02:18 PM
  3. Simple question
    By Qsc in forum New To Java
    Replies: 6
    Last Post: 03-06-2011, 11:24 PM
  4. How to set Image file path
    By anil.bharadia in forum AWT / Swing
    Replies: 1
    Last Post: 01-28-2009, 09:10 PM
  5. Replies: 6
    Last Post: 08-06-2008, 01:41 PM

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
  •