如何使用JGraphX绘制图形而无需分配坐标

时间:2016-02-24 06:42:10

标签: java swing jgraphx

    import javax.swing.JFrame;

         import com.mxgraph.swing.mxGraphComponent;
         import com.mxgraph.view.mxGraph;
       import com.mxgraph.view.*;

         public class HelloWorldMX extends JFrame
      {


          private static final long serialVersionUID = -2707712944901661771L;

    public HelloWorldMX()
    {
        super("Hello, World!");

        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        try
        {
            Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
                    30);
            Object v2 = graph.insertVertex(parent, null, "World!", 240, 150,
                    80, 30);
            graph.insertEdge(parent, null, null, v1, v2);
            Object v3 = graph.insertVertex(parent, null, "TXP", 0, 0, 240, 30);
              graph.insertEdge(parent, null, null, v1, v3);


            Object v4=  graph.insertVertex(parent, null, "2.1", 0, 0, 220, 30);
            Object v5=  graph.insertVertex(parent, null, "2.2", 0, 0, 220, 30);
            Object v6=  graph.insertVertex(parent, null, "2.3", 0, 0, 220, 30);

              graph.insertEdge(parent, null, null, v1, v4);
              graph.insertEdge(parent, null, null, v2, v6);
              graph.insertEdge(parent, null, null, v3, v6);
              graph.insertEdge(parent, null, null, v4, v6);


        }
        finally
        {
            graph.getModel().endUpdate();
        }

        mxGraphComponent graphComponent = new mxGraphComponent(graph);
        getContentPane().add(graphComponent);

        var layoutMgr = new mxLayoutManager(graph);
        mxLayoutManager.layoutMgr.getLayout = function(parent) {
        return layoutMgr;
    };
    }

    public static void main(String[] args)
    {
        HelloWorldMX frame = new HelloWorldMX();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 320);
        frame.setVisible(true);
    }

}

JGraphX可以随机和动态地分配坐标。是否有任何布局管理器API帮助 我不想在插入顶点时给出任何坐标,我希望图形库随机放置顶点。我也想知道是否有找出随机坐标的方法。

0 个答案:

没有答案