import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.media.jai.operator.CompositeDescriptor;
public class CompositeExample {
final static private int IMG_WIDTH = 1024;
final static private int IMG_HEIGHT = 1024;
final static private int BLOCK_WIDTH = 400;
final static private int BLOCK_HEIGHT = 200;
public BufferedImage createImage(String filename, int x, int y, Color foreColor, Color backColor) {
BufferedImage image = null;
image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.setColor(backColor);
g2.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
g2.setColor(foreColor);
g2.fillRect(x, y, BLOCK_WIDTH, BLOCK_HEIGHT);
g2.dispose();
return image;
}
public RenderedOp composite(String backFilename, String foreFilename) {
RenderedOp backImage = JAI.create("fileload", backFilename);
RenderedOp foreImage = JAI.create("fileload", foreFilename);
int[] bands;
ParameterBlock params;
bands = new int[]{0, 1, 2};
params = new ParameterBlock();
params.addSource(backImage);
params.add(bands);
RenderedOp backRGB = JAI.create("bandSelect", params);
bands = new int[]{3};
params = new ParameterBlock();
params.addSource(backImage);
params.add(bands);
RenderedOp backAlpha = JAI.create("bandSelect", params);
bands = new int[]{0, 1, 2};
params = new ParameterBlock();
params.addSource(foreImage);
params.add(bands);
RenderedOp foreRGB = JAI.create("bandSelect", params);
bands = new int[]{3};
params = new ParameterBlock();
params.addSource(foreImage);
params.add(bands);
RenderedOp foreAlpha = JAI.create("bandSelect", params);
params = new ParameterBlock();
params.addSource(foreRGB);
params.addSource(backRGB);
params.add(foreAlpha);
params.add(backAlpha);
params.add(Boolean.FALSE);
params.add(CompositeDescriptor.DESTINATION_ALPHA_LAST);
RenderedOp result = JAI.create("composite", params, null);
return result;
}
public CompositeExample() {
Color white = new Color(1.0f, 1.0f, 1.0f, 1.0f);
Color red = new Color(1.0f, 0f, 0f, 1.0f);
Color transparent = new Color(1.0f, 1.0f, 1.0f, 0.0f);
Color halfBlue = new Color(0f, 0f, 1.0f, .5f);
BufferedImage backgroundImage = createImage("background1.png", 200, 400, red, white);
RenderedOp op1 = JAI.create("filestore", backgroundImage, "background1.png", "png");
BufferedImage foregroundImage = createImage("foreground1.png", 400, 350, halfBlue, transparent);
RenderedOp op2 = JAI.create("filestore", foregroundImage, "foreground1.png", "png");
RenderedOp compositeImage = composite("background1.png", "foreground1.png");
RenderedOp op3 = JAI.create("filestore", compositeImage, "composite.png", "png");
}
public static void main(String[] argv) {
CompositeExample test = new CompositeExample();
}
}