So I’m starting back into Java and it’s been awhile since I’ve done asset management on something not android. I’ve done some google-fu and haven’t quite found an answer to my problem.
I’m writing a modular game library. My graphics section is in a lib project called core. My main game is in it’s own project. I want to be able to send a url for an img in my game project folder to the image class in my core.
Here’s my setup:
So the last time I had to load an image in pure java was when swing was still considered new. There are noticeably more functions to load something than I know what to do with, and none of them seem to work atm.
My current code:
public void loadSpriteFromLocation(String relativeLocation)
throws IOException
{
try
{
//URL url = new URL(getCodeBase(), relativeLocation);
URL url = ClassLoader.getSystemResource(relativeLocation);
System.out.println(url.toString());
sprite = ImageIO.read(url);
System.out.println("Image successfully loaded");
}
catch (IOException e)
{
throw e;
}
}
url = null. (the commented out function won’t even compile). http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource is pretty good at showing resource locations but it doesn’t help me if I’m trying to load from a separate module from where the image is located.
Ideally, I want to be able to load from an executable jar file, but I also want something that’s easy to work with or perhaps a method where I could test the jar first then go locally if need be. (even more ideally, I’d love to write a resource manager that works like the android resources (if that’s really even possible), but I need to start somewhere first)
What am I missing here? Do I need to write a resource builder for this? (because I’m probably going to run into the same problem for the sound)
Any help is appreciated!