img
FIGURE 25-4
Sample output from
MemoryImageGenerator
The data for the new MemoryImageSource is created in the init( ) method. An array of
integers is created to hold the pixel values; the data is generated in the nested for loops where
the r, g, and b values get shifted into a pixel in the pixels array. Finally, createImage( ) is called
with a new instance of a MemoryImageSource created from the raw pixel data as its parameter.
Figure 25-4 shows the image when we run the applet. (It looks much nicer in color.)
ImageConsumer
ImageConsumer is an abstract interface for objects that want to take pixel data from images
and supply it as another kind of data. This, obviously, is the opposite of ImageProducer,
described earlier. An object that implements the ImageConsumer interface is going to create
int or byte arrays that represent pixels from an Image object. We will examine the PixelGrabber
class, which is a simple implementation of the ImageConsumer interface.
PixelGrabber
The PixelGrabber class is defined within java.lang.image. It is the inverse of the
MemoryImageSource class. Rather than constructing an image from an array of pixel
values, it takes an existing image and grabs the pixel array from it. To use PixelGrabber,
you first create an array of ints big enough to hold the pixel data, and then you create a
PixelGrabber instance passing in the rectangle that you want to grab. Finally, you call
grabPixels( ) on that instance.
The PixelGrabber constructor that is used in this chapter is shown here:
PixelGrabber(Image imgObj, int left, int top, int width, int height, int pixel[ ],
int offset, int scanLineWidth)
Here, imgObj is the object whose pixels are being grabbed. The values of left and top specify
the upper-left corner of the rectangle, and width and height specify the dimensions of the
rectangle from which the pixels will be obtained. The pixels will be stored in pixel beginning
at offset. The width of a scan line (which is often the same as the width of the image) is passed
in scanLineWidth.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home