Path to image resources in custom classes

Sorry to ask such a basic question, but I am having trouble using images in my custom classes. I can load images within Eclipse using any number of approaches including a relative path from the working directory, the Class.getResource() function, and so on, but none of that works when building my vmod file and running it in Vassal. I did manage to get images to display within a JLabel, using html, but that is a fairly limited approach. I could, of course, put images in the Vassal working directory, but can’t ask users of the mod to do that. All the images I want are in the “images” folder of the vmod.

I suspect the problem is I am looking for paths relative to my own classes, rather than to the ClassLoader.

An example of what does not work:

URL resource = Resources.getResource(RulesFrame.class, "/images/Starship200.png");
if(resource!=null){ 
	System.out.println(resource.toString());
	ImageIcon icon = new ImageIcon(resource) ;
	Image messinaImage = icon.getImage();		
	setIconImage(messinaImage);
}

This works fine within Eclipse, where I have an images folder at the same level as my package folder (“zap”, because i have not bothered changing it). It does not work in the vmod, though, and throws an uncaught exception from the "getResource() function.

java.lang.IllegalArgumentException: resource /images/Starship200.png relative to *** not found.

where *** is whatever class I use in that function.

I also looked through the Vassal source code to find examples of code that converts from the filename to a usable image. I tried this, which works fine in Eclipse when the class is tested in isolation:

Image img = null;

try {
	img = ImageUtils.getImageResource("/images/Starship200.png");
	setIconImage(img);
} catch (ImageIOException e) {
	e.printStackTrace();
}

When the same code is run within Vassal, it throws an exception, which is caught and nothing crashes, but I don’t get the image either.

VASSAL.tools.image.ImageNotFoundException

I have spent quite a bit of time on this, so I would appreciate a pointer in the right direction. As a last resort, I suspect I could get images from the pieces in the game, working backwards, but that seems very clumsy.

The second bit of code posted there was used by Vassal to load its spash image, from a different “images” folder, so perhaps it is not surprising it failed. I am currently looking for the bit that loads user-supplied images, and expect to have more luck.

Hi,

If you need to load an Icon, you can use:

plus = new JButton(new OpIcon(Op.load(“plus16.png”)));

To use the image directly, try

ImageOp op = Op.load(“image.png”);
g.drawImage(op.getImage(), x, y);

Regards.

That worked perfectly, thanks… I was just trying to navigate through all the Op-related code, and might have got there in the end, but it is slow-going interpreting other people’s code. I am beginning to appreciate how complex Vassal is.

It appears to me that Op.load() always throws a null pointer exception if image.png does not exist. This could be considered brittle. It’s handy if the image is required early, because it fails hard and fast, but it could be awkward if the missing image that belonged to a counter used once every few games.

Thus spake Arcuate via messages:

It appears to me that Op.load() always throws a null pointer exception
if image.png does not exist.

I don’t see how. Can you show me a stack trace?


J.

You are right… I can’t reproduce this now. I must have had a different problem.