Using links in different folders for images (Java) - java

I'm designing a calculator with customised buttons. Naturally I want to organise my image files in folders different to the package interfaces.
The location of the folders is interfaces/Seven/seven-normal.png but whenever I don't include the full link
"C:\Liloka\Source\interfaces\Seven\seven-normal.png"
it doesn't work and everything disappears. I swear I have seen this being done in normal code. If I used this in proper programs I can't expect people to be changing the link to where they've put the code! Here's the code I've been using:
seven = new ButtonImage("/Seven/seven-normal.png"); - Doesn't work
nine = new ButtonImage("C:/Users/Liloka/workspace/WebsiteContent/src/interfaces/Nine/nine-normal.png"); - Does work
Thanks!

"/Seven/seven-normal.png"
...is a path to C:\Seven\seven-normal.png - because of the / at the very beginning of your path, which essentially means, from the root of the drive, go to the "Seven" folder, and then load "seven-normal.png"
You have to use a relative path, something like, "../../interfaces/Seven/seven-normal.png" or maybe just "interfaces/Seven/seven-normal.png"
The first path will take you "up" two folders, and then down to interfaces/Seven/seven-normal.png. Essentially, you have to figure out what folder your code is running under, also called the "working directory", and construct a relative path from there.

Just delete the first forward slash.
seven = new ButtonImage("interfaces/Seven/seven-normal.png");
interfaces is the folder that should be in the same folder as your JAR.

Related

Path, relative, direct

I know there were several similar questions, however, examples in them don't make things clear or I can't make profit of them - Shame on me.
So my problem is with loading images in simple app with GUI.
e.g.:
I got images in "D:\javaeclipseprog\Graphics\src\images", class and java files in "D:\javaeclipseprog\Graphics\src\app"
When I use direct path: "D:/javaeclipseprog/Graphics/src/images/icon.jpg" everything works, but as good practice I would like to get them from relative path, which as far as I know should be: "./images/icon.jpg".
Unfortunately it doesn't work.
Any help appreciated, thanks in advance.
When you are running it in eclipse, your default working directory in the project directory. That is the directory where srcis located in. In your example the project directory is:
D:/javaeclipseprog/Graphics
Therefore the correct path is:
./src/images/trophy.png
Edit: Just want to add that you could also load a file via a path relative to the class location by using the getResource method.
../../images/icon.jpg should work fine
You're going two folders up and go straight to the right folder.
Paths
A simple way to check this would be to use the Paths and Path classes and methods.
Path p1 = Paths.get("D:\\javaeclipseprog\\Graphics\\src\\app\\java.class");
Path p2 = Paths.get("..\\..\\images\\icon.jpg");
System.out.println(p1.resolve(p2).normalize()); // D:\javaeclipseprog\Graphics\src\images\icon.jpg
I'll use the inverted slash because it seems that you use windows. In this case .\ indicates that the same directory where the code is, will have the file you want to use. If you want to jump into the father of that directory, the one that contains the source, you'll use ..\
You can even do it more tan once, for example ..\..\ would be a valid path. Try adding quantities of ..\ in order to look for the directory you want. In this chase ..\src\images\icon.jpg (the parent on a java project is src)
Another important thing is that you're using / instead of \\ that would be the symbol of the directory separator on windows (\ is an special char that must be scaped using an aditional \) For portability i'd use:
String sep = System.getProperty("file.separator");
String path = ".."+sep+"src"+sep+"images"+sep+"icon.jpg"
I think what you need to use is "../images/icon.jpg".
Using it like you have it will look in the current directory, which unless I'm understanding it incorrectly, is "D:\javaeclipseprog\Graphics\src\app".

Referencing an image inside a jar file when running junit

I have been playing with the idea of using ImageMagic (im4java) to do a comparison of known good page renders against stored good pages.
I have got this working on a test site, but all my images (good, bad and differrent) are stored in my c:\temp folder. I have been toying with the idea of having the "expected" images kept inside the project folder structure, so when the project is checked out, the expected images are there.
(not saying this is a great solution, this is just something I have been playing with.)
So my test is stored in
/src/test/java/my.screen.test/compareTest.java
and I have my "expected" image in
/masterImages/test.png
I have tried various ways to reference this:
I included masterImages in the build path and then tried to use
InputStream input = getClass().getResourceAsStream("/masterImages/googleHomePage1.png");
(I then thought I could simply use input.toString() to pass into im4java - but the InputStream gave me nullpointer exception)
I also tried removing the masterImages from the buildpath and trying it that way.
I have also tried
String path = getClass().getClassLoader().getResource("masterImages/googleHomePage1.png").toString();
Again, null pointer. I know there is something stupid I am not seeing here, like I said this started as me playing but it's now annoying me why I can't get it to work.
Any insights into what I am missing greatly appreciated.
From - In java, how do you retrieve images from a jar file?
It is indeed simple: you use the various getResource() methods in java.lang.Class and java.lang.ClassLoader. For example, in your app, you could just write
treeURL = getClass().getResource("/images/tree.png");
This would find the file in an images directory at the root of the jar file. The nice thing about the getResource() methods is that they work whether the files are in a jar or not -- if the images directory is a real directory on disk, this will still work (as long as the parent of images is part of your class path.)
Thanks to all. I think I have this sorted. Turns out I had to add the folder containing the image to the classpath - I wrongly assumed that as I had masterImages/otherFolderName that any file inside otherFolderName would be included if I included masterImages. Turns out this is not the case (at least for me.)

Accessing Images in Eclipse

I'm a teacher attempting to use the AP Computer Science Picture Lab activity. Here are the teacher instructions:
Students should keep the images folder and the classes folder together in the pixLab folder.
The FileChooser expects the images to be in a folder called images, at the same level as the classes folder.
If it does not find the images there it also looks in the same folder as the class files that are executing.
If you wish to modify this, change the FileChooser.java class to specify the folder where the pictures are stored. For example, if you want to store the images in “r://student/images/,” change the following line in the method getMediaDirectory() in FileChooser.java:
URL fileURL = new URL(classURL,"../images/");
And modify it to
URL fileURL = new URL("r://student/images/");
I have created a GitHub repo for them to fork and use in Eclipse, but I'm having trouble getting the images in the right place for Eclipse to see them. Where should they be in the Eclipse Package Explorer? The tree now is:
PixLab > src > default package > various classes.
At what level should I drag and drop the images folder into?
Alternatively, what should I the following line to read?
URL fileURL = new URL(classURL,"../images/");
I'm wondering if there is some confusion between the naming of the project in Eclipse with the folder name for the source files.
The directory structure that you give does not include a pixLab folder. I'd be expecting something more like the following in Eclipse:
PixLab > src > pixLab > various classes
> images > various images
Then, the line:
URL fileURL = new URL(classURL,"../images/");
makes sense, as you are going up one level from the classes folder, and down from there to the images folder.
For the reference to a "student" folder, I think they are considering the scenario where the images are stored in a shared network file folder. In that case, they would NOT be part of the Eclipse package, and the FileChooser Url would have to be modified to reflect your chosen network location for the images.
If you've already set up the project in Eclipse with the "default package", can I suggest doing the following:
From within Eclipse:
1) make a package, named pixLab
2) drag & drop files from the default package to the new package
Eclipse should automatically add the following line to the top of all the source files that you brought over:
package pixLab;
3) place the images folder under src, parallel to the pixLab package.
A "package" functions as a folder. If everything is in the same folder, then the rest of the code should work fine.
Thanks for the attempt, but I'm afraid Java still can't find the jpg images. I'm afraid I'm not yet allowed to post images, but I took a screenshot, and the files are laid out exactly as suggested above. Any other ideas, anyone?

Json Relative Path

I use Java and libgdx for my application. I'm currently trying to describe a ui style with a json file, but I must give a path to a texture file. The absolute path is no problem, but how I can use a relative path especially go 2 folders back and then go through a few folders? I tried "../" but it doesn't seem to work.
Thanks for any help.
You go:
Gdx.files.internal("Path to project");
This will return a File that you can then use in your own ways. If you want to access the "root directory", which is the higher "level", put a / before the path, just like this:
Gdx.files.internal("/Path/ui.json");

Why is my BufferedImage receiving a null value from ImageIO.read()

BufferedImage = ImageIO.read(getClass().getResourceAsStream("/Images/player.gif"));
First of all, yes I did add the image folder to my classpath.
For this I receive the error java.lang.IllegalArgumentException: input == null!
I don't understand why the above code doesn't work. From everything I read, I don't see why it wouldn't. I've been told I should be using FileInputStream instead of GetResourceAsStream, but, as I just said, I don't see why. I've read documentation on the methods and various guides and this seems like it would work.
Edit: Okay, trying to clear some things up with regards to what I have in the classpath.
This is a project created in Eclipse. Everything is in the project folder DreamGame, including the "Images" folder. DreamGame is, of course, in the classpath. I know this works because I'm reading a text file in /Images with info on the gif earlier on in the code.
So I have: /DreamGame/Images/player.gif
Edit 2: The line that's currently in the original post is all that's being passed; no /DreamGame/Images/player.gif, just /Images/player.gif. This is from a method in the class ImagesLoader which is called when an object from PlayerSprite is created. The main class is DreamGame. I'm running the code right from Eclipse using the Run option with no special parameters
Trying to figure out how to find which class loader is loading the class. Sorry, compared to most people I'm pretty new at this.
Okay, this is what getClassLoader() gets me: sun.misc.Launcher$AppClassLoader#4ba778
getClass().getResource(getClass().getName() + ".class") returns /home/gixugif/Documents/projects/DreamGame/bin/ImagesLoader.class
The image file is being put in bin as well. To double check I deleted the file from bin, cleaned the project, and ran it. Still having the same problem, and the image file is back in bin
Basically, Class.getResourceAsStream doesn't do what you think it does.
It tries to get a resource relative to that class's classloader - so unless you have a classloader with your filesystem root directory as its root, that won't find the file you're after.
It sounds like you should quite possibly really have something like:
BufferedImage = ImageIO.read(getClass().getResourceAsStream("/Images/player.gif"))
(EDIT: The original code shown was different, and had a full file system path.)
and you make sure that the images are copied into an appropriate place for the classloader of the current class to pick up the Images directory. When you package it into a jar file, you'd want the Images directory in there too.
EDIT: This bit may be the problem:
First of all, yes I did add the image folder to my classpath.
The images folder shouldn't be in the classpath - the parent of the Images folder should be, so that then when the classloader looks for an Images directory, it will find it under its root.
If you use resourceAsStream "/" referes to the root of the classpath entry, not to the root of the file system. looking at the path you are using this might be the reason.
If you load something from some home path you probably should use a FileInputStream. getResourceAsStream is for stuff that you deploy with your app.

Categories