天天看點

JAVA中如何給Image填充透明

很多時候需要填充透明,然後再進行繪制。代碼如下:

public void init(JComponent comp)
{
    Graphics2D g2d = (Graphics2D )comp.getGraphics();
 
    //填充透明
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
    g2d.fillRect(0, 0, width, height);
 
    //進行繪制
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
    g2.setPaint(Color.RED);
    g2.fillOval(50,50,100,100);
}