我将 aJPanel
的背景颜色设置为白色。但是,当我将其保存为 JPG 或其他图像格式时,背景是黑色的。我已经放了这段代码TYPE_INT_ARGB
,但它不起作用。如何将背景设置为其他颜色?例如蓝色、白色等。
public void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// Create a graphics contents on the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(1)); // set the thickness of polygon line
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.00f));
g2d.setPaint(Color.black);//color of the polygon line
g2d.setBackground(Color.WHITE);
//draw polygon
for (Polygon triangle : triangles)
g2d.drawPolygon(triangle);
try {
File file = new File("newimage.jpg");
ImageIO.write(bufferedImage, "jpg", file);
} catch (IOException e) {
}
}//public void paint(Graphics g)