Hello Guest

Loading an image file from jar

  • 6 Replies
  • 9861 Views
Loading an image file from jar
« on: January 11, 2004, 20:45:26 »
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 ;)

*

Offline princec

  • *****
  • 1898
    • Puppygames
Loading an image file from jar
« Reply #1 on: January 12, 2004, 09:14:14 »
You should be using the following to access it:
Code: [Select]

InputStream is = MyGame.class.getResourceAsStream("/myimage.png");


Cas :)

Loading an image file from jar
« Reply #2 on: January 12, 2004, 22:03:33 »
And uh how do I type cast the value returned into an image?
Do I have to use a media tracker?

*

Offline princec

  • *****
  • 1898
    • Puppygames
Loading an image file from jar
« Reply #3 on: January 12, 2004, 22:05:17 »
No, you just pass that input stream to imageIO:
Code: [Select]

BufferedImage img = ImageIO.read(is);

Job done.

Cas :)

Loading an image file from jar
« Reply #4 on: January 12, 2004, 23:00:37 »
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)

Code: [Select]

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 [-(

*

Offline princec

  • *****
  • 1898
    • Puppygames
Loading an image file from jar
« Reply #5 on: January 12, 2004, 23:41:55 »
Yes, you forgot the preceding "/" in the pathname.
(Read up on ClassLoader.getResource())

Cas :)

Loading an image file from jar
« Reply #6 on: January 13, 2004, 03:27:50 »
Thanks for the tip!

Worked like a charm! :)