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

2019-02-05 05:50发布

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!

4条回答
Anthone
2楼-- · 2019-02-05 06:04

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 Zoomed in snapshot from the PDF file

查看更多
小情绪 Triste *
3楼-- · 2019-02-05 06:08
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.

查看更多
Viruses.
4楼-- · 2019-02-05 06:12

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
}
查看更多
看我几分像从前
5楼-- · 2019-02-05 06:15

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

查看更多
登录 后发表回答