graphstream使用“始终打开”自动布局

wwodge7n  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(286)

我在用一个小编译器玩graphstream,我正在编写一个抽象语法树可视化程序,如下所示:

// ASTNode is the root to to the AST tree. Given that node, this just displays
     // the AST on-screen.
     public static void visualize(ASTNode ast) throws IOException, URISyntaxException {
        Path file = Path.of(VisualizeAbstractSyntaxTree.class.getResource("graph.css").toURI());
        String css = Files.readString(file);
        System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
        Graph graph = new SingleGraph("AST");
        graph.addAttribute("ui.stylesheet", css);
        construct(ast, "0", graph);  // construct the tree from the AST root node.
        Viewer viewer = graph.display();
    }

运行程序显示自动定位的魔力。但是,当鼠标拖动一个节点时,其他节点保持静止。如果用鼠标拖动一个节点,有没有一种简单的方法让其他节点做出React(也被拖动)?
这必须得到支持,但我似乎找不到任何例子或api引用这个?

w80xi6nr

w80xi6nr1#

我不知道你的函数背后的代码,但通常它是自动与默认查看器。您可以尝试通过以下方式强制激活自动布局:

viewer.enableAutoLayout();

你可以在网站上找到一些文档。
如果自动布局工作,但只是突然停止,它可能是布局算法的参数,不适合你。布局算法被编写为在到达稳定点时停止,但您可以更改这一点。
您只需定义您喜欢的布局算法的新示例并使用它:

SpringBox l = new SpringBox();

然后可以定义参数,比如力或稳定点。约定值0表示控制布局的进程不会停止布局(因此不会考虑稳定极限)。换言之,布局将无休止地计算

l.setStabilizationLimit(0);

但是请记住,如果您想使用布局算法的示例,您将在显示之前创建查看器。这意味着要构建自己的ui。下面是一个简单的例子,您可以在官方github上找到更多信息:

SpringBox l = new SpringBox(); // The layout algorithm
l.setStabilizationLimit(0);

Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
viewer.enableAutoLayout(l); // Add the layout algorithm to the viewer

// Build your UI
add(viewer.addDefaultView(false), BorderLayout.CENTER); // Your class should extends JFrame
setSize(800, 600);
setVisible(true);

相关问题