Non-SDN scripts are troublesome with resources. You have to download the resources externally. That's why I made some code which can be used to download any image from the oldschool runescape wiki or any other website for that matter.
public static CompletableFuture<BufferedImage> getImage(File imageFolder, String fileName, String url) {
File imageFile = new File(imageFolder, fileName);
//AccessController.doPrivileged gives the new thread permission to read the image from the data directory
return CompletableFuture.supplyAsync(() -> AccessController.doPrivileged((PrivilegedAction<BufferedImage>) () -> {
try {
if (!imageFile.exists()) {
downloadImage(url, imageFile);
}
return ImageIO.read(imageFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}));
}
private static boolean downloadImage(String urlString, File destination) throws IOException {
URL url = new URL(urlString);
InputStream in = url.openStream();
FileOutputStream fos = new FileOutputStream(destination);
ReadableByteChannel rbc = Channels.newChannel(in);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
in.close();
return true;
}
Example usage:
//Somwhere in your class
private BufferedImage coinIcon;
//In your paint method
if(coinIcon != null) {
g.drawImage(coinsIcon, x, y, null);//Use original dimensions
g.drawImage(coinsIcon, x, y, width, height, null);
}
File imageFolder = new File(getDirectoryData() + File.seperator + "scriptname", "images");
String fileName = "coinstack.png";
String url = "https://oldschool.runescape.wiki/images/Coins_10000.png";
Function<Throwable, Void> errorHandler = e -> {
script.log(e);
return null;
};
ResourceManager.getImage(imageFolder, fileName, url).thenAccept(image -> {
coinIcon = image;
//Note: this is off the main thread, if youre inserting into lists or maps from multiple of these calls, use synchronized or concurrent collentions and maps.
}).exceptionally(errorHandler);
The fileName is the name for how it is saved in the data folder. This code spawns a new thread that reads or downloads the image. Then the thenAccept function is called with the buffered image.