JUNG: save whole graph (not only visible part) as image

人走茶凉 提交于 2019-11-30 05:24:16

I finally found the solution to my problem, using a VisualizationImageServer. Here is an example of how to create an image from a whole JUNG graph, for others struggling with it:

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
}

You might want to have a look at the StandardPrint class I put together a while back:

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

You can render any component (or anything, using SpecialPrint) to an image, using preview()

My experience with capturing images the way dylan202 suggested, was that the quality of images was not up to the mark. Since I needed the images for my presentation.

Another way to get high quality images of your Jung network is to use VectorGraphics library from FreeHEP.

I used this library to generate images in a pdf file. Afterwards I took snapshots of the image from the pdf to my presentation.

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();

It is also possible to generate JPEG or other type of files. For example to generate SVG files only one line needs to be changed:

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

For more information see the manual.

Zoomed in snapshot from the PDF file

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

There is no such a method named "getGraphLayout" of Graph class but of a visualizationviewer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!