I set the JPanel background color as white. However when I save it into JPG or other image format, the background are all in black. I have put this code TYPE_INT_ARGB
but it doesnt work. How can I set the background to other color? e.g. blue, white etc.
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)