Question

I have created an undirected graph, however when I set down the co-ordinates they never seem to match up correctly.

Integer v1 = nodeCount;
g.addVertex(v1);
layout.transform(v1);
double x =  Math.random() * 600;
double y = Math.random() * 600;
System.out.println("x and y "  + x + " " + y);
layout.setLocation(v1, x, y);

The problem is that it can generate say and X value of 300 and another with an X value of 350 and somehow the 350 one be to the left of the other one, clearly showing it has not placed them right.

I also created an example below, that ran through and starting at 0 it then creates a vertex at 50 more in both X and Y direction, and you can clearly see that they are not equally spaced. (This is using FRLayout2).

enter image description here

Whereas this example uses FRLayout (the layout that i was actually using) using the same method, and you can see from it that the first 2 seem to be right, but then from the 3rd it goes wrong completely.

enter image description here

This is my code.

public class AnimatingAddNodeDemo extends JApplet {
    @Override
    public void init() {

        //create a graph
        Graph<Number, Number> ig = Graphs.synchronizedUndirectedGraph(new UndirectedSparseMultigraph<Number, Number>());

        ObservableGraph<Number, Number> og = new ObservableGraph<Number, Number>(ig);
        og.addGraphEventListener(new GraphEventListener<Number, Number>() {

            public void handleGraphEvent(GraphEvent<Number, Number> evt) {
                //System.err.println("got " + evt);
            }
        });
        this.g = og;
        //create a graphdraw
        layout = new FRLayout<Number, Number>(g);
        layout.setSize(new Dimension(600, 600));   
        setSize(730, 680);
        Relaxer relaxer = new VisRunner((IterativeContext) layout);
        relaxer.stop();
        relaxer.prerelax();

  Layout<Number, Number> staticLayout = new StaticLayout<Number, Number>(g, layout);
  vv = new VisualizationViewer<Number, Number>(staticLayout, new Dimension(550, 550));

  JRootPane rp = this.getRootPane();
  rp.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);

And the method for adding the vertecies

public void process() {

        vv.getRenderContext().getPickedVertexState().clear();
        vv.getRenderContext().getPickedEdgeState().clear();
        try {
            if (g.getVertexCount() < 100) {
                //add a vertex

                Integer v1 = nodeCount;
                g.addVertex(v1);
                layout.transform(v1);
                double x =  Math.random() * 600;
                double y = Math.random() * 600;
                System.out.println("x and y "  + x + " " + y);
                layout.setLocation(v1, x, y);


                if (layout.getX(v1) > furthestRight){
                    furthestRight = layout.getX(v1);
                }
                if (layout.getX(v1) < furthestLeft){
                    furthestLeft = layout.getX(v1);
                }
                System.out.println(layout.getX(v1) + " Lowest is " + furthestLeft + " and " + layout.getY(v1) + "highest is " + furthestRight);

                nodeCount++;
                System.out.println("adding vertex " + v1);
                vv.getRenderContext().getPickedVertexState().pick(v1, true);
                j.setText(myText);

                // wire it to some edges
                if (v_prev != null) {
                    Integer edge = edgeCount;

                    // let's connect to a random vertex, too!

                    int rand = (int) (Math.random() * (edgeCount-1)); // because there is a 0 node
                    while (v1.equals(rand)) {
                        System.out.println("avoided connecting to myself");
                        rand = (int) (Math.random() * (edgeCount-1)); // because there is a 0 node
                    }

                    edgeCount++;
                    g.addEdge(edge, rand, v1);  //add an edge called var1, between the nodes var2 and var3
                    vv.getRenderContext().getPickedEdgeState().pick(edge, true);
                    System.out.println("Adding edge " + edge + " between " + rand + " & " + v1 + "()");
                }

                v_prev = v1;
                layout.initialize();

                Relaxer relaxer = new VisRunner((IterativeContext) layout);
                relaxer.stop();
                relaxer.prerelax();

                vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
                vv.repaint();

            } else {
                done = true;
            }

        } catch (Exception e) {
            System.out.println(e);
        }
    }
Was it helpful?

Solution

If you have specific locations that you want the vertices to be placed at, use StaticLayout. FRLayout is a force directed Layout that will reposition the vertices according to the topology of the graph.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top