Loading an image file from jar

Started by Morgrog, January 11, 2004, 20:45:26

Previous topic - Next topic

Morgrog

I have a Webstart app that loads a texture (glass.png)

Everything works fine except the image doesn't show (no texture is loaded).  What I did is create a jar file with the image in it, it's signed and accessible via my webstart (the jar) but it still doesn't load :'(

If I manually copy the image (glass.png) at the same place my jnlp resides, it loads the texture correctly and everything is fine.

Anyone knows how I can access that png file when its inside a jar?

I have no idea if that made sense, hopefully someone will understand enough to help me out ;)

princec

You should be using the following to access it:
InputStream is = MyGame.class.getResourceAsStream("/myimage.png");


Cas :)

Morgrog

And uh how do I type cast the value returned into an image?
Do I have to use a media tracker?

princec

No, you just pass that input stream to imageIO:
BufferedImage img = ImageIO.read(is);

Job done.

Cas :)

Morgrog

gah, I get a null InputStream

Here's my code (patched up and prolly real ugly) can anyone point me to my mistake? (the path I get in argument is hard-coded, so no problem there)

private final int loadTexture(String path) throws Exception 
	{
		BufferedImage tex = null;
		InputStream is = null;

		try { 
			is = Nehe11.class.getResourceAsStream(path);
			System.out.println("is=" + is + "\n\n");
	  		tex = (BufferedImage) ImageIO.read(is); 
			if (tex == null) 
			{ 
				throw new Exception("Error: Got null from ImageIO.read()"); 
			} 
		} 
		catch ( Exception e ) { 
			throw new Exception("Problem loading bitmap", e); 
		} 

		// Create A IntBuffer For Image Address In Memory   
  		IntBuffer buf =
			ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();

		// Extract The Image 
		tex = ImageIO.read ( is );

		// Flip Image
	  	AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
		//	  tx.translate(0, -image.getHeight(null));
	  	AffineTransformOp op =
			new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
		tex = op.filter(tex, null);

		// Put Image In Memory
		ByteBuffer scratch =
			ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());

	  	byte data[] =
			(byte[]) tex.getRaster().getDataElements(
		  	0,
		  	0,
		  	tex.getWidth(),
		  	tex.getHeight(),
		  	null);
	  	scratch.clear();
	  	scratch.put(data);
	  	scratch.rewind();

	  	GL.glGenTextures(buf); // Create Texture In OpenGL   

		GL.glBindTexture(GL.GL_TEXTURE_2D, buf.get(0));
	  	// Typical Texture Generation Using Data From The Image

	  	// Linear Filtering
	  	GL.glTexParameteri(
			GL.GL_TEXTURE_2D,
			GL.GL_TEXTURE_MIN_FILTER,
			GL.GL_LINEAR);
	  	// Linear Filtering
	  	GL.glTexParameteri(
			GL.GL_TEXTURE_2D,
			GL.GL_TEXTURE_MAG_FILTER,
			GL.GL_LINEAR);

	  	// Generate The Texture
	  	GL.glTexImage2D(
			GL.GL_TEXTURE_2D,
			0,
			GL.GL_RGB,
			tex.getWidth(),
			tex.getHeight(),
			0,
			GL.GL_RGB,
			GL.GL_UNSIGNED_BYTE,
			scratch);

	  	return buf.get(0); // Return Image Address In Memory
	}



Do you guys see anything wrong (with a quick glance?) apart from the newbiness of the code [-(

princec

Yes, you forgot the preceding "/" in the pathname.
(Read up on ClassLoader.getResource())

Cas :)

Morgrog

Thanks for the tip!

Worked like a charm! :)