File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call an image from another package

 
maja neskovic
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I have set images on my JPanel like this:



and then just use drawImage method.

I would like to place this image in separate package (called images) and to call it from it, but I'm not sure how to do it.

I have seen getPackage() method, but I don't know how to use it in this notation.

Hope somebody can help.
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) getClass().getResource("info.png") already returns a URL. I think that if you print getClass().getResource("info.png") and new java.net.URL(getClass().getResource("info.png"), "info.png") you would see that they are the same.

2) the resource paths are by default relative to the current class. However, that doesn't mean you're constrained to resources in the same folder. You can use resources in a folder (package) inside your current package, by adding a directory part to the resource name: "images/info.png". You can use .. to go one level down: "../info.png". And if your resource starts with a / it will be absolute: "/com/coderanch/pkg/info.png".
 
maja neskovic
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did that call: "images/info.png", but it does not work...?
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is images a sub folder of the folder where your class file is located?
 
maja neskovic
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have folder named classes (project made in NetBeans) and in it folders impl and images (folders of packages). Folder impl has class files and images. I would like to place images in folder (package) images and to call it from there.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic