很多时候需要填充透明,然后再进行绘制。代码如下:
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); }