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?
-
Have you checked the following SO post? [Exporting JUNG graphs to hi-res images (preferably vector based)](http://stackoverflow.com/questions/8518390/exporting-jung-graphs-to-hi-res-images-preferably-vector-based) – ecle Jan 21 '12 at 10:10
-
Dont help, because he will save save only the part shown in the vv, e.g. after zoom in/out.For Vector based output i use Batik. – Thargor Jan 23 '12 at 10:54
-
Who is "he" you are referring to? How about the rest of the solutions for the given post? – ee. Jan 25 '12 at 01:32
-
I can save the part of the graph i see as PNG and SVG, that is no problem. The problem is, that i want so save the whole graph not only a part of it. And this problem is not part of the solution in the given post. In the meantime i think it exist no "simple" solution. I need to write my own functions. – Thargor Jan 25 '12 at 08:15
-
1There is a sample source code `VisualizationImageServerDemo` from **jung-samples-2.0.1.jar** which uses `VisualizationImageServer` class to save the graph at a defined size and position: `Image im = vv.getImage(new Point2d.Double(300,300), new Dimension(600,600));` – ee. Jan 26 '12 at 03:00
-
I know this example. Here the problem is, that VisualizationImageServer dont support all methodes i use with VisualizationViewer (like all renderer). So i can't replace my VisualizationViewer with VisualizationImageServer. But i will look at the getImage() function, maybe there is my solution. – Thargor Jan 26 '12 at 12:52
-
Facing the same problem, did you come up with a solution @Thargor? – Zakum Jul 30 '18 at 16:24
-
@Zakum i stoped working years ago on that project. but as i remeber, i found no solution. – Thargor Dec 14 '18 at 08:26
-
@Thargor Oh, I forgot all about this, too. Managed to make it work using the freehp.hraphicsIO library: http://java.freehep.org/vectorgraphics/ I'll post the solution below for posteriority. – Zakum Dec 18 '18 at 14:07
2 Answers
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.

- 2,157
- 2
- 22
- 30
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.

- 321,443
- 19
- 166
- 288
-
1Dont work. VisualizationViewer is a Element to visualize a network / graph. If in zoom into the graph i see only a part of it and all Methodes save only these part into the Image. – Thargor Jan 20 '12 at 12:14