Memor yImageSource
MemoryImageSource is a class that creates a new Image from an array of data. It defines
several constructors. Here is the one we will be using:
MemoryImageSource(int width, int height, int pixel[ ], int offset, int scanLineWidth)
The MemoryImageSource object is constructed out of the array of integers specified by pixel,
in the default RGB color model to produce data for an Image object. In the default color
model, a pixel is an integer with Alpha, Red, Green, and Blue (0xAARRGGBB). The Alpha
value represents a degree of transparency for the pixel. Fully transparent is 0 and fully opaque
is 255. The width and height of the resulting image are passed in width and height. The starting
point in the pixel array to begin reading data is passed in offset. The width of a scan line
(which is often the same as the width of the image) is passed in scanLineWidth.
The following short example generates a MemoryImageSource object using a variation
on a simple algorithm (a bitwise-exclusive-OR of the x and y address of each pixel) from the
book Beyond Photography, The Digital Darkroom by Gerard J. Holzmann (Prentice Hall, 1988).
/*
* <applet code="MemoryImageGenerator" width=256 height=256>
* </applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
public class MemoryImageGenerator extends Applet {
Image img;
public void init() {
Dimension d = getSize();
int w = d.width;
int h = d.height;
int pixels[] = new int[w * h];
int i = 0;
for(int y=0; y<h; y++) {
for(int x=0; x<w; x++) {
int r = (x^y)&0xff;
int g = (x*2^y*2)&0xff;
int b = (x*4^y*4)&0xff;
pixels[i++] = (255 << 24) | (r << 16) | (g << 8) | b;
}
}
img = createImage(new MemoryImageSource(w, h, pixels, 0, w));
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
}
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home