Loading an Image
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.
Why can't image loading be as easy as that:
BufferedImage image = ImageIO.read(ref);
Actually, "loading an image" IS that easy.
GolezTrol is exactly right: there IS a lot happening here:
The preceding code dynamically gets a reference to whatever graphic needs to be loaded. You can just as easily pass a filename, a URL address, or a Java File reference (among many other possibilities).
The subsequent code gets a reference to your graphics environment in order to display the graphic. If you called "drawImage()" inside a GUI component's "OnPaint()" method, all of this work would already have been done for you.
The ability to decode just about any graphics file and the ability to display it in just about any graphical environment in just one line each IS pretty darn awesome!
The ability to mix'n'match where stuff comes from, how you use it, and where it goes to at will is equally awesome.
IMHO...
I have googled a bit and I think I found the answer to your question. Optimization of the BufferedImage for given device stands behind the the Game Tutorial way of loading the image, depending on device colour depth, device resolution and colour model. There is another reason for using this version of code, which is caching of images in earlier versions of Java. In earlier versions, JVM will mark the image as managed(enables hardware acceleration) when you load it like the tutorial says, while it wont mark it as managed if it's loaded using the ImageIO.read(..). You can see the full explanation in this post. Hope this helps.
精彩评论