0

我创建了一个无向图,但是当我设置坐标时,它们似乎从未正确匹配。

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

问题是它可以生成 300 的说和 X 值和另一个 X 值为 350 的值,并且不知何故 350 位于另一个的左侧,清楚地表明它没有将它们放置在正确的位置。

我还在下面创建了一个示例,它从 0 开始运行,然后在 X 和 Y 方向上再创建 50 个顶点,您可以清楚地看到它们的间距不相等。(这是使用 FRLayout2)。

在此处输入图像描述

而本示例使用相同的方法使用 FRLayout(我实际使用的布局),您可以从中看到前 2 个似乎是正确的,但从第 3 个开始就完全出错了。

在此处输入图像描述

这是我的代码。

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

以及添加顶点的方法

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

1 回答 1

0

如果您有希望放置顶点的​​特定位置,请使用 StaticLayout。FRLayout 是一个力导向的布局,它将根据图形的拓扑重新定位顶点。

于 2013-11-03T16:09:10.007 回答