I am currently writing a program that I need to send to a friend as a jar. The program has images that need to be loaded for the program to work properly and I want it all to be
Instead of using /./resources/back_img.png
, use resources/back_img.png
with ClassLoader
.
Here is example :
String path = "resources/back_img.png";
ClassLoader cl = ImageHandler.class.getClassLoader();
URL imgURL = cl.getResource(path);
//URL imgURL = ImageHandler.class.getResource(path);
if (imgURL != null) {
ImageIcon icon = new ImageIcon(imgURL, description);
Image img = icon.getImage();
Image sizedImg = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
return new ImageIcon(sizedImg);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}