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

若如初见. 提交于 2019-12-30 01:24:33

问题


I've been looking around for solutions to my question, but nothing does exactly what I want.

What I want to do is save a whole JUNG graph (with custom vertex and edge rendering) to an image (PNG or JPEG). When I save the VisualizationViewer to a BufferedImage, it only takes the visible part. I want to save the whole graph, so that's not an option.

Does anyone have an idea on how to render my whole graph to an image?

Thanks in advance!


回答1:


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
}



回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/10420779/jung-save-whole-graph-not-only-visible-part-as-image

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