I read in a Game tutorial how to load images, and they did this:
public Image loadImage(... String ref ...) {
...
URL url = this.getClass().getClassLoader().getResource(ref);
BufferedImage source = ImageIO.read(url);
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Image image = gc.createCompatibleImage(source.getWidth(), source.getHeight(), Transparency.BITMASK);
image.getGraphics().drawImage(source, 0, 0, null);
return image;
}
This is absolutely not the best way in terms of usability and clarity (= nobody could find this way just by using their healthy brains). Why is it that complicated? Does it have to be? Why can’t image loading be as easy as that:
BufferedImage image = ImageIO.read(ref);
Is there an easier way if you just want to load an image in a way that Java automatically does all the default “stuff” it has to do? Is there maybe a reason the author of that code chose another way, because he wanted to achieve some non-default behaviour? When writing an answer please understand that I am looking for an easy way to solve the problem of image loading and also the ability and understanding to apply more complicated means if nessesary.
edit:
Also both answers are good presentations of their respective authors oppinions, until now I can’t accept one answer, because no answer shows a simpler way and/or explains what exactly is happening.
Well, a lot more is happening here. An url is opened, the url contents are loaded in a buffer. The image is loaded from the buffer. It is converted to a device compatible bitmap.
I think 4 lines of code to do all this is pretty awesome. I don’t want to sound old, but when I wanted to draw a bitmap in Ye Olde C++ in DOS, I first had to visit the library to get a book about various bitmap formats, then write about 10.000 lines of code to support only a few of them… And then I just got a bitmap (which is about the easiest of images) on a local computer. No jpg, no networking, let alone internet, and certainly not cross platform.
If you need a lot of images to be loaded from the internet, feel free to put it in a function, like you did here. It doesn’t seem weird to me that these quite unrelated functionalities aren’t by default grouped in a single statement.