Issue displaying PNG file

I seem to have an issue displaying "some" PNG files. I am using the source code from Brakeen's tile game in his book. I have converted from using ImageIcon to ImageIO as my way of reading in images. Specifically, I have used the code snippet from Abuse about loading images so that they are accelerated. I can get the game to run fine.

I have started porting the code so that I can write my own game. I took a bmp file that I wanted to use as a spalsh screen and converted it to a PNG file using MS Paint. When I try to display that as my splash page, ImageIO.read throws an IllegalArgumentException. Here are the main code snippets that get an image:

public URL getResource( String filename ) {

return getClass().getClassLoader().getResource( filename );

}

/**

* Gets an image from the images/ directory.

*/

public BufferedImage loadImage( String name ) {

String filename = "images/" + name;

BufferedImage ret = null;

try {

System.out.println( filename );

System.out.println( this.getResource( filename ) );

BufferedImage src = ImageIO.read( this.getResource( filename ) );

// Images returned from ImageIO are NOT managed images. Therefore,

// we copy it into a managed image.

ret = gc.createCompatibleImage( src.getWidth(), src.getHeight(), src.getColorModel().getTransparency() );

Graphics2D g2d = (Graphics2D) ret.getGraphics();

g2d.setComposite( AlphaComposite.Src );

g2d.drawImage( src, 0, 0, null );

g2d.dispose();

} catch( IOException ioe ) {

ioe.printStackTrace();

// ignore so that this method returns null

}

return ret;

}

I added in the System.out.println and see that when I request my new PNG file, that the URL returned is null which leads to the issue with ImageIO.read. If I change my splash screen back to the JPG that Brakeen uses, it displays the image fine. I tried converting the JPG to a PNG using MS Paint and get the same issue that I received with my PNG file. I am positive that I have them in the right directory because I simply saved Brakeen's image in the same directory with a PNG extension and changed my source code from .jpg to .png. It appears that the getResource method is returning null for the two PNG files that I created yet returns a proper URL for other PNG files that I got from Brakeen.

I have searched the forums for the last hour and do not see anything that matches my problem. I even tried moving the images into a res folder that I saw in one post to no avail. I am running Windows XP if that has any bearing on this. I can only think that somehow I have bad stuff in the PNG files. However, when I click my two PNG files, they are properly displayed in Windows.

Any help would be greatly appreciated.

[2851 byte] By [tballan48] at [2007-9-30 11:04:42]
# 1

Here is some output that I got from my System.out.println as well as the stacktrace from ImageIO.read:

images/tankbrigade.png <-- from first System.out

null <-- from second System.out

java.lang.IllegalArgumentException: input == null!

at javax.imageio.ImageIO.read(ImageIO.java:1338)

at com.brackeen.state.ResourceManager.loadImage(ResourceManager.java:94)

at com.ballantine.game.tankbrigade.SplashGameState.loadResources(SplashGameState.java:66)

at com.brackeen.state.GameStateManager.loadAllResources(GameStateManager.java:66)

at com.ballantine.game.tankbrigade.GameManager$1.run(GameManager.java:64)

tballan48 at 2007-7-3 22:38:17 > top of Java-index,Other Topics,Java Game Development...
# 2
It is now working. I do not know what has changed. I am using Eclipse and I think I exited it and relaunched it earlier today. This must have somehow cleared something up.Topic is closed.
tballan48 at 2007-7-3 22:38:17 > top of Java-index,Other Topics,Java Game Development...
# 3
HiHave u any idea what was the exat problem.Now I am facing the same problem.In my case i am using .gif images.I certainly need ur help.ThanksSrikant
srikant at 2007-7-3 22:38:17 > top of Java-index,Other Topics,Java Game Development...
# 4

A common problem are uppercase file extensions (paint tends to do that). Always use lowercase file extensions. And be also sure to slap everyone in your team who commits some file with a non lowercase extension.

Be also sure that you understand how the lookup works. See here:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResource(java.lang.String)

I always use pathes with a leading slash, because I found em to be less painful.

A simple example (src included/double clickable) can be found here:

http://kaioa.com/k/jarimage.jar

oNyx at 2007-7-3 22:38:17 > top of Java-index,Other Topics,Java Game Development...