Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Re: Accessing images from a JAR  (Read 1662 times)
0 Members and 1 Guest are viewing this topic.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Posted 2003-02-16 12:24:49 »

try to replace

1  
tempIcon = new ImageIcon(getClass().getResource("tiles/cloud.gif")).getImage();


with

1  
tempIcon = new ImageIcon(getClass().getResource("/tiles/cloud.gif")).getImage();


This works for me, although my images are in a separate jar (but are in a directory).

Offline JuddMan

Senior Devvie


Medals: 1


Your Ad Here


« Reply #1 - Posted 2003-02-16 12:27:56 »

oh. thanks for replying so soon..... what, within 20 minutes :)

anyway, i deleted the post before i saw your reply.

here's the original post... and i thought i had fixed it but it didn't work. your suggestion didnt work either unfortunately.


Quote
hi. i am trying to work out how to access many gif files after they are put into the jar... the gifs are in their own 'directory' in the jar file...
eg. using
tempIcon = new ImageIcon(getClass().getResource("cloud.gif"))
allows me to access the cloud file from the root of the jar file, but when i try putting the cloud.gif image into a folder in the jar and use
tempIcon = new ImageIcon(getClass().getResource("tiles/qblock.gif"))
it throws

Exception in thread "main" java.lang.NullPointerException
       at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
       at TileBank.addTile(TileBank.java:92)
       at TileBank.loadTiles(TileBank.java:40)
       at TileBank.<init>(TileBank.java:26)


this is after compiling the jar with
jar cvfm BufferStrat.jar MANIFEST.MF *.class *.gif *.java tiles

extracting the jar with WinRar i can run the class file just fine and it loads the images.

code is here... but you have to extract it to run it...
http://members.austarmetro.com.au/~juddman/java/BufferStratBroken.jar

if anyone knows another way to access image files in the jar the program is running from, plz help out. i dont really want to have to dump all the gifs in amongst the class files.

Relevant
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public void addTile(String filename, String description,
      boolean pl, boolean pr, boolean pu, boolean pd, boolean intr, int index)
      {
            //tempIcon = new ImageIcon(getClass().getResource(filename)).getImage();
            tempIcon = new ImageIcon(getClass().getResource("tiles/cloud.gif")).getImage();
            tempImage = gc.createCompatibleImage(32,32,Transparency.BITMASK);
            Graphics2D imgGfx = (Graphics2D)tempImage.getGraphics();
            imgGfx.setComposite(AlphaComposite.Src);
            imgGfx.drawImage(tempIcon,0,0,null);
            imgGfx.dispose();
            tempTile = new Tile(tempImage, description, pl, pr, pu, pd, intr, index);
            tiles[index]=tempTile;
      }
Offline zparticle

Senior Devvie




Thick As A Brick


« Reply #2 - Posted 2003-02-16 12:42:14 »

This works for me:

1  
2  
3  
public static final String titleURL = "/images/title.png";
URL url = Main.class.getResource(titleURL);
Image titleImage = new ImageIcon(url).getImage();


I just happen to be using my Main class as the reference but it could be any class. The images directory is at the root of the jar file.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JuddMan

Senior Devvie


Medals: 1


Your Ad Here


« Reply #3 - Posted 2003-02-16 13:18:02 »

this is a really strange thing...


           System.out.println("1");
           String titleURL = "/tiles/cloud.gif";
           System.out.println("2");
           URL url = TileBank.class.getResource(titleURL);
           System.out.println("3");
           Image tempIcon = new ImageIcon(url).getImage();
           System.out.println("4");

leads to


Reading tile meta data...
1
2
3
Exception in thread "main" java.lang.NullPointerException
       at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
       at TileBank.addTile(TileBank.java:99)
       at TileBank.loadTiles(TileBank.java:41)
       at TileBank.<init>(TileBank.java:27)
       at BufferStrat.<init>(BufferStrat.java:62)
       at BufferStrat.main(BufferStrat.java:248)

same as before...
Offline JuddMan

Senior Devvie


Medals: 1


Your Ad Here


« Reply #4 - Posted 2003-02-16 14:14:44 »

thanks zparticle. your way did work

i had another problem as well with trying to read the "meta.dat" text file from the same location. not quite as easy as reading an image file.

after ages of API surfing and looking at little demos and stuff i worked that out too.

http://www.tair.freeservers.com/Java/readtext.htm

it's meant for reading an online file... seems though that the jar files are very network orientated.

edit:

Windows and DOS are sooo lenient on file names. they're not case sensitive and you can have spaces following them. i discovered somehow that all the file names but one had spaces after them in the dat file so it didnt work either way.

good thing MS introduced the intercnangable \ and / things or there'd be yet another annoying compatibility issue to overcome. some of my GIF's had upper case first letters.

it finally works independently from a jar file.
the result is an extendable cache system which dictates what tiles are available and what they do. that'd let me change it very quickly later on, or add more tiles.

a little more interesting than before, there're 42 tiles, adding tiles will throw 20 random ones in there. only very slightly lower performance than the BufferStrat demo.

http://members.austarmetro.com.au/~juddman/java/MultiGFXBuffer.jar
Offline zparticle

Senior Devvie




Thick As A Brick


« Reply #5 - Posted 2003-02-16 16:18:08 »

That's funny, this is the first thing I did with your original code. Made it load 50 different images, thought it might make a difference but it still worked fine.

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.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (30 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!