将 JPanel 上绘制的图片保存在文件中 [java]
Save a drawn picture on a JPanel in a file [java]
我编写了一个代码来绘制 Jpanel
frame1.add( new JPanel() {
public void paintComponent( Graphics g ) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
g2.fillRect(0, 0, width, height);
//Drawnig Part
});
- 一切正常
- 现在我的问题是如何将我在
JPanel
上绘制的内容保存在文件 a.PNG
或任何其他类型 中
- 我花了很多时间来编写
Drawing Part
,因此,如果您能通过更改我的代码的所需部分而不是重写整个代码来提出解决方案,那将会很有帮助。
查看 Writing/Saving an Image 了解更多详细信息,但基本上您可以执行类似...
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
// Draw what ever you want to to the graphics context
g2d.dispose();
ImageIO.write(img, "png", new File("My Awesome Drawing.png"));
如果您不能将绘图逻辑与面板分开,您可以简单地使用 BufferedImage
中的 Graphics
上下文来绘制组件。
查看 Exporting a JPanel to an image and Print the whole program layout 示例
我建议您使用 BufferedImage 缓冲绘图操作,如下所示:
// This should not be done in the draw method, but rather when
// the frame is created. You should also make a new buffer image when
// the frame is resized. `frameWidth` and `frameHeight` are the
// frame's dimensions.
BufferedImage bufferImage = new BufferedImage(frameWidth, frameHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics2D bufferGraphics = bufferImage.createGraphics();
// In your draw method, do the following steps:
// 1. Clear the buffer:
bufferGraphics.clearRect(0, 0, width, height);
// 2. Draw things to bufferGraphics...
// 3. Copy the buffer:
g2.drawImage(bufferImage, null, 0, 0);
// When you want to save your image presented in the frame, call the following:
ImageIO.write(bufferImage, "png", new File("frameImage.png"));
如果您需要更多信息,The Java Tutorial on Creating and Drawing to an Image, along with the ImageIO API reference 可能会对您有所帮助。
我编写了一个代码来绘制 Jpanel
frame1.add( new JPanel() {
public void paintComponent( Graphics g ) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
g2.fillRect(0, 0, width, height);
//Drawnig Part
});
- 一切正常
- 现在我的问题是如何将我在
JPanel
上绘制的内容保存在文件a.PNG
或任何其他类型 中
- 我花了很多时间来编写
Drawing Part
,因此,如果您能通过更改我的代码的所需部分而不是重写整个代码来提出解决方案,那将会很有帮助。
查看 Writing/Saving an Image 了解更多详细信息,但基本上您可以执行类似...
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
// Draw what ever you want to to the graphics context
g2d.dispose();
ImageIO.write(img, "png", new File("My Awesome Drawing.png"));
如果您不能将绘图逻辑与面板分开,您可以简单地使用 BufferedImage
中的 Graphics
上下文来绘制组件。
查看 Exporting a JPanel to an image and Print the whole program layout 示例
我建议您使用 BufferedImage 缓冲绘图操作,如下所示:
// This should not be done in the draw method, but rather when
// the frame is created. You should also make a new buffer image when
// the frame is resized. `frameWidth` and `frameHeight` are the
// frame's dimensions.
BufferedImage bufferImage = new BufferedImage(frameWidth, frameHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics2D bufferGraphics = bufferImage.createGraphics();
// In your draw method, do the following steps:
// 1. Clear the buffer:
bufferGraphics.clearRect(0, 0, width, height);
// 2. Draw things to bufferGraphics...
// 3. Copy the buffer:
g2.drawImage(bufferImage, null, 0, 0);
// When you want to save your image presented in the frame, call the following:
ImageIO.write(bufferImage, "png", new File("frameImage.png"));
如果您需要更多信息,The Java Tutorial on Creating and Drawing to an Image, along with the ImageIO API reference 可能会对您有所帮助。