Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Exporting Jar in eclipse with images  (Read 8566 times)
0 Members and 1 Guest are viewing this topic.
Offline Sinuath

Junior Member


Medals: 2



« Posted 2011-06-21 21:59:10 »

Hello! i'm still learning the ways of Java and eclipse, i've started creating a space invaders clone(gotta start somewhere right?) But the game isn't really where my problem is. I've spent the last few days trying to export to a jar file. And i finally managed it but then i hit a wall with my code within the jar trying to find the pictures.

Which is really where i'm still kind of confused. I found a site http://ubuntuforums.org/showthread.php?t=711238 that helped me out and now the code works but i don't know why it works and that's bothering me more then anything.

i've imported the images into eclipse to help automate the export into a jar file. I was able to manually copy the pictures into the jar and it worked before but i'd rather eclipse just handled it for me.

Anyway in the jar the files are under a folder called /Images/ and here's the code i was using:

1  
2  
3  
4  
5  
6  
   private Image image;

   private String craft = "./Images/cannon.png";

   ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
   image = ii.getImage();


courtesy of: http://zetcode.com/tutorials/javagamestutorial/

Now i'm using

1  
2  
   java.net.URL imgURL = getClass().getResource("/Images/cannon.png");
   image = Toolkit.getDefaultToolkit().getImage(imgURL);


Now the code automagically works but i'm not sure why. I was wondering if someone could explain the differences between these two? Do i have to use the second one? is it more efficient? the path to the images looks like it was exactly the same so i'm not sure why the other one didn't work. I noticed i needed to import java.awt.Toolkit for this one for obvious reasons. But i'm not sure what's up with java.net.URL

Anyway i'd apreciate a point in the right direction. There's so much code out there and so many ways to do things that i'm not all that sure whether i'm heading in the right direction or not. This could be a horribly inefficient way of doing it for all i know.

Hey [you][/you], you should totally check out my boring Site ~ http://davediel.com/chris
Offline gouessej
« Reply #1 - Posted 2011-06-21 22:46:03 »

Hi!

The difference is the absence of the dot at the beginning of the path in the working example. The image is fetched from the JAR or a directory. If you put the dot, it looks in the "current" directory on the file system anyway whereas in the second case, in Java Web Start, it will look at the image in the JARs.

Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-06-21 23:25:27 »

The dot signifies a relative URL so it will look for an folder called Images at the same level as whatever class you called "getClass()" from.
Using a forward slash signifies the root directory so it will look for an Image folder at the current working directory which is either the folder above the first package folder or the root inside of a jar file.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online philfrei
« Reply #3 - Posted 2011-06-22 01:19:09 »

With the constructor new ImageIcon(string), the string is taken to be a file name. This is problematic when reading data from within a jar, as a file embedded within a jar does not have a "normal" file location for most operating systems.

I believe you could continue to use the ImageIcon constructor if the parameter you passed was a URL instead of a file name string. URL's are able to "look inside" of a jar. Don't ask me how! There's more about URL's, including the ability to use relative addressing here: http://download.oracle.com/javase/1.5.0/docs/api/java/net/URL.html

So, I'm guessing the following code should also work for you (relieving you of the task of importing/loading the Toolkit):
1  
2  
3  
    URL imgURL = getClass().getResource("/Images/cannon.png");
    ImageIcon ii = new ImageIcon(imgURL);
    image = ii.getImage();

This is similar with what I ended up using in my game after a brief flirtation with the defaultToolkit method. I can't say that I know much about the Toolkit. It seems like a pretty cool thing, but I haven't used it.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-06-22 02:26:49 »

With the constructor new ImageIcon(string), the string is taken to be a file name. This is problematic when reading data from within a jar, as a file embedded within a jar does not have a "normal" file location for most operating systems.

I believe you could continue to use the ImageIcon constructor if the parameter you passed was a URL instead of a file name string. URL's are able to "look inside" of a jar. Don't ask me how! There's more about URL's, including the ability to use relative addressing here: http://download.oracle.com/javase/1.5.0/docs/api/java/net/URL.html

So, I'm guessing the following code should also work for you (relieving you of the task of importing/loading the Toolkit):
1  
2  
3  
    URL imgURL = getClass().getResource("/Images/cannon.png");
    ImageIcon ii = new ImageIcon(imgURL);
    image = ii.getImage();

This is similar with what I ended up using in my game after a brief flirtation with the defaultToolkit method. I can't say that I know much about the Toolkit. It seems like a pretty cool thing, but I haven't used it.
1. Using ImageIcon and getting the image there is ugly and not efficient. The way the OP should be getting images is via javax.imageio.ImageIO.
1  
2  
URL imgURL = getClass().getResource("/path/to/image.png");
BufferedImage image = ImageIO.read(imgURL);


2. A URL points to a file inside a jar by using the special "!" character. For example lets say we have the jar file MyJar.jar and inside the jar we need "/myfolder/MyImage.png", the URL would look like this:
1  
file:/C:/Path/To/Jar/File/MyJar.jar!/myfolder/MyImage.png


3. There is not point in using Toolkit for anything graphics-wise. Creating images and retrieving resources are done using specific tools made for the job.

Offline zoto

Senior Member


Medals: 4



« Reply #5 - Posted 2011-06-22 02:57:21 »

I use code like this to load images.
1  
2  
3  
4  
5  
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

String fileName = "Images/cannon.png";
BufferedImage image = ImageIO.read( Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName) );
Online philfrei
« Reply #6 - Posted 2011-06-22 03:16:50 »

Just spotted this great article: "Loading images using getResource" embedded in "How to Use Icons".
http://download.oracle.com/javase/tutorial/uiswing/components/icon.html

There doesn't seem to be anything about needing to put in a "!" when a URL references a resource in a jar. I guess this is handled automatically when using the code snip below. The "seven configurations" referred to include the case where images are included within the same jar.

Quote
All seven configurations shown are valid, and the same code reads the image:
1  
2  
3  
4  
5  
java.net.URL imageURL = myDemo.class.getResource("images/myImage.gif");
...
if (imageURL != null) {
    ImageIcon icon = new ImageIcon(imageURL);
}

I agree and like and used the same construct as the one you gave, ra4king, (e.g., "myBufImg = ImageIO.read(url);"). But I'm not dealing with IconImage objects in my program. Seems like one should prefer to load directly into the form one wants to use in the program, in general. In my case I was actually needing to get at the bits.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-06-22 03:21:59 »

philfrei, have you tried printing out what you get from getResource? It automatically puts the "!" for you if it finds the file inside a jar file.

Online philfrei
« Reply #8 - Posted 2011-06-22 04:19:46 »

@ra4king Yes, I've seen it. I can't recall if it was when I did a getFile() method on a URL or maybe its toString(). But, happily, it is not needed in the coding, when using getResource() as presented in the "Using Icons" tutorial I just cited. As I said, the use of the "!" must be handled behind the scenes, automatically, somehow or another.

Nothing about it in the java spec for URL either. Maybe it is covered somewhere else, perhaps in some documentation specifically about jars. I don't understand why the URL section doesn't say anything. It spends some time on things like the syntax needed for accessing ports, and about various parts of a URL. Mystery to me. Could be something client/OS specific. I don't know. Huh

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Sinuath

Junior Member


Medals: 2



« Reply #9 - Posted 2011-06-22 07:12:28 »

appreciate the feedback guys!

i thought the '.' was for referring to the directory i was in, not the directory prior to the one i was in. *shrug

So if i understand this right, the URL is important for referring to the images within the jar file?

Hey [you][/you], you should totally check out my boring Site ~ http://davediel.com/chris
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2011-06-22 07:58:00 »

"." means the current directory. ".." means the directory above the current one.
And a URL is the only way to point to a location inside a Jar file.

Offline Sinuath

Junior Member


Medals: 2



« Reply #11 - Posted 2011-06-22 16:50:14 »

I ended up switching the images back to the imageicon deally. I don't know if that's really the best way to do it but the collision detection wasn't working with the url.. and i am still learning so i didn't want to make this more complicated then it had to be in the beginning.

Oh i think i just get what was up with the dot. it wasn't looking inside the jar it was looking in the operating system right? Ok i get that.

oh and if anyone's interested here's the beginning of my space invader wannabe game http://davediel.com/chris/projects/Java/Cattack.jar It's not impressive or even at demo status yet but you can shoot things and they will disappear Smiley so i'm happy about it.


Hey [you][/you], you should totally check out my boring Site ~ http://davediel.com/chris
Offline zoto

Senior Member


Medals: 4



« Reply #12 - Posted 2011-06-23 08:58:37 »

The code I posted above will load images from any jars in the classpath/archive tag. It also doesn't create ToolkitImages or trip the SecurityManager.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!