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.