在我的程序中,我有一个paint()方法。 我希望创建绘制的矩形的图像(使用for循环)。 我尝试了下面的方法,确实给了我这些矩形(蓝色),但是背景全部是黑色。 当我运行程序而不创建图像,只在JFrame上绘制矩形时,背景是白色。 我该如何解决这个问题?
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = Image.getGraphics(); <<<----- is this correct?
g.setColor(Color.blue);
for ( ..... ) {
g.fillRect(X , Y, width , height);
....
}
try {
ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}
paintComponent
而不是尝试在JFrame上进行绘制。像JFrame这样的顶级容器没有双缓冲,这可能会导致问题。 - Paul Samsotha