1
    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);
    }

}

Can JGraphX assign coordinates randomly and Dynamically .Does any Layout Manager API help I dont want to give any coordinates while inserting vertex, and I want the graph library to place the vertices randomly. I also want to know whether there is a method to find out the random coordinates.

Frodo Baggins
  • 8,290
  • 6
  • 45
  • 55
span
  • 75
  • 6
  • [tag:prefuse], seen [here](http://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bprefuse%5d), has a variety of suitable layouts. – trashgod Feb 24 '16 at 12:01
  • No i want to do it using JGraphX or mxgraph itself . Prefuse is another visualization tool – span Feb 25 '16 at 05:44
  • Right, but the layouts might give you some ideas for a [custom](http://www.jgraph.com/downloads/jgraph/jgraphmanual.pdf) [layout](http://www.jgraph.com/downloads/jgraph/legacy/jgraph-tutorial.pdf). – trashgod Feb 25 '16 at 10:06
  • Can you point out which Layout Manager would help me do that .Also I want to use the mxgraph/JgraphX library and not JGraph . Thanks in advance – span Feb 26 '16 at 06:05
  • Please see my question here: http://stackoverflow.com/questions/32758018/mxhierarchicallayout-not-working-as-expected it contains example that can help you, but there is a problem indeed. You have to put coordinate on each cell, then call layout you prefer – Vokail Feb 26 '16 at 09:39
  • But in the question above , the code contains insertvertex function with coordinates given . I want to insert a vertex without having to assign coordinates and let the layout manager take care of where to place the vertex. I can get the coordinates of the vertex by getX() and getY() functions – span Feb 29 '16 at 08:54
  • Randomly, I would say no. But you can just add all your vertices at (0,0) and then have some layout place them somewhere. – Nieke Aerts Mar 16 '16 at 14:10

0 Answers0