Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  problem loading image from jar with an applet  (Read 1205 times)
0 Members and 1 Guest are viewing this topic.
Offline kemalenver

Senior Newbie




null pointer exception


« Posted 2003-03-12 20:27:31 »

Hi,
I've packaged my applet and all the iamges into a jar file for neatness.  However the applet seems to be trying to load the images from outside of the jar rather than inside. meaning that I need to send the images in a folder seperately.  Its messy!  What am I doing wrong?  Heres an example of where I am loading images in my code..
1  
2  
3  
fileName = getClass().getResource("resources/terrain/grass.gif");
GRASS_TILE = Toolkit.getDefaultToolkit().getImage(fileName);
mediaTracker.addImage(GRASS_TILE, 0);


I've tried using getCodeBase()+fileName and it doesnt make a difference!! How do I make it use the images in the jar!! lol.  Thanks for any help.
Offline Peppi

Junior Newbie




Java games rock!


« Reply #1 - Posted 2003-03-12 21:26:35 »

Here is what I do... and in no should you think that what I do correct  Wink

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
 // use getResource so it works in jar files        
       URL iconUrl[] = new URL[6];                    
        iconUrl[0] = Client.class.getResource("images/blue.gif");                
        iconUrl[1] = Client.class.getResource("images/red.gif");
        iconUrl[2] = Client.class.getResource("images/green.gif");      
        iconUrl[3] = Client.class.getResource("images/yellow.gif");  
        iconUrl[4] = Client.class.getResource("images/magenta.gif");
        iconUrl[5] = Client.class.getResource("images/cyan.gif");
       
        // import the images, initialize pics of players &
       // initialize the score of every player to 0            
       ImageIcon gif[] = new ImageIcon[6];    
            for (int i = 0; i < 6; i++) {          
              if (iconUrl[i] != null)
                    gif[i] = new ImageIcon(iconUrl[i]);                          
              pic[i] = new JLabel(gif[i]);                                                        
            scores[i] = new JTextField("  0  ",5);
            scores[i].setBackground(Color.black);
            scores[i].setBorder(BorderFactory.createEmptyBorder());
            scores[i].setEnabled(false);
        }


might be hard to understand sorry.

If that does not work go to
http://developer.java.sun.com/developer/qow/archive/76/index.html

good luck
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #2 - Posted 2003-03-12 23:48:59 »

1  
2  
3  
4  
5  
6  
7  
class ... extends Applet
[...]
URL keyboardImagePath;

keyboardImagePath=getClass().getResource("/gfx/keyboard.gif");
     
kb=getToolkit().getImage(keyboardImagePath);


that works for me.

btw u dont need an mediaTracker... the file is already loaded... if u try now to display it it will take awhile, cuz the image needs to be decrunched. and java waits with that task until it's needed...

u can simply ask for one of the image properties like this:

1  
kb.getWidth(null);


to start the decrunching procedure (as a side effect Wink)

after that u can use it w/o any delay Smiley

弾幕 ☆ @mahonnaiseblog
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.

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

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

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

Riven (26 views)
2014-07-23 20:56:16

ctomni231 (56 views)
2014-07-18 06:55:21

Zero Volt (48 views)
2014-07-17 23:47:54

danieldean (39 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
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

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!