aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Call an image from another package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Call an image from another package" Watch "Call an image from another package" New topic
Author

Call an image from another package

maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I did that call: "images/info.png", but it does not work...?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Is images a sub folder of the folder where your class file is located?
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
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.
 
jQuery in Action, 2nd edition
 
subject: Call an image from another package