tii*_*in4 14 java export image jpanel
我正在尝试使用Java开发某种绘画.
我有一个位于JPanel内的JComponent.
我已经可以在该JComponent中绘制线条和矩形.
现在,我如何将这些图纸导出为图像(png,gif,jpg)?
我试过这个:
BufferedImage b = new BufferedImage(1700,1100,BufferedImage.TYPE_INT_RGB);
this.print(getGraphics());
try{ImageIO.write(b,"png",new File("test.png"));}catch (Exception e) {}
Run Code Online (Sandbox Code Playgroud)
但这只会创建一个全黑的.png文件.
救命!!!
解决!!!
BufferedImage bi = new BufferedImage(this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
this.paint(g); //this == JComponent
g.dispose();
try{ImageIO.write(bi,"png",new File("test.png"));}catch (Exception e) {}
Run Code Online (Sandbox Code Playgroud)
首先,print()是不正确的方法.我猜应该工作(尚未测试)是:获取BufferedImage的图形(b.createGraphics())并将该图形对象用于paint()您的面板/组件.
(例如yourPanel.paint(b.createGraphics());)
| 归档时间: |
|
| 查看次数: |
16094 次 |
| 最近记录: |