Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
As you have already learned from the
Images 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.
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:
java.awt.Image
class is the superclass that represents graphical images as rectangular arrays of pixels.java.awt.image.BufferedImage
class, which extends the Image
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. 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.
The Raster performs the following functions:
The basic operations with images are represented in the following sections:
This section explains how to load an image from an external image format into a Java application using the Image I/O API
This section teaches how to display images using the drawImage
method of the Graphics
and Graphics2D
classes.
This section describes how to create an image and how to use the image itself as a drawing surface.
This section explains how to save created images in an appropriate format.