Skip to main content
GameDev.net gamedev.net
🔒 Locked

Java Image-Handling?

Started by Fuji Nov 19, 2010 at 11:50 AM 2 replies 1.5k views
Original Post
Fuji
Fuji
I'm unsure of which class I should instantiate. I've seen BufferedImage, ImageIcon, and Image with the getResource() method called. I'm not entirely sure of the differences between the classes, but it's crucial that I know how to proceed. My first guess would be that BufferedImage uses less memory, but I'm not quite sure. I'll be doing animation and such, by the way. Thanks in advance; I've searched a few sites but there seems to be contradiction.
C++: Where your friends have access to your private members
OrangyTang
OrangyTang

Image is just the base class for all image types. It gives you the basic stuff like size and being able to use it for drawing. You can't create one directly, instead you create something like a BufferedImage (which is an Image).

BufferedImage is 'an image with an accessible buffer of data'. Ie. it's an image where you can reach around and touch the raw pixel data in memory.

ImageIcon is actually an Icon. You'll only use this if you want to put an image on a button or a menu. Sometimes you'll see people being lazy and use this instead of loading an image the proper way via ImageIO.

Most of the time you want to use ImageIO to read() a BufferedImage. Then you can either hold it as an Image (if you just want to draw it) or as a BufferedImage if you want to tinker with the pixel data directly.

HTH
Fuji
Fuji
For future reference for handling tileset and spritesheet Images, would it be a safe assumption that I should use a BufferedImage not typed as an Image in order to access pixel data directly? It seems like getSubImage() is exactly what I'd need! Thanks a lot; I can make some good progress with this information :)
C++: Where your friends have access to your private members
aryx
aryx
Quote:
Original post by Fuji
For future reference for handling tileset and spritesheet Images, would it be a safe assumption that I should use a BufferedImage not typed as an Image in order to access pixel data directly? It seems like getSubImage() is exactly what I'd need! Thanks a lot; I can make some good progress with this information :)

You wouldn't want to call getSubImage() in a tight loop (i.e., your rendering loop). If you break your tileset up into an array of all the individual tiles though, that would be fine, since it would only need to happen once for every time the tileset is loaded (which probably won't be too often). Another option is to use drawImage with source x/y/width/height parameters.

[Edit]
Actually, after reading the documentation, I should point out that getSubImage() isn't quite as bad in a tight loop as what I originally though because it shares the same data array rather than making a copy. Nevertheless, the instantiation of BufferedImages is probably going to have some impact if getSubImage() were called in a tight loop.

[Another Edit]
This link may be of interest too, although I've never dealt with volatile images before.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.