General question - import image and write into jar file? [message #786354] |
Mon, 30 January 2012 10:10 |
Ben S. Messages: 6 Registered: January 2012 |
Junior Member |
|
|
Hi guys.
I spend almost an hour searching on google but I didnt find what I was searching for.
I've written a program which uses some images embedded in the frames.
These images have to be in a specific folder, otherwise the program is not able to read and show them.
If I want to send the program to friends, which may not know how to copy the files to the right path, the program is not working at all.
So here comes the point. Is it somehow possible to scan and write images into the bytecode / the jar file? (its a couple of small images, some kb) I mean, could I compile the image data so that the program got the necessary images within itself? Or am I raping the entire java programing concept with this question?
Thank you!
Edit: found solution (if somebody cares)
Quote:
private byte[] resourceToByteArray(String resName) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
InputStream iresStream = getClass().getResourceAsStream(resName);
int bytesRead = 0;
byte[] tmp = new byte[4096];
while ( (bytesRead = iresStream.read(tmp, 0, 4096)) > -1 )
byteStream.write(tmp, 0, bytesRead);
iresStream.close();
// JDocs: "Closing a ByteArrayOutputStream has no effect."
return byteStream.toByteArray();
}
...
here comes the real code
...
try {
icon = new ImageIcon(resourceToByteArray("u12.jpg"));
icon2 = new ImageIcon(resourceToByteArray("zw1217.jpg"));
icon3 = new ImageIcon(resourceToByteArray("17.jpg"));
icon4 = new ImageIcon(resourceToByteArray("ue18.jpg"));
}
catch (IOException ex) {
// Fehlerbehandlung nach Wunsch...
}
...
[Updated on: Mon, 30 January 2012 13:56] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.02621 seconds