JUNG: save the entire graph (not only the visible part) as an image

I have been looking for solutions to my question, but nothing is doing exactly what I want.

What I want to do is save the entire JUNG chart (with custom vertex and edge rendering) to the image (PNG or JPEG). When I save a VisualizationViewer to a BufferedImage, it only accepts the visible part. I want to keep the whole schedule, so this is not an option.

Does anyone have an idea on how to make my entire chart an image?

Thanks in advance!

+6
source share
4 answers

I finally found a solution to my problem using VisualizationImageServer . Here is an example of how to create an image from a whole JUNG chart, 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 eg 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 } 
+11
source

You might want to take a look at the StandardPrint class that I put together a while ago:

http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/print/

You can display any component on the image (or anything using special printing) using the preview function ()

+1
source

My experience with shooting images, as dylan202 suggested, was that the quality of the images did not match the value. Since I needed images for presentation.

Another way to get high-quality images of your Jung network is to use the VectorHraphics library from FreeHEP.

I used this library to create images in a pdf file. Subsequently, I took snapshots of the image from a pdf file for 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 create JPEG files or another type. For example, to generate SVG files, you need to change only one line:

 VectorGraphics g = new SVGGraphics2D(new File("Network.svg"), vv); 

See manual for more information.

PDF file snapshot reduced Zoomed in snapshot from the PDF file

+1
source
 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 method called "getGraphLayout" of the Graph class, but with visualization.

0
source

Source: https://habr.com/ru/post/914750/


All Articles