Java Reference
In-Depth Information
for (int y = 0; y < fHeight; y++) {
for (int x = 0; x < fWidth; x++) {
// Peaks white in middle
int gray = (255 * x)/half - width;
if (x > half - width) gray = 510 gray;
fPixels[i++] = (byte) gray;
}
}
// Create a BufferedIamge with the gray values in
// bytes.
fBufferedImage =
new BufferedImage (fWidth, fHeight,
BufferedImage.TYPE - BYTE - GRAY);
// Get the writable raster so that data can be changed.
WritableRaster wr = fBufferedImage.getRaster ();
// Now write the byte data to the raster
wr.setDataElements (0, 0, fWidth, fHeight, fPixels);
} // makeImage
/** Draw the image on the panel. **/
public void paintComponent (Graphics g) {
super.paintComponent (g);
if (fBufferedImage! = null)
g.drawImage (fBufferedImage, 0, 0, this);
}
} // class GrayBufImagePanel
With the BufferedImage we can also create an animation by altering the pixel
array for each frame, invoking the setRGB() method to reload the pixel data,
and then invoking repaint() .Amore sophisticated approach, which more
closely resembles the MemoryImageSource animation technique, is to build
the BufferedImage with a DataBufferInt object that holds the pixels plus
a RGB ColorModel and a WritableRaster that allows direct modification
of the raster data. For each frame of the animation, you modify the pixel array and
then invoke repaint() . See the Web Course Chapter 11 for demonstrations of
these two techniques.
11.9 Filtering
The Java 2D API provides a framework for filtering, or processing, images. That
is, a source image enters a filter, the filter modifies the image data in some way,
and a new image emerges out of the filter (the original is unaffected). Several
 
Search WWH ::




Custom Search