我一直在寻找问题的解决方案,但是没有任何事情能满足我的需求。
我要做的是将整个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<Node, Edge> vis =
new VisualizationImageServer<Node, Edge>(vv.getGraphLayout(),
vv.getGraphLayout().getSize());
// Configure the VisualizationImageServer the same way
// you did your VisualizationViewer. In my case e.g.
vis.setBackground(Color.WHITE);
vis.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Edge>());
vis.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line<Node, Edge>());
vis.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>());
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
}