3

I know, I know, this has been asked before. But every resource I've looked at uses IconImages while I just have plain Images.

Is there a different solution? Please help as I've been stuck researching and trying to figure this out for days now with no progress.

Image Floor = Toolkit.getDefaultToolkit().getImage("Floor.PNG");

EDIT: If I was to make sure the jar wouldn't compress and I created a seperate directory in the jar for images and put the correct file path, would this code work?

DGH94
  • 97
  • 1
  • 6

6 Answers6

3

Toolkit#getImage(String s) looks for a file and likely your image is in the Jar and is now a resource not a file. Look to using resources for this.

Note that ImageIO.read(...) can accept an InputStream parameter the Class class has a method, getResourceAsStream(...) which can put the resource into a Stream that ImageIO can read. Give that a try.

Also, are you getting any error messages when you try what you're doing?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • No errors whatsoever. And I'd like to add that if the jar is in the same directory as the images, it works fine, but as soon as I yank it out of there and on to the desktop alone, it goes all gray and bleak. – DGH94 Feb 22 '11 at 23:24
  • Please translate "yank it out of there and on to the desktop alone..." you may be assuming that we know more about your situation than we actually do. – Hovercraft Full Of Eels Feb 22 '11 at 23:25
  • Haha, sorry. I originally had the jar file in the same directory as the images, and it worked fine. I then moved the jar outside of that directory onto the desktop, where it couldn't access the images and thus did not display them. – DGH94 Feb 22 '11 at 23:38
1

Check the extension of files. I had this problem because the extension was "PNG", when I changed it to "png", everything was ok.

Mahsa
  • 85
  • 1
  • 11
1

Make sure you know what your current directory is, and how it relates to the position of the files in your jar.

Here's how I would handle it.

1) Require there to be a file called "images.txt" in the directory with your jar (or bundle it into the jar.)
2) Make a file called "images.txt" with a format like `FLOOR:C:\\images\\floor.png`
3) Load this file into memory on load.
4) Load your images based on the entries in the file

This will give you the advantage of changing your images without changing your code if it's defined outside the jar :)

It's not loading because you're not putting the path to the images in the declaration. It expects the images to be wherever the jar is (notice there's no directories there)

You need to offload the definition of the file names to a file, or at the very least guarantee the relative position of the files.

Another good option is to put the images in the jar itself, say in an img directory, and reference them there. But then changes to the images require a new jar, which may not be desired for development purposes.

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • The path to the image is correct, that I'm certain. It's just that when I launch the jar, it is a gray screen with no images whatsoever. And I know for ImageIcon you use: `new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg")` but I have not found anything similar for plain Image. – DGH94 Feb 22 '11 at 23:16
  • Does any of the logic work? Is your jar manifest properly defined? – corsiKa Feb 22 '11 at 23:20
  • Yes, my manifest file works fine, and if the jar is in the same directory as the images, the images appear. – DGH94 Feb 22 '11 at 23:23
  • Updated my answer what I would do in your shoes. – corsiKa Feb 22 '11 at 23:26
  • Haha, I forgot to add that I have the images within the jar itself, AND that they are not in a separate folder within the jar (unless it creates one automatically?) To be 100% honest, I have only just started my first programming class in high school, so this seems a little above my head, but I'll definitely keep it in mind if I can't find another solution. – DGH94 Feb 22 '11 at 23:33
1

The getImage call is looking in the file system working directory, not inside the Jar file. This is why the jar file loads the images successfully when they are placed in the same directory outside the jar file. If the images are bundled in the jar file, they are no longer file system files to be accessed, but rather Jar file resources. There is a different way to load these, but sorry, I don't know it off the top of my head.

Zak
  • 24,947
  • 11
  • 38
  • 68
0

You can't expect a JAR file to magically know where your images are. If you put a JAR file alone on the desktop, it's going to look for the files on the desktop! The code

getImage("Floor.png")

searches the current directory of the JAR (or source project) by default and you'd expect that if the JAR was in the same directory, it would work. If the JAR is on the desktop how does it know where Floor.png is? Of course, you can specify a hard-coded path

getImage("C:\Some Folder Path\Floor.png")

but then Floor.png has to be in C:\Some Folder Path\ for the JAR to work properly.

It sounds like what you really want to do is keep the images in the JAR file (which acts like a ZIP file). The tutorial on doing that is here:

http://download.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource

donnyton
  • 5,874
  • 9
  • 42
  • 60
0

And I know for ImageIcon you use: new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg") but I have not found anything similar for plain Image.

<head-desk /> You should really get into reading the JavaDocs. Otherwise you are 'coding by magic'. Which generally won't work.

URL urlToImage = getClass().getResource("myimage.jpeg");
// If you need to support Java 1.3
Image image = Toolkit.getDefaultToolKit().getImage(urlToImage);
// If your users have dragged their JRE into this millennium
BufferedImage bufferedImage = ImageIO.read(urlToImage);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433