Java Reference
In-Depth Information
// Now inform the MemoryImageSource that the image has
// changed
source.newPixels ();
// Sleep between frames
try {
Thread.sleep (dt);
}
catch (InterruptedException e) {}
}
Here modifyPixels() would be a method in the program that changes the
pixel values in a desired manner. Then by invoking newPixels() the image is
repainted in each pass of the loop, thus creating an animated effect.
11.8.4 Pixel handling with the BufferedImage class
As discussed in Section 11.3, the BufferedImage class offers much more
access to and control of the image data than the Image class. Internally the
BufferedImage consists of the ColorModel , Raster , DataBuffer , and
SampleModel objects, which allow for a wide variety of image types and pack-
ing arrangements for the pixel data. However, for routine pixel handling tasks,
you don't need to delve into the details of these classes to work with images at
the pixel level.
Forexample, you can easily create an image from a pixel array packed with
ARGB data (Section 11.8.1) as follows:
buffered - image =
new BufferedImage (width, height,
bufferedImage.TYPE - INT - ARGB);
buffered - image.setRGB (0, 0, width, height, pixels,
0, width);
The constructor parameters specify the dimensions of the image and the type of
image. In setRGB() the first four parameters specify the top left corner position
and the width and height of the area of the image to be filled by the pixels
array. The last two parameters give the offset into the pixel array where the
data begins and the scan size for a row of pixels (usually just set to the image
width).
Conversely, the ARGB pixel data for an image can be obtained via
int[] pixels = buffered - image.getRGB(0, 0, width, height,
array, 0, width);
This method returns an array with the ARGB data for the area of the image
specified by the first four parameters. If not null, the fifth parameter should be an
Search WWH ::




Custom Search