JUNG:将整个图形(不仅仅是可见部分)保存为图像

我一直在寻找我的问题的解决方案,但没有什么是我想要的。

我想要做的是将整个JUNG图(使用自定义顶点和边渲染)保存到图像(PNG或JPEG)。 当我将VisualizationViewer保存到BufferedImage时,它只占用可见部分。 我想保存整个图表,所以这不是一个选项。

有没有人知道如何将整个图形渲染到图像?

提前致谢!

我终于使用VisualizationImageServer找到了我的问题的解决方案。 这是一个如何从整个JUNG图创建图像的示例,以及其他与之斗争的图:

 import edu.uci.ics.jung.visualization.VisualizationImageServer; ... // Create the VisualizationImageServer // vv is the VisualizationViewer containing my graph VisualizationImageServer vis = new VisualizationImageServer(vv.getGraphLayout(), vv.getGraphLayout().getSize()); // Configure the VisualizationImageServer the same way // you did your VisualizationViewer. In my case eg vis.setBackground(Color.WHITE); vis.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller()); vis.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line()); vis.getRenderContext().setVertexLabelTransformer(new ToStringLabeller()); vis.getRenderer().getVertexLabelRenderer() .setPosition(Renderer.VertexLabel.Position.CNTR); // Create the buffered image BufferedImage image = (BufferedImage) vis.getImage( new Point2D.Double(vv.getGraphLayout().getSize().getWidth() / 2, vv.getGraphLayout().getSize().getHeight() / 2), new Dimension(vv.getGraphLayout().getSize())); // Write image to a png file File outputfile = new File("graph.png"); try { ImageIO.write(image, "png", outputfile); } catch (IOException e) { // Exception handling } 

你可能想看一下我放在一起的StandardPrint类:

http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/print/

您可以使用preview()将任何组件(或使用SpecialPrint的任何组件)渲染到图像

我以dylan202建议的方式捕获图像的经验是,图像质量达不到标准。 因为我需要我的演示文稿的图像。

获得Jung网络高质量图像的另一种方法是使用FreeHEP的VectorGraphics库。

我用这个库在pdf文件中生成图像。 之后我将图像的快照从pdf到我的演示文稿。

 JPanel panel = new JPanel (); panel.setLayout(new FlowLayout()); panel.setBackground(Color.WHITE); panel.add(vv); Properties p = new Properties(); p.setProperty("PageSize","A4"); // vv is the VirtualizationViewer VectorGraphics g = new PDFGraphics2D(new File("Network.pdf"), vv); g.setProperties(p); g.startExport(); panel.print(g); g.endExport(); 

也可以生成JPEG或其他类型的文件。 例如,要生成SVG文件,只需要更改一行:

 VectorGraphics g = new SVGGraphics2D(new File("Network.svg"), vv); 

有关更多信息,请参阅手册 。

放大PDF文件的快照 放大PDF文件的快照

 BufferedImage image = (BufferedImage) vis.getImage( new Point2D.Double(graph.getGraphLayout().getSize().getWidth() / 2, graph.getGraphLayout().getSize().getHeight() / 2), new Dimension(graph.getGraphLayout().getSize())); 

没有像Graph类的“getGraphLayout”这样的方法,而是一个可视化查看器。