直接声明的bufferedImage背景是黑色的,怎么使它变透明呢?也就是只显示在它上面画的东西。
以下是一个测试程序,主要是红色部分:
package image;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.image.*;
import java.awt.*;
import java.awt.geom.*;
public class ImageDemo extends JFrame {
public ImageDemo(){
setTitle("Image");
setSize(600, 300);
setLocation(200, 100);
MyPanel mp = new MyPanel();
add(mp);
}
public static void main(String[] args) {
// TODO 自动生成方法存根
ImageDemo id = new ImageDemo();
id.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
id.setVisible(true);
}
}
class MyPanel extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2D ;
BufferedImage buffImage = new BufferedImage(200,200,
BufferedImage.TYPE_3BYTE_BGR);
g2D = buffImage.createGraphics();
int width = buffImage.getWidth();
int height = buffImage.getHeight();
//for(int i = 0; i < width; i++)
// for(int j = 0; j < height; j++)
// buffImage.setRGB(i, j, 0x000000);
//g2D.setBackground(Color.blue);
//g2D.setComposite(AlphaComposite.getInstance(
// AlphaComposite.CLEAR, 0.0f));
//Rectangle2D.Double rect1 = new Rectangle2D.Double(0,0,width,height);
//g2D.fill(rect1);
//g2D.clearRect(0, 0, width, height);
//g2D.setComposite(AlphaComposite.getInstance(
// AlphaComposite.CLEAR, 1.0f));
//g2D.fill(rect1);
Rectangle2D.Float rect = new Rectangle2D.Float(50,50,50,50);
g2D.setPaint(Color.red);
g2D.draw(rect);
g2D.fill(rect);
g.drawImage(buffImage,50,50,null);
}
}