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.
Related
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"));
im fairly new to java and im making a pokemon style game for practice and i would like to be able to send the game to my friends.
here is the main problem: the game works fine in my netbeans IDE, but using the jar file in my dist folder does not work and throws a nullPointerException. i have narrowed down the problem. my game uses imageIcons and png/gif images that i have imported in my libraries. im getting access to them like this
Icon bckground = new javax.swing.ImageIcon(getClass().getResource("/pictures/BG.gif"));
i am unsure how to get the images into the lib folder for the program to find. i have tried copying the files straight into the lib folder and creating a folder for them called pictures; neither worked. right now the lib folder contains only a single jar from one of my other libraries. (that is the only jar file that i am importing to my libraries)
Pic of what it looks like in IDE
In my case, i did something like this.
final BufferedImage image = ImageIO.read(new File("something.jpg"));
I just transferred something.jpg into the dist folder, and it worked fine.
After clean-building your project, put the BG.gif into the dist folder, then run your jar file in dist folder. Now everything should be fine. When sending your game to your friends, you can encapsulte (hide, set read-only) your code (google encapsultaion java), then with the BG.gif being transferred into dist folder, archive the project. Then your friends only need to unzip it and find jar file in dist folder.
Hope this will help:)
Please put your picture (BG.gif) in the package (directory) where it is used as icon Icon bckground = new javax.swing.ImageIcon(getClass().getResource("BG.gif")); inside the jar file. You need to change the path of the file first getResource("BG.gif") and then create the jar file from IDE. If the jar does not contain the image, you can open the jar using using any unzip application (winrar etc.) and copy and paste the the image file in the directory where the class is present. Please let me know the outcome.
I need set of images in jpg format to be in this specific folder.
path = getFilesDir() + "/imagefiles/";
After root my mobile phone, I manage to find the specific folder. The location is /data/data/*packagename/files/imagesfile/*png images.
I can copy the image manually into the folder but i want to deploy a project that already has the image in it.
I have tried several solution but none really works for me. Please help me.
I can copy the image manually into the folder but i want to deploy a project that already has the image in it
It is not possible for you to create an app that automatically has files in getFilesDir() immediately upon installation.
What is possible is for you to put images there on the first run of your app. Those images could be:
downloaded from the Internet
packaged in assets/ in the project, then copied into your desired location using AssetManager and open()
packaged as raw resources in the project, then copied into your desired location using Resources and openRawResource()
etc.
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?
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.)