Menu

graph to image (e.g. jpg)

2009-07-27
2013-05-29
  • Patrick Sobetzko

    Hi, is there a way to export the image from the visualizationviewer to a file?
    I tried the paint(g) way, but failed.
    Thx in advance.

     
    • Tom Nelson

      Tom Nelson - 2009-07-27

      there are many discussions about it in this forum, and the GraphEditorDemo has sample code to do it.

      Tom Nelson

       
    • Patrick Sobetzko

      I know about the sample, but in my case it does not work. Here is my code:

      FRLayout<Factor, Regulation> layout = new FRLayout<Factor, Regulation>(graph);       
              Dimension dim = new Dimension(1000, 1000);
              layout.setSize(dim);
              layout.setMaxIterations(500);
              for(int i = 0; i < 5; i++)
              {
                  layout.step();
                  System.out.println(i);
              }
              layout.setMaxIterations(0);
              BasicRenderer<Factor, Regulation> renderer = new BasicRenderer<Factor, Regulation>();
              renderer.setVertexRenderer(new GradientVertexRenderer<Factor, Regulation>(Color.WHITE, Color.BLACK, false));
             
             
              VisualizationViewer<Factor, Regulation> view = new VisualizationViewer<Factor, Regulation>(layout, dim);
              Transformer<Factor, Shape> vertexShapeTransformer = new Transformer<Factor, Shape>()
              {
                  public Shape transform(Factor fac)
                  {
                      return new Ellipse2D.Float(-2,-2,4,4);
                  }
              };
             
              view.getRenderContext().setVertexShapeTransformer(vertexShapeTransformer);
              view.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(new Color(200, 200, 200, 120)));
              view.getRenderContext().setEdgeArrowTransformer((Transformer)new DirectionalEdgeArrowTransformer<Regulation, Paint>(5, 3, 2));
              view.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.GRAY));
              view.getRenderContext().setArrowDrawPaintTransformer(new ConstantTransformer(Color.GRAY));
              //view.getRenderContext().setEdgeShapeTransformer((Transformer)new EdgeShape.Line<Regulation, Paint>());
              view.setRenderer(renderer);
              JFrame frame = new JFrame();
              frame.add(view);
              frame.pack();
              frame.setVisible(true);
              System.out.println(view.getWidth()+" "+ view.getHeight());
              BufferedImage img = new BufferedImage(view.getWidth(), view.getHeight(), BufferedImage.TYPE_INT_RGB);
              Graphics2D g = img.createGraphics();
              view.paint(g);
              g.dispose();
              ImageIO.write(img, "TIFF", new File("c:/tmp/network.tiff"));   

       
      • Tom Nelson

        Tom Nelson - 2009-07-28

        are you saying that our sample does not work?

         
    • Patrick Sobetzko

      yes, the image is empty. Should it also work, if i just use the viewer without setting it visible and such things? I also dont want to see some windows pop up, just to draw an image to file ;).

       
      • Tom Nelson

        Tom Nelson - 2009-07-28

        The sample I am referring to is the VisualizationImageServerDemo, which creates a graph, makes an image, then only opens a window to show the image to you. You could instead save the image to a file. Is that the sample code that is not working for you?

         
      • Anonymous

        Anonymous - 2009-07-28

        Just to be sure, try using a PNG format output instead of TIFF, there are issues on ImageIO codecs on certain operating systems which frankly I am too lazy to remember. Anyway, PNG should work in any OS, while TIFF had some issues.

        Rafael

         
    • Patrick Sobetzko

      Now i can draw an image to file with tiff, but i have to put my viewer into a JFrame and make it visible and i have to call the paint()of the frame. Otherwise i just get an empty image.
      i cant find the reason. I also get some nullpointer exeptions from the awt framework...
      should it not simply work like that?
      1.build graph
      2. build renderer and layout,
      3. build viewer(layout)
      4. viewer.setRenderer()
      5.layout.step() // some...
      6.viewer.paint(g) // g of some buffered image
      7. image to file with ImageIO.

      or is there some needs to draw the graph on the viewer somehow

       
      • Tom Nelson

        Tom Nelson - 2009-07-29

        Your choices are to:
        1. really look at our demo and use it as a guide, because it already shows how to do what you want using the VisualizationImageServer instead of the VisualizationViewer
        2. understand enough about how java.awt works so that you can force the awt rendering system to think that the VisualizationViewer JPanel is actually rendered when it is not (this is exactly what the VisualizationImageServer does).

         

Log in to post a comment.