Reputation: 3001
What is the difference between Image and BufferedImage?
Can I create a BufferedImage directly from an Image source "image.png"?
Upvotes: 28
Views: 43234
Reputation: 209132
What is the difference between
Image
andBufferedImage
?
As stated in the Oracle Java tutorial for working with Images
The BufferedImage class is a cornerstone of the Java 2D immediate-mode imaging API. It manages the image in memory and provides methods for storing, interpreting, and obtaining pixel data. Since BufferedImage is a subclass of Image it can be rendered by the Graphics and Graphics2D methods that accept an Image parameter.
A BufferedImage is essentially an Image with an accessible data buffer. It is therefore more efficient to work directly with BufferedImage. A BufferedImage has a ColorModel and a Raster of image data. The ColorModel provides a color interpretation of the image's pixel data.
Can I create a
BufferedImage
directly from an Image source "image.png"?
Sure.
BufferedImage img = ImageIO.read(getClass().getResource("/path/to/image"));
Upvotes: 15
Reputation: 10468
If you are familiar with Java's util.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.
Upvotes: 33
Reputation: 53880
Image is an abstract class. You can't instantiate Image directly. BufferedImage is a descendant, and you can instantiate that one. So, if you understand abstract classes and inheritance, you'll understand when to use each.
For example, if you were using more than one Image descendant, they're going to share some common properties, which are inherited from Image.
If you wanted to write a function that would take either kind of descendant as a parameter you could do something like this:
function myFunction(Image myImage) {
int i = myImage.getHeight();
...
}
You could then call the function by passing it a BufferedImage or a VolatileImage.
BufferedImage myBufferedImage;
VolatileImage myVolatileImage;
...
myFunction(myVolatileImage);
myFunction(myBufferedImage);
You won't convert an Image to a BufferedImage because you'll never have an Image.
Upvotes: 17