0

I would like for the user to be able to choose from my pre-selected images within the program. I believe it would be similar to a text file within the program, however, as of right now it will only work with an image saved on to my computer. To specify, if I were to send my program to somebody, it would not function properly because they do not have the same photo I am using. I need the photos saved within the program. How would I go about dong this? Would jar files be the way to go?

Thanks!

3 Answers3

1

Yes, jar files could work great for this. But if you use jar files, just remember that the images are not present as Files but rather as resources since Java doesn't see files within a jar. So don't try to read the image into the program via new File(...) but rather SomeClass.getResourceAsStream(...)

i.e.

BufferedImage myImg = ImageIO.read(getClass()
           .getResourceAsStream("/images/myImage.jpg"));
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

Yes, a .jar is what you need. It contains all your files in a runnable form.

http://docs.oracle.com/javase/tutorial/deployment/jar/

Isaiah Taylor
  • 615
  • 1
  • 4
  • 17
1

You could either package the file into the program, which could be accomplished with a jar file as you mentioned. Jar files are archives that can hold classes, metadata, resources (such as images) and anything else you want.

An alternate way to go would be to upload the images to a site/server which you could then provide a URL to, such as your own personal site/server or an image hosting site. I'd go with a jar file though, as this approach would require an internet connection to function.

AlienHoboken
  • 2,750
  • 20
  • 23