Whenever dealing with the loading and rendering of images in Java, I have previously always used BufferedImage
s to store and manipulate the images in memory.
However, I have recently come across a few different sites that use the Image
class instead of BufferedImage
and this got me wondering - what are the differences?
I'm aware that a BufferedImage
has a larger/optimised toolset, but does come at any cost? If so, when does this cost become noticeable? In which situations would you use an Image
over a BufferedImage
, or vice-versa?
List, the difference between Image and BufferedImage is the same as the difference between List and LinkedList. Image is a generic concept and BufferedImage is the concrete implementation of the generic concept; kind of like BMW is a make of a Car. Show activity on this post. Image is an abstract class.
Java For Testers Java BufferedImage class is a subclass of Image class. It is used to handle and manipulate the image data. A BufferedImage is made of ColorModel of image data. All BufferedImage objects have an upper left corner coordinate of (0, 0).
A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0).
Java implements a particular type of object called a BufferedImage for images in Java. A BufferedImage can be read from several distinct image types (i.e., BMP, HEIC, etc.). Not all of these are backed by ImageIO itself, but there are plugins to extend ImageIO and other libraries such as Apache Imaging and JDeli.
BufferedImage extends Image
. Image
is just a base abstract class and you can't instantiate it. Under the hood you are using BufferedImage
or another implementation for sure.
There shouldn't be any real performance difference between directly creating a BufferedImage and a Toolkit image (java.awt.Toolkit or Image#getScaledInstance). You'll never have an actual instance of Image because it's an abstract class; you'll only be dealing with its subclasses (e.g. BufferedImage).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With