我將JPanel
的背景顏色設置爲白色。但是,當我將它保存爲JPG或其他圖像格式時,背景爲黑色。我已經把這個代碼TYPE_INT_ARGB
,但它不起作用。我怎樣才能將背景設置爲其他顏色?例如藍色,白色等當我將圖像保存爲JPG格式時,爲什麼會出現黑色背景?
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)
使用帶'JPEG的'TYPE_INT_ARGB'沒什麼意義。 「'A」代表「alpha」或「transparency」,但JPEG不具有透明度。嘗試使用PNG代替。 – wchargin 2012-06-11 03:23:22