Accessing Images in Eclipse - java

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?

Related

Unable to use images in Intellij IDEA [duplicate]

I have a Java Project in NetBeans 7.0.
I want to add some image to some label dynamically. The image will differ depending on the state of the program.
I put one such image, 'filling.jpg', in the 'resources' folder of my project.
I want to reach this file correctly (not by absolute or relative path, because that will cause problems when I build the jar file).
So I found this method:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);
It keeps give me java.lang.NullPointerException.
But I am sure that there is that image, because I can assign the image to the label from the NetBeans Properties menu for that label (but I don't want this, I want to add the image by Java code).
What am I doing wrong, and how can I get that image correctly?
This was a pain, using netBeans IDE 7.2.
You need to remember that Netbeans cleans up the Build folder whenever you rebuild, so
Add a resource folder to the src folder:
(project)
src
project package folder (contains .java files)
resources (whatever name you want)
images (optional subfolders)
After the clean/build this structure is propogated into the Build folder:
(project)
build
classes
project package folder (contains generated .class files)
resources (your resources)
images (your optional subfolders)
To access the resources:
dlabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resources/images/logo.png")));
and:
if (common.readFile(getClass().getResourceAsStream("/resources/allwise.ini"), buf).equals("OK")) {
worked for me. Note that in one case there is a leading "/" and in the other there isn't.
So the root of the path to the resources is the "classes" folder within the build folder.
Double click on the executable jar file in the dist folder. The path to the resources still works.
I have a slightly different approach that might be useful/more beneficial to some.
Under your main project folder, create a resource folder. Your folder structure should look something like this.
Project Folder
build
dist
lib
nbproject
resources
src
Go to the properties of your project. You can do this by right clicking on your project in the Projects tab window and selecting Properties in the drop down menu.
Under categories on the left side, select Sources.
In Source Package Folders on the right side, add your resource folder using the Add Folder button. Once you click OK, you should see a Resources folder under your project.
You should now be able to pull resources using this line or similar approach:
MyClass.class.getResource("/main.jpg");
If you were to create a package called Images under the resources folder, you can retrieve the resource like this:
MyClass.class.getResource("/Images/main.jpg");
Thanks, Valter Henrique, with your tip i managed to realise, that i simply entered incorrect path to this image.
In one of my tries i use
String pathToImageSortBy = "resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
But correct way was use name of my project in path to resource
String pathToImageSortBy = "nameOfProject/resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
For me it worked like I had images in icons folder under src and I wrote below code.
new ImageIcon(getClass().getResource("/icons/rsz_measurment_01.png"));

Navigating a folder up?

I am wondering how I can navigate a folder up like shown here: https://github.com/libgdx/libgdx/wiki/Texture-packer#automatic-packing
I did exactly the same ../ but libgdx is registering .. as a folder and does not go up in the tree. However the docs are kinda old and might be outdated.
TexturePacker.Settings settings = new TexturePacker.Settings();
settings.maxWidth = 1024;
settings.maxHeight = 1024;
TexturePacker.process(settings, "../images",
"../android/assets/tilesets/",
"set01");
//Input file does not exist: C:\Programming\Java\LibGDX\Projects\CloneQuest\android\assets\..\images
The reason why I want this is because I want to have a image folder with all separate images outside the assets folder. Whenever the desktop application is run in it's current state it will pack the images into the assets folder. But these separate images won't be transferred to the jar or apk when making a build.
This is working, the output just prints it like this. Are you sure there is a folder images as a sibling of assets, as you can see you are in the assets folder
C:\Programming\Java\LibGDX\Projects\CloneQuest\android\assets..
Please provide your folder structure if you struggle.

Loading image from a Java file

I have this project structure:
ProjectName
src
xxx
co
com
package
something.java
web
image
print
pic.jpg
I want to load the pic.jpg in the java file which will then be used in the pdf file generated. I have gone through the answers here but, nothing helped me yet. Possibly I am missing a small thing.
If the pic.jpg was under package, then getClass().getResource("pic.jpg") works absolutely fine. getClass().getResource("/web/image/print/pic.jpg") doesnt work as well. But I want to place all my images under image folder and refer it in the java file.
You should get the application runtime path and rebase the path to the folder which images locate.
You can use the code:
String path = new File(".").getCanonicalPath();// or System.getProperty("user.dir")
Seems your files are not copied to bin/ directory. Change your build script to copy them to output directory
getClass().getResource() will use class loader to load class, since those files need to be in classpath

Java won't export my resources properly

So I have 2 class folders one is res the other is lib. My res folder has two other sub folders one with images the other with sounds. My lib folder has 4 other jar files and a native folder. It all works within eclipse but when I try to export it as a runnable jar it does not work. I won't won't recognize anything.
I am to call my images I am using ImageIO.read(new File(imagePath)); For the sound I am using the external libraries I mentioned earlier to load and play them.
I am to call my images I am using ImageIO.read(new File(imagePath))
Contrary to your title, this is not an Eclipse problem - it's simply a bug in your code, because your code assumes that the image is stored as a file in the file system, when it's not.
You don't have a file for the image, so you shouldn't use new File. You should instead use Class.getResource or ClassLoader.getResource - or the getResourceAsStream equivalents. That way, it will load the resource from whatever context the class itself is loaded, which is appropriate for jar files. So for example, you might have:
Image image = ImageIO.read(MyClass.getResource("foo.png"));
... where foo.png is effectively in the same package structure as the class. Alternatively:
Image image = ImageIO.read(MyClass.getResource("/images/foo/bar.png"));
where images is a folder within the root directory of one of your jar files loaded by the same ClassLoader. (We don't have enough information to give you complete code here, but that should be enough to get you going.)

Find a resource both in .jar and eclipse with the same String computation

I want to get the path to a resource for ImageIO to read out a BufferedImage from some .png s.
While developing the project I use a relative path to "/bin/stuff/icons/image.png" , but this will definetly not work when I put everything together into a .jar file, so I need a way to get the path to these resources both while testing in eclipse and when later running it within a .jar .
After a lot of trying out both finding the file and getting the input stream to the file I came to the conclusion that this approach works every time:
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(path)
BufferedImage image = ImageIO.read(in)
Where path is
"projectName/resourceFolder/" + nameOfResource.stuff
as found in the src directory of the eclipse project.
E.g.
"myProject/images/icon.png"
When getting only the resource and then getting the path of the resource to link to a file, you will get FileNotFoundExceptions when using a .jar (but not while testing with eclipse, so one should be warned to think that his code works).
And - no - I don't save images in the bin/ - but they are copied to this directory and thus I find them there while testing. Now everything seems to be working.
Don't put anything under the bin directory in Eclipse: if you run a clean on the project it will be erased.
What you can do is to define a new source folder like resources, and put the image there. This way it will be automatically copied to the bin folder.
If you include the resources folder into the Jar, it will be available in both environments by using something like:
ImageIO.read( getClass().getResource("/image.png") )
PS: You can evade using a different resources folder but mixing the sources and images will quickly pollute your source folder.

Categories