2012-05-02 91 views
6

我一直在寻找解决方案来解决我的问题,但没有什么是我想要的。JUNG:将整个图形(不仅仅是可见部分)保存为图像

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

有没有人有如何呈现我的整个图形的想法?

在此先感谢!

回答

11

我终于找到了解决我的问题,使用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 
} 
+0

而且,如果你需要保存矢量图形(而不是PNG),看到这一点:http://stackoverflow.com/questions/8518390/exporting-jung -graphs-to-hi-res-images-preferred-vector-based – bikashg

+0

@ dylan202保存的图像不反映任何颜色,形状或可见性。你是否困扰这些事情,并找到解决办法? – SacJn

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

有没有这样的一个名为“getGraphLayout”图形类,但一个visualizationviewer的方法。

+0

正是!我纠正了它:) – dylan202

1

dylan202提出的拍摄图像的经验是,图像的质量达不到标准。因为我需要用于演示文稿的图像。

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

我用这个库在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); 

欲了解更多信息,请参阅manual

放大快照从PDF文件 Zoomed in snapshot from the PDF file

相关问题