2

Normally we write code to read a data file such as an image. In my case I want to produce a single *.class file which contains the information of some images, so that when I execute the java file it will display the images without dependency on any other files. Appreciate if anyone can help.

yu quan
  • 161
  • 1
  • 1
  • 14
  • 1
    If you are happy to have it in the same `.jar` file put it as a resource. – Henry Mar 08 '17 at 08:56
  • @Henry it wants it to be embedded in the produced .class file. – LppEdd Mar 08 '17 at 09:00
  • @LppEdd I know. But this is not be the "normal way" of doing it. Also almost all non-trivial Java programs have more than one `.class` file anyhow. – Henry Mar 08 '17 at 09:03
  • @Henry I know I know, no sane developer would use resources this way. I think it is more of a challenge. – LppEdd Mar 08 '17 at 09:05
  • In case this can be an option, you may have your class draw the image by itself using `Graphics` objects and methods like`drawLine`, etc.. . I remember having done that once, in an application that created its icons by itself . – Arnaud Mar 08 '17 at 09:09

2 Answers2

1

You can store the images as base 64 so they will be part of the file, see: Java - Convert image to Base64 for more details.

this way the image will be constant in the class and therfore will be compiled inside it

Community
  • 1
  • 1
KitBag
  • 23
  • 5
0

Never done this, but an image is a series of bytes. You could store the bytes in a byte array and use it to "recreate" it.

See Convert and display image from byte array

Community
  • 1
  • 1
LppEdd
  • 20,274
  • 11
  • 84
  • 139