• Advertisement
Sign in to follow this  

Pack images into .jar file?

This topic is 1853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

[CODE]
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!");
}
[/CODE]

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!

Share this post


Link to post
Share on other sites
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:
[CODE]
BufferedImage img_ball;
String Pathl1 = "Images/ball2.png";
InputStream streaml1 = MyClass.class.getResourceAsStream(Pathl1);
img_ball = ImageIO.read(streaml1);
[/CODE]

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:

[CODE]
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
[/CODE]

Oh yeah, change "MyClass" to whatever the name of your class is

Share this post


Link to post
Share on other sites
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!
[url="http://www.langeder.org/wordpress/jpong-version-1-1-released-multiplayer-launcher/"]http://www.langeder.org/wordpress/jpong-version-1-1-released-multiplayer-launcher/[/url]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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 [I]least[/I] useful.   Class and subclass structure should take into consideration such things, just so you know in future game structure. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement