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.
Related
I'm making a small program that will make the user chose between about a thousand pictures (thumbnails) and then display this picture in full size. I have the pictures saved on both my computer and on a CD, so how do I go about "importing" these to the program? Is the best way to put all the pictures in one huge picture and import it? And if I wish to make the program run on somebody else's computer, what do I do then?
If you are trying to use this application on another computer i would put the pictures on some sort of cloud service that can be reached anywhere then jar up the java application and point to the file path of those images. Hope this helped :)
To ship files with an application, one can put files in a package along with sources and read a file with ClassLoader.getResourceAsStream.
If you are using maven or gradle for build/project, put the package under src/main/resources and not in the src/main/java folder.
Note on Android this may not work, put images in assets folder and use Context.getAssets().open.
If you have a huge database, I would advise to a webdatabase software (SQL like). Here is a tutorial on how to use a SQL database with java; https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html
I hope this gives you ideas on how to solve this issue.
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.
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.
I have a very specific question.
I want to compile an ActionScript project within Java in-memory with the help of the Flex Compiler API ( http://livedocs.adobe.com/flex/3/compilerAPI_flex3.pdf ).
My last problem is to add images (jpg, png) that are embedded in the project to the compiler. I donĀ“t see a possibility to do this in-memory and my attempt to add them from disk was also unsuccessful.
Hope someone has a good hint for me.
Greets, Konsumierer
Invoking the flex compiler from java shouldn't be any different from invoking it from the eclipse plugin. I suppose you have the project located somewhere on the filesystem, so I suppose if the images are in the correct relative path, they should be embedded. How do you embed them, actually?
Another option is, in case when you use them in web-environment (and not AIR), to just write a relative server path to the image. For instance - the .swf is in /, you can poing the image to /images/myimage.jpg, and it will work.
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.