views:

179

answers:

1

Hello,

I'm making a torpedo game for school in java with swing gui, please see the zipped source HERE.

I use custom button icons and mouse cursors of images stored in the /bin/resource/graphics/default folder's subfolders, where the root folder is the program's root folder (it will be the root in the final .jar as well I suppose) which apart from "bin" contains a "main" folder with all the classes. The relative path of the resources is stored in MapStruct.java's shipPath and mapPath variables. Now Battlefield.java's PutPanel class finds them all right and sets up its buttons' icons fine, but every other class fail to get their icons, e.g. Table.java's setCursor, which should set the mouse cursor for all its elements for the selected ship's image or Field.java's this.button.setIcon(icon); in the constructor, which should set the icon for the buttons of the "water".

I watched with debug what happens, and the images stay null after loading, though the paths seem to be correct. I've also tried to write a test file in the image folder but the method returns a filenotfound exception. I've tried to get the path of the class to see if it runs from the supposed place and it seems it does, so I really can't find the problem now.

Could anyone please help me? Thank you.

+1  A: 

You need to load icons like this:

ClassLoader cl= this.getClass().getClassLoader();
URL imageURL   = cl.getResource("resources/...");
ImageIcon icon = new ImageIcon(imageURL);

And you need to add your resource folder to the classpath in Eclipse. Note that the path is not a file path, this way it will work if you decide to bundle your app in a jar file.

Guillaume
Thanks, and sorry for the lack of comments
KáGé