Question

I use JUNG to visualize my graph / network. Now i want to save the graph (as seen in the VisualizationViewer) in a image file. I use the paint() / paintAll() function of the VisualizationViewer (who extend JPanel). But with this function, only the part who is actually shown in the VisualizationViewer (for example after zooming in) is in the image. I want to draw all Vertexes and Edges. Is there a way to draw all Elements?

Was it helpful?

Solution

I found a solution using the freeHEP library and JUNG's VisualizationImageServer:

private void doSaveAs() throws IOException {
    // instantiate and configure image-able visualization viewer
    VisualizationImageServer<Vertex, Edge> vis =
            new VisualizationImageServer<Vertex, Edge>(this.visViewer.getGraphLayout(),
                                                       this.visViewer.getGraphLayout().getSize());

    setUpAppearance(vis);

    ExportDialog export = new ExportDialog();
    export.showExportDialog(vis, "Export view as ...", vis, "export");
}

When called, this will open an export dialog to the user, where directory and filetype can be selected.

In this snippet, ExportDialog is org.freehep.graphicsbase.util.export.ExportDialog, which you would have to somehow get to your build path, in my case using maven by adding freehep-graphicsio to my pom file.

The field this.visViewer contains your regular VisualizationViewer instance, that you would also use for displaying your graph.

The method setUpAppearance(vis); performs the same setup that I do on the VisualizationViewer instance for displaying purposes. Here's an example, the details will probably vary for you:

private void setUpAppearance(BasicVisualizationServer<Vertex, Edge> vis) {
    vis.setBackground(BGCOLOR);
    vis.setPreferredSize(new Dimension(1500, 600)); // Sets the viewing area

    // modify vertices
    vis.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
    vis.getRenderContext().setVertexFontTransformer(Transformers.vertexFontTransformer);
    vis.getRenderContext().setVertexShapeTransformer(Transformers.vertexShapeTransformer);
    vis.getRenderContext().setVertexFillPaintTransformer(Transformers.vertexFillPaintTransformer);
    vis.getRenderContext().setVertexDrawPaintTransformer(Transformers.vertexDrawPaintTransformer);
    vis.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);

    // modify edges
    vis.getRenderContext().setEdgeShapeTransformer(Transformers.edgeShapeTransformer);
    vis.getRenderContext().setEdgeDrawPaintTransformer(Transformers.edgeDrawPaintTransformer);
    vis.getRenderContext().setArrowDrawPaintTransformer(Transformers.edgeDrawPaintTransformer);
    vis.getRenderContext().setArrowFillPaintTransformer(Transformers.edgeDrawPaintTransformer);
    vis.getRenderContext().setEdgeArrowPredicate(Transformers.edgeArrowPredicate);
    vis.getRenderContext().setEdgeStrokeTransformer(Transformers.edgeStrokeHighlightingTransformer);
}

As a last step you need to figure out when doSaveAs should be called. For instance you could add a Button on the UI for that.

OTHER TIPS

I don't know anything about JUNG, but it is just extends a JPanel to do the painting then you should be able to use the Screen Image class to create an image of any component.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top