Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 421
dylan202 Avatar asked May 02 '12 19:05

dylan202


2 Answers

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
}
like image 88
dylan202 Avatar answered Sep 28 '22 07:09

dylan202


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

like image 22
ControlAltDel Avatar answered Sep 28 '22 07:09

ControlAltDel