Results 1 to 14 of 14
Thread: Simple image path question
- 09-18-2011, 08:22 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
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.
- 09-18-2011, 08:56 PM #2
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?
- 09-18-2011, 09:14 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
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.
-
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.
- 09-18-2011, 09:35 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
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!
- 09-18-2011, 09:38 PM #6
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.
- 09-18-2011, 09:42 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
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?
- 09-18-2011, 09:46 PM #8
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:
That tells you where "someimage.png" is located from this program's point of vlew.Java Code:File aFile = new File("someimage.png"); System.out.println("path=" + aFile.getAbsolutePath()); //path=D:\JavaDevelopment\Testing\ForumQuestions6\someimage.png
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.
- 09-18-2011, 09:56 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
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.
- 09-18-2011, 09:58 PM #10
Re: Simple image path question
What prints out for the absolute path of the image file when you run in eclipse?run the program from eclipse
You should use the same path in both constructors: File and ImageIcon
- 09-18-2011, 10:04 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
Re: Simple image path question
path=/Users/sebastian/Desktop/Sebastian/Java/Tests/Image icon test/Play.png
- 09-18-2011, 10:08 PM #12
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.
- 09-18-2011, 10:32 PM #13
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
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 :)
- 09-18-2011, 10:34 PM #14
Similar Threads
-
question about abstract path name
By jperson in forum New To JavaReplies: 1Last Post: 06-28-2011, 11:24 PM -
Real Path for Uploading Image
By jatinkansagara in forum Java ServletReplies: 4Last Post: 06-22-2011, 02:18 PM -
Simple question
By Qsc in forum New To JavaReplies: 6Last Post: 03-06-2011, 11:24 PM -
How to set Image file path
By anil.bharadia in forum AWT / SwingReplies: 1Last Post: 01-28-2009, 09:10 PM -
restrict users from entering the image/text files path directly in the browser.
By vishnujava in forum Java ServletReplies: 6Last Post: 08-06-2008, 01:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks