Best way to set up a program with a lot of pictures - java

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.

Related

Creating or reading files on any computer

my program on startup will:
1. search for a file
2. read the file
3. and set a string to the files contents
But the way ive done it it will only work if they have the exact path that i am hard coding in.
i want the path to adapt to other computers. I think i should use the Path class but ive just heard about that so not sure where to go.
basically i want it to search for a file on any users desktop, and if its not there make it.
if you need some code to clarify i can post it just let me know
I could think of two options.
You can simply specify a file name such as "myFile.txt", so the program will search this file in its program/project folder.
If it does not exist you can write the code to create it in the program folder, instead of hard coding any absolute path.
Else, you can try using the javax.swing.JFileChooser class to pop up an Open and Save dialog box.
This will give the end-user the freedom to select any file for reading and writing.
I found below two articles with some example on how to use the class. Please refer them for more information.
https://www.codejava.net/java-se/swing/show-save-file-dialog-using-jfilechooser
How to "Open" and "Save" using java
Thanks.
You can use the path "./yourfile.txt". It will search for "yourfile.txt" in the directory ".". That means the project's current directory. Maybe it can help you.

How to Hide / Protect Media Folder in Java Project?

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.

Unable to import image resources into our GUI

I'm working on a project with some friends over Github for a University project. I've only just taken my friends code off the repository for the first time. For some reason, all references to images in the code don't seem to allow compilation due to a directory problem I think. I'm using Eclipse, my friend's using Netbeans(don't know if that affects it or not?). Anyway, all of the images referenced are either in a folder known as runone, on the same level as the Eclipse src, or within the package 'runone' within src. I don't know which.
Here's an example of some of the references:
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/OSTeeeennny.jpg")))
jLabel53.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/clown_fishTIny.jpg")));
I guess what I'm wanting to know is, how can I make these resources work correctly, on any machine that we code this program on?
Hope that all made sense!
For the examples you have given your file structure might look like this
src/com/yourpackage/YourClass.java
src/runone/OSTeeeennny.jpg
src/runone/clown_fishTIny.jpg
For a more best practice way of organizing your resources you could do this
src/com/yourpackage/YourClass.java
src/com/yourpackage/resources/OSTeeeennny.jpg
src/com/yourpackage/resources/clown_fishTIny.jpg
and then use the following calls to load them
getClass().getResource("resources/OSTeeeennny.jpg")
getClass().getResource("resources/clown_fishTIny.jpg")

Adding a path for a ImageIcon - Java

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.

How to store high score inside a jar file

I am developing a small game in Java and I am shipping it as a single Jar file. I want to store the high scores/best times for that game somewhere. Instead of storing it in a separate file, I would like to store it in the application itself (inside the Jar) so that its not lost. Is this possible at all ? If so, how to do it programatically.
Java does not give you tools to modify the JARs which are currently run. If you really want to do it, you have to guess the location of the JAR by yourself (which might reside on a read-only filesystem) and modify it the same way you would modify any archive file.
Bottom line: it's a very bad idea, don't do it! See this question for a much more reasonable solution.
Nothing is impossible, but storing it in the jar file would make it very complicated. You might also end up with unwanted side effects like "Permission Denied" errors when the jar is owned by another user. Virus scanners might get nervous when they see jar files change without reason, etc....
I would look to the Preferences API for storing this kind of info.
I think it is a bad idea to try and store anything in the jar file. Another option is to have a web based service offered to the people playing with your game. The game could connect through a web service to your hosted server and then store everything centrally there. Not sure if it is exactly what you want but it's just an idea. It would also allow people to compete with each other.
Java JAR file is a ZIP-Archive, so you could possibly access it with standard ZIP-Tools and just extract one hisghscores.txt file, modify it and then pack it back again.

Categories

Resources