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

前端 未结 2 1148
生来不讨喜
生来不讨喜 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 :-)

提交回复
热议问题