如何在GraphStream中布置节点一次?

时间:2015-01-31 10:28:16

标签: java swing graphstream

可以使用Viewer#enableAutoLayout()GraphStream中激活布局流程。不幸的是,这个过程会篡改每个用户交互,比如节点拖动。

可以自动布局一次然后停止吗?

我试图将autolayout转一秒钟然后等待,但这没有用。

package tests.graphstream;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.swingViewer.View;
import org.graphstream.ui.swingViewer.Viewer;

public class Tutorial1_01 {

    private static Graph graph = new SingleGraph("Tutorial 1");

    public static class MyFrame extends JFrame {

        private static final long serialVersionUID = 8394236698316485656L;

        //private Graph graph = new MultiGraph("embedded");
        private Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
        //private Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_SWING_THREAD);
        private View view = viewer.addDefaultView(false);
        private View defaultView  = viewer.getDefaultView();

        public MyFrame() {

             setLayout(new BorderLayout());

             //add( new JScrollPane(defaultView), BorderLayout.CENTER);

             add(defaultView, BorderLayout.CENTER);

             setDefaultCloseOperation(EXIT_ON_CLOSE);

        }

    }

    public static void main(String args[]) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {


                MyFrame frame = new MyFrame();


                frame.setSize(320, 240);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                graph.addNode("A");
                graph.addNode("B");
                graph.addNode("C");
                graph.addEdge("AB", "A", "B");
                graph.addEdge("BC", "B", "C");
                graph.addEdge("CA", "C", "A");

                graph.addAttribute("ui.quality");
                graph.addAttribute("ui.antialias");


                frame.viewer.enableAutoLayout();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
                frame.viewer.disableAutoLayout();

                //frame.view.getCamera().resetView();
            }
        });





    }
}

1 个答案:

答案 0 :(得分:1)

一个(但肯定不是最好的)解决方案是计算run方法中的布局。

首先创建一个布局类的实例,并在>修改图形之前将其插入图形

然后计算布局直到某些停止条件。在计算方面,一定数量的迭代是一个安全的选择,但可能无法给您带来好的结果。相反,您可以迭代直到布局自行稳定(根据您的图形可能永远不会发生......)

public void run() {
    MyFrame frame = new MyFrame();

    frame.setSize(320, 240);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    // a layout algorithm instance plugged to the graph
    Layout layout = new SpringBox(false);
    graph.addSink(layout);
    layout.addAttributeSink(graph);

    graph.addNode("A");
    graph.addNode("B");
    graph.addNode("C");
    graph.addEdge("AB", "A", "B");
    graph.addEdge("BC", "B", "C");
    graph.addEdge("CA", "C", "A");

    graph.addAttribute("ui.quality");
    graph.addAttribute("ui.antialias");

    // iterate the compute() method a number of times
    while(layout.getStabilization() < 0.9){
        layout.compute();
    }   
}