Pack images into .jar file?

Started by
5 comments, last by 3Ddreamer 11 years, 3 months ago
Hello everyone!

I just started learning Java and i use eclipse.
At the moment i work on a Pong game and i want to include some pictures
i.e. a ball instead of a circle, to make it look better.

This is the Code i use to load the image:


try {
img_ball = ImageIO.read(new File("/home/michael/Desktop/workspace-java/JPong/src/ball2.png"));
} catch (IOException e) {
System.out.println("ball2.png not found!");
}


As you can see i use absolute paths, because relative would not work at all.
But using absolute paths is not what i want since i'm not the only to playing it.

Where do i have to place those files to be accessible relatively?
I would also like to put it INTO the jar file, so the release is just one file to run without the folders with files

Thanks in advance!
Advertisement
I encountered a similar issue recently, so I feel your pain and frustration!
Here's the code I use to load images, storing each image in a folder called "Image" inside of src:

BufferedImage img_ball;
String Pathl1 = "Images/ball2.png";
InputStream streaml1 = MyClass.class.getResourceAsStream(Pathl1);
img_ball = ImageIO.read(streaml1);


When you export as compile you .jar all the images in your Image folder will be packaged inside.
Your going to make the following imports:


import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;


Oh yeah, change "MyClass" to whatever the name of your class is
Stay gold, Pony Boy.
Thanks for the answer!
I hope this will solve my problem! :)
Thank you so much!
It works, but i had to make a folder inside the Class, not inside src

:)

Here is a dl, i would appreciate some feedback!
http://www.langeder.org/wordpress/jpong-version-1-1-released-multiplayer-launcher/
Looks good! Maybe you could make an AI single player version, because it doesn't look like you have a very large server community! Very cool!
Stay gold, Pony Boy.
Looks good! Maybe you could make an AI single player version, because it doesn't look like you have a very large server community! Very cool!

Thanks!
I guess i will do that, since jPong isn't a game one would play on a LAN-Party.

I thought about creating a game-lobby,
where one can download the games and directly connect to other players,
but that will take a while.

Hi,

It is not always possible or practical to load all the images into a .jar file. When a file is part of a model or the file is to be changed dynamically, then your solution could be least useful. Class and subclass structure should take into consideration such things, just so you know in future game structure.

Personal life and your private thoughts always effect your career. Research is the intellectual backbone of game development and the first order. Version Control is crucial for full management of applications and software. The better the workflow pipeline, then the greater the potential output for a quality game. Completing projects is the last but finest order.

by Clinton, 3Ddreamer

This topic is closed to new replies.

Advertisement