How do you use AlphaComposite to superimpose BufferedImages using a mask
I'm trying to learn how to do transparencies using BufferedImages, not shapes. I'd like to:
a) clone my RGB BufferedImage into an TYPE_INT_ARGB
b) build a "mask" in the alpha channel
c) transform the cloned image somehow
d) then composite it back onto the source RGB with the transparency handled by the "mask"
This doesn't seem to work. Does AlphaComposite work if 1 image is just TYPE_INT_RGB? If so, which of the AlphaComposites do I choose to have the argb image show through inside the mask?
BufferedImage rgb = getBufferedImage();
BufferedImage argb =new BufferedImage (rgb.getWidth(), rgb.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = argb.createGraphics();
//deep copy image
g2.drawImage(rgb,0,0,rgb.getWidth(), rgb.getHeight(),null);
// change this image somehow, i.e. grayscale
argb = transformedImage(argb);
// draw shape for clip and build mask
g2.setColor(Color.WHITE);// set the mask for opaque
Rectangle s =new Rectangle(X,Y,W,H);
g2.clip(s);
g2.fillRect(0, 0, argb.getWidth(), argb.getHeight());
// which AlphaComposite do I want?
g2.setComposite(AlphaComposite.SrcAtop);
g2.drawImage(rgb, 0,0,rgb.getWidth(), rgb.getHeight(),null);
g2.dispose();
[1601 byte] By [
mixersofta] at [2007-11-27 6:39:29]

# 2
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class SomeImageOptions {
private JScrollPane getContent(BufferedImage[] images) {
JPanel panel = new JPanel(new GridLayout(1,0));
panel.add(wrap(copyToType(images[0], BufferedImage.TYPE_BYTE_GRAY)));
panel.add(wrap(getComposite(images, 0.4f)));
panel.add(wrap(clippedCopy(images)));
return new JScrollPane(panel);
}
private BufferedImage copyToType(BufferedImage src, int type) {
int w = src.getWidth();
int h = src.getHeight();
BufferedImage copy = new BufferedImage(w, h, type);
Graphics2D g2 = copy.createGraphics();
g2.drawImage(src, 0, 0, null);
g2.dispose();
return copy;
}
private BufferedImage getComposite(BufferedImage[] images, float alpha) {
int w = Math.max(images[0].getWidth(), images[1].getWidth());
int h = Math.max(images[0].getHeight(), images[1].getHeight());
int type = BufferedImage.TYPE_INT_RGB;// good performance
BufferedImage dest = new BufferedImage(w, h, type);
Graphics2D g2 = dest.createGraphics();
int x = (w - images[0].getWidth())/2;
int y = (h - images[0].getHeight())/2;
g2.drawImage(images[0], x, y, null);
AlphaComposite ac = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha);
g2.setComposite(ac);
x = (w - images[1].getWidth())/2;
y = (h - images[1].getHeight())/2;
g2.drawImage(images[1], x, y, null);
g2.dispose();
return dest;
}
private BufferedImage clippedCopy(BufferedImage[] images) {
int w = images[0].getWidth();
int h = images[0].getHeight();
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage dest = new BufferedImage(w, h, type);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(images[0], 0, 0, null);
Rectangle clip = new Rectangle(50, 50);
// Put the clip where you want the image to appear.
// Here, center it in base image.
int x = (w - clip.width)/2;
int y = (h - clip.height)/2;
clip.setLocation(x, y);
g2.setClip(clip);
// Locate the image relative to the clip origin
// to expose the part you want to see.
// Here, show it at 20, 20 from its origin.
x = x - 20;
y = y - 20;
g2.drawImage(images[1], x, y, null);
g2.dispose();
return dest;
}
private JLabel wrap(BufferedImage image) {
ImageIcon icon = new ImageIcon(image);
return new JLabel(icon, JLabel.CENTER);
}
private static void confirmClip(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage dest = new BufferedImage(w, h, type);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.setPaint(Color.red);
// Draw clip on image.
g2.drawRect(20,20,50,50);
g2.dispose();
JOptionPane.showMessageDialog(null, new ImageIcon(dest), "", -1);
}
public static void main(String[] args) throws IOException {
String[] ids = { "cougar.jpg", "Bird.gif" };
BufferedImage[] images = new BufferedImage[ids.length];
for(int j = 0; j < images.length; j++)
images[j] = ImageIO.read(new File("images/" + ids[j]));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new SomeImageOptions().getContent(images));
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
confirmClip(images[1]);
}
}