jgraph可视化形成png图片

阅读更多

 

pom.xml

1.3.1



    org.jgrapht
    jgrapht-ext
    ${jgrapht.version}



    org.jgrapht
    jgrapht-io
    ${jgrapht.version}



    org.jgrapht
    jgrapht-core
    ${jgrapht.version}

 

 

自定义Edge

public class UDFEdge extends DefaultEdge{
    
    /**
     * 
     */
    private static final long serialVersionUID = 8239849745413195857L;

    /**
     * 
     */
    public UDFEdge() {
        super();
    }

    @Override
    public String toString()
    {
        return StringUtils.EMPTY;
    }
}

 

 

jgraph的图形化,画出png的图片。

   public static void drawGraph() throws Exception {

        File imgFile = new File(
                "temp/jgrapht/hello1.png");
        if (imgFile.exists()) {
            imgFile.delete();
        }
        imgFile.createNewFile();

        DefaultDirectedGraph g = new DefaultDirectedGraph(UDFEdge.class);

        List vertices = IntStream.rangeClosed(1, 20).mapToObj(i -> {
            return String.format("v%d", i);
        }).collect(Collectors.toList());

        Graphs.addAllVertices(g, vertices);

   

        
          g.addEdge("v1", "v2");
          g.addEdge("v2", "v3");
          g.addEdge("v2", "v4");
          g.addEdge("v3", "v5");
          g.addEdge("v2", "v5");
          
          g.addEdge("v5", "v6");
          g.addEdge("v6", "v7");
          g.addEdge("v6", "v8");
          g.addEdge("v6", "v9");
          g.addEdge("v6", "v10");
          g.addEdge("v8", "v11");
          g.addEdge("v8", "v12");
          g.addEdge("v8", "v13");
          g.addEdge("v14", "v13");
          g.addEdge("v15", "v13");
          g.addEdge("v13", "v16");
          g.addEdge("v16", "v17");
          g.addEdge("v16", "v18");
          g.addEdge("v16", "v19");
          g.addEdge("v20", "v19");
          g.addEdge("v4", "v13");
          g.addEdge("v4", "v20");
         

        JGraphXAdapter graphAdapter = new JGraphXAdapter(g);
        // mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
        mxIGraphLayout layout = new mxHierarchicalLayout(graphAdapter);
        layout.execute(graphAdapter.getDefaultParent());

        BufferedImage image =
                mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null);
        ImageIO.write(image, "PNG", imgFile);

    }

 

 

你可能感兴趣的:(jgraph可视化形成png图片)