Load Java Image inside package from a class in a different package
Asked Answered
K

6

15

I have a Java project called MyProject. I have a few different packages (keeping names simple for the purpose of this question), as follows:

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 of MyJavaFile.java, I would like to declare an Image oject of MyImage.png

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

How can I do this?

Kurdish answered 28/8, 2012 at 6:6 Comment(3)
Q: Why are you putting a .png resource in a Java package??? Even if you put stuff in a .jar file, you'll usually have a separate directory for resources ("/resources" being a Pop Favorite). For example: Add image to Jar. PS: This link answers your specific question ... but I would advise against it, if possible. IMHO...Razzia
src/PackageB/PackageBa/PackageBaa/MyImage.png package will be folderArundel
Image image = Toolkit.getDefaultToolkit().getImage("Agent.gif");Hogback
C
20

You could either call Class.getResource and specify a path starting with /, or ClassLoader.getResource and not bother with the /:

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 allow you to specify a resource relative to the class, but I don't think it allows you to use ".." etc for directory navigation.

Of course, if you know of a class in the right package, you can just use:

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

(I'm assuming you can pass a URL to the Image constructor in question. There's also getResourceAsStream on both Class and ClassLoader.)

Commerce answered 28/8, 2012 at 6:13 Comment(1)
How can I get a BufferedImage from this Image.Kurdish
M
5

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

 ImageIcon img = new ImageIcon("src/PackageB/PackageBa/PackageBaa/MyImage.png");
Mckeon answered 1/10, 2015 at 23:24 Comment(0)
A
3
/folderB/folderBa/folderBaa/MyImage.png

The image can stored into a project folder location .eg: /images/MyImage.png

Then try:

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

Using a file path is not possible when running a program that's in a jar file, especially if the program is being loaded as an applet or WebStart application then you can use ClassLoader to get image.

use the following code to load the images:

ClassLoader cldr = this.getClass().getClassLoader();

java.net.URL imageURL = cldr.getResource("/PackageB/PackageBa/PackageBaa/MyImage.png");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
Arundel answered 28/8, 2012 at 6:12 Comment(5)
I agree - Creating a directory called "/images" is a much smarter, better practice than storing your .png in a source folder/Java package. IMHO...Razzia
I need the image, not the image icon. Can I just do a getImage()Kurdish
this is incorrect. I get a null pointer. and yes, the path is absolutely correct.Kurdish
where is your image location . if it in your package folder then correct. And it is in folder location then ./foldernameArundel
And it is in folder location then foldername/image.pngArundel
M
1

This IS the best way to handle all images and icons in a JAR App.

Once you've zipped up all of your images and icons into its own JAR file - Configure your build path by adding the images JAR file into your libraries tab so that its now included in your classpath.

Then simply use the following 3x lines of code at the start of your constuctor to access any image you need for anything including a SystemTray image which doesn't accept the simple ImageIcon's as its main icon (weird I know). The 3x lines are:

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

(imageIcon is just a constructor declared Image variable) Now you can set a window icon as simply as:

setIconImage(imageIcon );

and at the same time use the same variable when setting the System TrayIcon by declaring:

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

The above allows you to declare Images or ImageIcons easily and centrally without running the risk of not keeping image resources in the right place. It keeps it nice and tidy, with the JAR containing all your images automatically compiled at run time and distribution of your program.

As a bonus, once the JAR is registered in your classpath - you can keep adding any other images into the same JAR at any time without any fuss too - Everything just works and the added images are instantly available to your app.

Much better in my view.

Mares answered 30/12, 2014 at 22:57 Comment(0)
R
0

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

// 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 that the following entries exist in the application's JAR file:

images/save.gif
images/cut.gif
Recce answered 9/9, 2013 at 10:7 Comment(0)
B
0
Image img = new Image("./src/PackageB/PackageBa/PackageBaa/MyImage.png");

This shall go the path of the image is first inside src (source) then package so the program would access the image this way.

Bismuthic answered 28/11, 2022 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.