I\'ve been using JUNG to visualize some simple graphs, and I\'d like to write several of them to a PNG file. Unfortunately, the images often appear to render before the graph is
We usually want to save the state of a manipulated graph. We zoom and position the components the way we like and then we make a picture of the container. This can be achieved like this:
Pass the JPanel inside your JScrollPane, or any Component that hosts your JUNG2 graph to ScreenImage.createImage in order to create an image.
private void writeToImageFile(String imageFileName) {
BufferedImage bufImage = ScreenImage.createImage((JComponent) jPanel1);
try {
File outFile = new File(imageFileName);
ImageIO.write(bufImage, "png", outFile);
System.out.println("wrote image to " + imageFileName);
} catch (Exception e) {
System.out.println("writeToImageFile(): " + e.getMessage());
}
}
Read also other topics of the above mentioned blog :-)
You can also use VisualizationImageServer. It's a subtype of BasicVisualizationServer which adds a getImage method. I have had no trouble with it rendering the graphs properly.
Your code would then be like:
public void writeImage(String filename) {
Layout layout = new CircleLayout<V, E>(jungGraph);
layout.setSize(innerSize);
bvs = new VisualizationImageServer<V,E>(layout);
// [...]
BufferedImage image = (BufferedImage)bvs.getImage();
}