Java Reference
In-Depth Information
Invoking the grabPixels() method then initiates the filling of the pixel
array. This method can return before the pixel array has finished being filled. In
the snippet above, the status of the pixel filling is obtained and checked against
the ImageObserver flags (see Section 11.3).
11.8.3 The MemoryImageSource class
Yo u can also create images from pixel arrays. The class MemoryImageSource
offers a convenient way to create images from pixel arrays and also for creat-
ing simple animations. Below we show the ImagePanel class, which gives an
example of creating an image from a pixel array whose element values are created
from a spectrum of RGB and alpha values:
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
/** Create an image from a pixel array. **/
class ImagePanel extends JPanel
{
Image fImage;
int fWidth = 0, fHeight = 0;
int[] fPixels;
/** Create image with a pixel array and
* MemoryImageSource. **/
void init () {
fWidth = getSize ().width;
fHeight = getSize ().height;
fPixels = new int [fWidth * fHeight];
int i = 0;
int half - width = fWidth/2;
// Build the array of pixels with the color components.
for (int y = 0; y < fHeight; y++) {
for (int x = 0; x < fWidth; x++) {
// Start red on left and decrease to zero at center
int red = 255 (512 * x)/fWidth;
if (red < 0) red = 0;
// Green peaks in center
int green;
if (x < half - width)
green = (512 * x)/fWidth;
 
Search WWH ::




Custom Search