writing JUNG graphs to images: can't reliably render complete graph

前端 未结 2 1146
生来不讨喜
生来不讨喜 2021-02-06 12:58

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

相关标签:
2条回答
  • 2021-02-06 13:11

    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:

    1. Get the ScreenImage Class from the excellent Rob Camick's blog
    2. 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());
         }
      }
      
    3. Read also other topics of the above mentioned blog :-)

    0 讨论(0)
  • 2021-02-06 13:26

    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();
    }
    
    0 讨论(0)
提交回复
热议问题