As you have already learned from the Overview lesson,
Image
s are described by a width and a height, measured in pixels, and have a coordinate system that is independent of the drawing surface.There are a number of common tasks when working with images.
- Loading an external GIF, PNG JPEG image format file into Java 2D™'s internal image representation.
- Directly creating a Java 2D image and rendering to it.
- Drawing the contents of a Java 2D image on to a drawing surface.
- Saving the contents of a Java 2D image to an external GIF, PNG, or JPEG image file.
This lesson teaches you the basics of loading, displaying, and saving images.
The are two main classes that you must learn about to work with images:
- The
java.awt.Image
class is the superclass that represents graphical images as rectangular arrays of pixels.- The
java.awt.image.BufferedImage
class, which extends theImage
class to allow the application to operate directly with image data (for example, retrieving or setting up the pixel color). Applications can directly construct instances of this class.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. SinceBufferedImage
is a subclass ofImage
it can be rendered by theGraphics
andGraphics2D
methods that accept anImage
parameter.A
BufferedImage
is essentially anImage
with an accessible data buffer. It is therefore more efficient to work directly withBufferedImage
. ABufferedImage
has a ColorModel and a Raster of image data. The ColorModel provides a color interpretation of the image's pixel data.The Raster performs the following functions:
- Represents the rectangular coordinates of the image
- Maintains image data in memory
- Provides a mechanism for creating multiple subimages from a single image data buffer
- Provides methods for accessing specific pixels within the image
The basic operations with images are represented in the following sections:
Reading/Loading an image
This section explains how to load an image from an external image format into a Java application using the Image I/O APIDrawing an image
This section teaches how to display images using thedrawImage
method of theGraphics
andGraphics2D
classes.Creating and drawing To an image
This section describes how to create an image and how to use the image itself as a drawing surface.Writing/saving an image
This section explains how to save created images in an appropriate format.