I'm trying to add an ImageIcon to a JLabel I'm adding to a JPanel to get Graphics for my Java Swing game. However, I can't seem to even create the ImageIcon for the JLabel with my current code. I've already tried looking through Stack Overflow, but I couldn't find an answer that fit my issue. pls help
protected ImageIcon createImageIcon(Class c, String file) {
File f = new File(file);
String path = f.getAbsolutePath();
java.net.URL imgURL = c.getResource(path);
System.out.println(imgURL);
System.out.println(path);
if (path != null) {
return new ImageIcon(path);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
Called from:
Graphic startScreenG = new Graphic();
Icon startScreenI = startScreenG.createImageIcon(MyAppClass, "download.jpg");
JLabel startScreen = new JLabel(startScreenI);
Screen.add(startScreen);
This returns:
null
C:\Users*justmyname*\Documents\NetBeansProjects\IA\download.jpg