javaSE swing生成图片中,clearRect()方法的使用疑惑
我在写图片的时候,遇到了点问题,请看我的demo:
public class One {
public static void main(String[] args) throws IOException {
One one = new One();
one.createImage();
}
public void createImage() throws IOException{
File file = new File("C:/1.jpg");
int width = 200 ;
int height= 200 ;
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
/*问题位置*/g2.setBackground(Color.red);
/*问题位置*/g2.clearRect(0, 0, width, height);
Rectangle2D rect = new Rectangle2D.Double(0, 0, width, height);
Ellipse2D raduis = new Ellipse2D.Double();
raduis.setFrameFromCenter(width/2, height/2, 0, 0);
g2.draw(raduis);
g2.draw(rect);
ImageIO.write(bi, "jpg", file);
}
}
Graphics2D g2 = bi.createGraphics();
g2.setPainter(Color.WIHTE);
g2.fillRect(0, 0, width, height);
g2.setPainter(Color.RED);
Rectangle2D rect = new Rectangle2D.Double(0, 0, width, height);
Ellipse2D raduis = new Ellipse2D.Double();
raduis.setFrameFromCenter(width/2, height/2, 0, 0);
g2.draw(raduis);
g2.draw(rect);
g2.dispose();