Load the Java image inside the package from the class to another package

I have a Java project called MyProject. I have several different packages (the simplest names for the purpose of this question):

src/PackageA src/PackageA/PackageAa src/PackageA/PackageAa/PackageAaa src/PackageB src/PackageB/PackageBa src/PackageB/PackageBa/PackageBaa 

I have a class

 src/PackageA/PackageAa/PackageAaa/MyJavaFile.java 

And I have an image

 src/PackageB/PackageBa/PackageBaa/MyImage.png 

Inside MyJavaFile.java , I would like to declare an Image object MyImage.png

 Image img = new Image(....what goes here?...) 

How can i do this?

+14
source share
5 answers

You can call Class.getResource and specify a path starting with / , or ClassLoader.getResource and not worry with / :

 URL resource = MyJavaFile.class .getResource("/PackageB/PackageBa/PackageBaa/MyImage.png"); 

or

 URL resource = MyJavaFile.class.getClassLoader() .getResource("PackageB/PackageBa/PackageBaa/MyImage.png"); 

Basically, Class.getResource will let you specify the resource relative to the class, but I don't think it allows you to use "..", etc. to navigate directories.

Of course, if you know the class in the correct package, you can simply use:

 URL resource = SomeClassInPackageBaa.class.getResource("MyImage.png"); 

(I assume that you can pass the URL to the Image constructor, as well as getResourceAsStream to Class and ClassLoader .)

+16
source

you can use the relative path since the relative path is the project folder.

  ImageIcon img = new ImageIcon("src/PackageB/PackageBa/PackageBaa/MyImage.png"); 
+4
source
 /folderB/folderBa/folderBaa/MyImage.png 

The image can be stored in the project location folder .eg: /images/MyImage.png

Then try:

 Image img = new Image(/images/MyImage.png); 

Using the file path is not possible when starting the program in the jar file, especially if the program loads as an applet or WebStart application, then you can use ClassLoader to get the image.

use the following code to upload images:

 ClassLoader cldr = this.getClass().getClassLoader(); java.net.URL imageURL = cldr.getResource("/PackageB/PackageBa/PackageBaa/MyImage.png"); ImageIcon aceOfDiamonds = new ImageIcon(imageURL); 
+2
source

This is the best way to handle all images and icons in a JAR application.

Once you have attached all your images and icons to your own JAR file - Customize your build path by adding the JAR image file to the Libraries tab so that it is now included in your class path.

Then simply use the following 3x lines of code at the beginning of your constuctor to access any image you need for anything, including a SystemTray image that does not accept a simple ImageIcon as the main icon (strange, I know). Lines 3x:

  URL iconUrl = this.getClass().getResource("/image-iconb.png"); Toolkit tk = this.getToolkit(); imageIcon = tk.getImage(iconUrl); 

(imageIcon is just declared by the Image variable constructor) Now you can set the window icon in the same way as:

 setIconImage(imageIcon ); 

and use the same variable when setting System TrayIcon, declaring:

  trayIcon = new TrayIcon(imageIcon, "SystemTray Demo", popupMenu); 

The above allows you to easily and centrally declare Images or ImageIcons without risking not storing image resources in the right place. This keeps it beautiful and neat: a JAR containing all of your images automatically compiled at run-time and distributed by your program.

As a bonus , once a JAR is registered on your way to the class, you can add any other images to the same JAR at any time at any time without fuss. Everything just works, and the added images are instantly available to your application.

Much better in my opinion.

+1
source

Use the getResource method to read resources inside the src root. For example, the following code retrieves images from the src / images folder.

 // Get current classloader ClassLoader cl = this.getClass().getClassLoader(); // Create icons Icon saveIcon = new ImageIcon(cl.getResource("images/save.gif")); Icon cutIcon = new ImageIcon(cl.getResource("images/cut.gif")); 

The example assumes the following entries are in the application JAR file:

 images/save.gif images/cut.gif 
0
source