Java Reference
In-Depth Information
else
green
=
255
—
(255 * (x
—
half
-
width))/half
-
width;
// Blue starts from center and peaks at right side.
int blue = 0;
if (x > half
-
width)
blue
=
(255 * (x
—
half
-
width))/half
-
width;
int alpha
=
255; // non
—
transparent
fPixels[i++]
=
(alpha
<<
24)
|
(red
<<
16)
|
(green
<<
8)
|
blue;
}
}
// Now create the image from the pixel array.
fImage
=
createImage (
new MemoryImageSource (fWidth, fHeight, fPixels,
0, fWidth));
}
// init
/** Paint the image on the panel. **/
public void paintComponent (Graphics g)
{
super.paintComponent (g);
g.drawImage (fImage, 0, 0, this);
}
} // class ImagePanel
The
MemoryImageSource
can be set so that if the pixel array is modified the
image changes as well. This involves a subtle aspect of the way the
Image
class
works. The
MemoryImageSource
implements the
ImageProducer
interface.
The
Image
class obtains its pixel data from an
ImageProducer
rather than
simply reading an array.
To animate the display, use
setAnimate()
as follows:
source = new MemoryImageSource (width, height, pixels, 0,
width);
source.setAnimate (true);
image = createImage (source);
To create frames for the animation, we could use a loop in a
run()
method of a
threaded class as in the following code:
...
in the
run()
method
...
while (true)
{
// Invoke a method in the class that
// modifies the pixels in some way
modifyPixels ();
Search WWH ::
Custom Search