Java Reference
In-Depth Information
For example, you can call
BufferedImage
's
int getWidth()
and
int
getHeight()
methods to obtain an image's width and height without having to
deal with image observers. (Because
BufferedImage
extends
Image
, the image
observer-oriented width and height getter methods are also available.)
BufferedImage
declares three constructors, with
BufferedImage(int
width, int height, int imageType)
being the simplest. The arguments
passedtothisconstructoridentifyabufferedimage'swidth(inpixels),height(inpixels),
and type (the format used to store pixels).
Although
BufferedImage
declares several type constants,
TYPE_INT_RGB
(each pixel has red, green, and blue color components but no alpha component),
TYPE_INT_ARGB
(each pixel has alpha, red, green, and blue components), and
TYPE_INT_ARGB_PRE
(same as
TYPE_INT_ARGB
except that each pixel's color
component values are premultiplied with its alpha value) are commonly used.
Note
The compositing portion of the rendering pipeline normally has to multiply
each pixel's color component by its alpha value. Because this takes time,
BufferedImage
letsyouoptimizethisprocessbypremultiplyingeachpixel'scolor
components and storing the results as new color component values.
The following example instantiates
BufferedImage
to describe a 100-column-
by-50-row buffered image that stores pixels of RGB type:
BufferedImage
bi
=
new
BufferedImage(100,
50,
BufferedImage.TYPE_INT_RGB);
BufferedImage
zeroseachpixel'scolorcomponentssothattheimageisinitially
empty.Ifthebufferedimageisof
TYPE_INT_RGB
,thesepixelsareblack.Ifthebuf-
feredimageisof
TYPE_INT_ARGB
,thesepixelsaretransparent.Drawingatranspar-
ent buffered image over a destination results in only the destination pixels appearing.
One way to populate a buffered image is to invoke its
void setRGB(int x,
int y, int rgb)
method.
setRGB()
sets the pixel at (
x
,
y
) to the 32-bit
rgb
value. If you specify an alpha component (as the most significant 8 bits), the
alpha component is ignored when the type is
TYPE_INT_RGB
. However, the alpha
component is stored with the red, green, and blue color components when the type is
TYPE_INT_ARGB
.
Thefollowingexamplesetsoneofthepreviouslycreatedbufferedimage'spixelstoa
specific value:
bi.setRGB(10, 10, 0x80ff0000);