如何在GraphStream中布置节点一次? [英] How to layout nodes once in GraphStream?

查看:133
本文介绍了如何在GraphStream中布置节点一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用

It is possible to activate layout process in GraphStream with Viewer#enableAutoLayout(). Unfortunately, this process will tamper each user interaction like node dragging.

是否可以进行一次自动布局然后停止?

Is is possible to do automatic layout once and then stop?

我试图将自动版式设置一秒钟然后等待,但这没有用.

I have tried to turn autolayout for a second and wait, but this didn't work.

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





    }
}

推荐答案

一种(当然不是最好的)解决方案是计算run方法内部的布局.

One (but surely not the best) solution is to compute the layout inside your run method.

首先创建布局类的实例,然后将其插入到图形中在修改图形之前.

First create an instance of the layout class and plug it to the graph before modifying the graph.

然后计算布局,直到出现某些停止条件为止.就计算而言,固定数量的迭代是一个安全的选择,但可能不会给您带来良好的效果.相反,您可以迭代直到布局自行稳定(这可能永远不会发生,具体取决于您的图形...)

Then compute the layout until some stop condition. A fix amount of iterations is a safe choice in terms of computation but may not give you good results. Instead you can iterate up until the layout stabilizes by itself (which may never happen depending on your graph...)

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

这篇关于如何在GraphStream中布置节点一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆