Reputation: 11
If I want to use Java to implement the SRCAND
effect of BitBlt()
in VC++, that is, perform AND
operation on the image and background. For the following example, how can I mix the red string AAAAAAAA
in the background with the corresponding part of the image m_image
?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class t_srcand {
public static void main(String[] args) {
new t_srcand();
}
public t_srcand() {
JFrame frame = new JFrame("Test SRCAND");
frame.add(new mypanel());
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class mypanel extends JPanel {
private BufferedImage m_image;
public mypanel() {
try {
m_image = ImageIO.read(new File("image.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.red);
g2d.drawString("AAAAAAAA", 0, 20);
if (m_image != null) {
g2d.drawImage(m_image, 0, 0, 300, 200, this);
}
g2d.dispose();
}
}
}
If m_image
is a completely opaque image, when it is drawn on the surface of a red string, it will overwrite the red string. So is it possible to obtain the pixel array of the area covered by the m_image before drawing it?
Upvotes: 0
Views: 26