Home | | Web Programming | ImageProducer

Chapter: Java The Complete Reference : The Java Library : Images

ImageProducer

ImageProducer is an interface for objects that want to produce data for images.

ImageProducer

 

ImageProducer is an interface for objects that want to produce data for images. An object that implements the ImageProducer interface will supply integer or byte arrays that represent image data and produce Image objects. As you saw earlier, one form of the createImage( ) method takes an ImageProducer object as its argument. There are two image producers contained in java.awt.image: MemoryImageSource and FilteredImageSource. Here, we

will examine MemoryImageSource and create a new Image object from data generated in an applet.

 

MemoryImageSource

 

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);

 

}

 

}

 

The data for the new MemoryImageSource is created in the init( ) method. An array of integers is created to hold the pixel values; the data is generated in the nested for loops where the r, g, and b values get shifted into a pixel in the pixels array. Finally, createImage( ) is called with a new instance of a MemoryImageSource created from the raw pixel data as its parameter. Figure 27-3 shows the image when we run the applet. (It looks much nicer in color.)




Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : Images : ImageProducer |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.