I have the following snippet that loads the images:
String imgName = "/assets/" + name;
URL imgURL = Groovy.class.getResource(imgName);
System.out.println(imgURL.getPath());
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.getImage(imgURL);
return image;
And where the image is drawn:
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,screenWidth,screenHeight);
g.drawImage(background, 0, 0, null);
System.out.println(background.getWidth(null));
if (background.getWidth(null) < 0)
System.exit(1);
However imgURL always returns null. I’m using Eclipse (and fresh to it, blegh), and running the classes with run or debug option. If I change the path, it gives a file not found exception.
File structure is as followed:
Project -> src and assets -> src has Groovy, assets has image -> Groovy has Groovy.class
Edit: I switched back to Netbeans. The imgURL is now loading fine and getPath returns it’s correct path. However the image width / height return -1. This is the actual problem now.
Edit: -1 means that the width is not yet known, however displaying the image shows a white screen.
The problem resided in the use the toolkit to get the image, possibly because I’m using OpenJDK? Anyway, using ImageIO.read(imgURL) fixed this.