I'm creating a game with LWJGL, and I want to display the LWJGL logo when the game starts.
For that, I need to load the image from the jar file.
You can see how I set up my resources here: http://imgur.com/HKHLDnM
I have been searching, and it appears that you have to use a '/' in front of the path.
That didn't work for me, also without the '/' in front.
I also tried putting 'jar:' before resources, but it doesn't work either.
Instead of using ResourceLoader.getResourceAsStream() use getClass.getResourceAsStream. This will start the path from where your main class is, so you may have to use .. (it goes to the file above). Sorry if this isn't clear or I misunderstood, but this is what I did to load images in a jar.
Related
I am making a Cluedo boardgame in java and I cannot get my images to show up when converting it to a .jar file.I am adding the images on top of one another so I used JLabels(new ImagIcon ("..")) like so.To add the images and now the images can't be seen when I create a .jar file. Just wondering if there is any way to do without editing my code a significant amount. I have read all stackoverflow answers on this issue and none of them solves this particular problem. Here is what the board game looks like when I run it in Eclipse.
When I convert it to .jar file it no longer displays the board or any of the players.
ImageIcon(String) expects that the String value is "file" reference to the image residing on disk.
If you have externalised your resources then you need to ensure that the path you are using is correct. Remember, the "working directory" in which the Jar is executed isn't always the same as the directory that the Jar is stored in.
To this end, it's generally recommend to "embedded" the images (and other resources) within the Jar itself. This way you can simply perform a lookup for the resource regardless of where it's installed.
The means by which you embedded resources is slightly different for each IDE, but it basically requires that the images are included in the Jar and a specific location/package.
An important note, when embedded in the Jar, the resources can no longer be referenced as if they "files", because they're not, they are part of the Jar/Zip archive and need to be referenced in a different manner.
To load embedded resources you need to use Class#getResource, which returns a URL or Class#getResourceAsStream which returns a InputStream.
In most case, the first is enough. While ImageIcon does take a URL, it is generally recommended to use ImageIO.read, have a read through Reading/Loading images for more details.
The advantage of this is two fold:
It blocks until the image is read, meaning the image is fully realised when the method returns
It throws an IOException when the image can't be read, which is way more meaningful, as ImageIcon fails silently
So, all that would accumulated down to something like...
new JLabel(new ImageIcon(ImageIO.read(instanceOfMyAwesomeGameObject.getResource("/path/to/resource/ResourceName.png"))));
I think it comes from the path to your images. The image path for sources is probably not the same as for .jar
Ive been learning to code games in java and libgdx. My most hated part of any language Ive attempted is the Level Editor (.TMX , tiled) parsing. The Github for libgdx says there is a helpful class made for this which Ive been trying to follow the instructions but Im stuck.
Basically if I comment out the following line of code, the app runs fine but of course without any level elements included. When I have this line in code it crashes my phone instantly whilst loading app:
TiledMap map
public Level(OrthographicCamera camera) {
map = new TmxMapLoader().load("level1.tmx"); /// THIS LINE CRASHES IT
}
I have checked the file 'level1.tmx' definitely exists in my 'assets' folder inside the Android folders of the game hierarchy.
If you do need the rest of my code Im happy to put it up but there is lots of garbage there not related to the problem. Thanks for any help
EDIT: One thing i think it may be is the type of file Im making in Tiled Editor. (ie. I chose zlib compression) other options were XML CSV and i think gzip
ANOTHER EDIT -- i managed to view an error message at the bottom of Android Studio saying unable to load file "....tiles-brown.png" -- I was unaware I had to load those textures, the two tiles png files used are also in the assets folder anyway already but it says Couldn't Load on the errors. PLEASE HELP
If your error is "unable to load file "....tiles-brown.png" then go to the folder in windows where you've stored the .tmx map. Right click and open in notepad. There you will see a reference path for each asset used on top of the file. Make sure the asset exists in the specified path in your project. If it doesn't or is different change it to where you store the assets for this map. Then save and refresh the android project. This should solve the problem.
You do not have to load the textures yourself. The loader will load all referenced textures for you, but you will have to put them in your assets folder at the same location which was used when creating the map with TilEd. Since you didn't do that right, you get the exception.
A good way to do this is creating a folder levels and a folder textures in your assets directory. Put your .tmx files in levels and the tilesets in textures. Now when you import a tileset from TilEd, it will be referenced as ../textures/tileset.png. To make sure this is correct, you can check the .tmx file. Do not use absolute file paths!
I am a beginner Java developer and I have created an application in Java (Netbeans).
I have used buttons icons, backgrouds for jframes etc. When I built the project, they can easily accessible using WinRAR. Anyone can extract my jar file and see all the images that I have used in my program. Even the images used in jpanel that is not accessible without password.
How can I hide or protect these images?
I have created a .exe file also using Launch4j but still facing same problem. When I right click on the .exe file, its easy to extract my whole program.
To be honest, if someone wants your picture, they are going to be able to get it. One can simply do a print screen. However you can still do somethings.
Remove the extensions for the picture files and give them crazy names - this will make it harder for people to find your pictures
Encript the files then have the application decript them when loading - I don't know how to do this but shouldn't be too hard to find, for instance you could save the pictures as a byte stream with some alterations then reload it.
Save the picture in some archive which only your application knows how to read.
But anyway even with all these things, I still know how one could get a handle to an open JFrame, look through the components, and then could get your picture. IMHO trying to prevent people for getting your pictures is not worth doing.
Hey So I'm pretty much awful at java and I want to add a ImageIcon now I've done this before and I even have the images to work the only issue is the program I am making is for University work and when I submit the work it will be submitted online through a .rar file.
So my issue is currently the image's have a huge direct path so i.e.
C:\Users\MY-NAME\Documents\NetBeansProjects\UNI-PROJECT\src\IMAGES\image.png
Since they will be reviewing my work on their work PC or home PC whatever the images wont work for them since it's using my home reference. How can I reference a image so they can open it where ever and the images will still work?
Thanks in advance
-SKENG-
The Java Tutorial on How to Use Icons has a small section explaining how to use Class#getResource() when the image is included with the application - look at the createImageIcon() method and the description that follows it.
You can put your images in the current folder or subfolders and then use the following path (for example):
System.getProperty("user.dir") + "images\pic1.gif"
Simply do not use absolute path, and put the images into a subdirectory where is located your sources or binaries.
I'm new to Java. I'm simply trying to build a .jar file of my applet so I can run it from my browser. This is what my directory structure looks like:
C:\java\pacman\src
contains all of the .java class files.
C:\java\pacman\assets
contains about 4-5 images and audio files.
If I try to use the following code:
Image someFile=getCodeBase().toString() + "file.png";
The result of getCodeBase() is
file:/C:/java/pacman/bin/
However the following code fails to load:
img=new ImgHelper(getCodeBase().toString() + "assets/");
ImageIO.read(new File(img.getPath("pacman.png")));
Moving my 'assets' folder to the 'bin' folder didn't fix this either. It tries loading:
file:/C:/java/pacman/bin/assets/pacman.png
saying:
Can't read input file!
But the url it gave opens fine if I paste it into run and hit enter:
So to avoid myself a lot of headache i commented out the code in my ImgHelper class and did this:
public ImgHelper(String dir)
{
//this.imgDir=dir;
imgDir="C:\\java\\pacman\\assets\\";
}
Which works perfectly. But I want to put this on a web server, and I have no idea how/what I should do to make all the images and sounds work. Any ideas?
Thanks...
Why not put it all in a JAR file and then call Class.getResourceAsStream?
A JAR file is better as it is a single HTTP connection rather than one HTTP connection per file. It is also much more flexible to use a Stream than a File.
getResourceAsStream will work when the files are not in a JAR as well, they need to be relative to the class file.
EDIT:
Another thing, the File method won't work if the applet is on a server as it will be trying to open the file from the local machine (I think, I haven't tried it) rather then from the server. Even if it tried to create a file path to the server that won't work.
I agree with tofubeer about the JAR, but if you want to put the image on your server, see the tutorial on Applet images here. The codebase will be whatever location your applet is on the server, and you can put images relative to that on the server as well. Use a media tracker along with the Applet.getImage() method to retrive the url. From the example:
my_gif = getImage(getDocumentBase(),"imageExample.gif");
There are two possible solutions that would work:
The images could be present outside the applet JAR. The applet could then be initialized with the location of the directory where the images are present. Once you have that information you could then load images from the server. The Sun Java tutorial provides an example usage of the applet parameter to pass the image source directory.
The applet class loader could be utilized to load the images from the applet's JAR, using the getResourceAsStream() method.
PS: It would be helpful if you referred to the section in the Java tutorials to load icons for your application. The same section discusses a lot of the points brought forth by TofuBeer and John.
EDIT : The usage of the File API is not recommended because it ends up reading off the local file system. That is unacceptable for most users on the internet.