2

我正在使用我正在编写的用于创建抽象语法树可视化的小型编译器来玩 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();
    }

运行程序显示了自动定位的魔力。但是,当一个节点被鼠标拖动时,其他节点保持静止。如果用鼠标拖动节点,是否有一种简单的方法可以让其他节点做出反应(也被拉动)?

这必须得到支持,但我似乎找不到任何示例或 API 参考?

4

1 回答 1

3

我不知道你的函数背后的代码,但通常它是默认查看器自动生成的。您可以尝试使用以下命令强制激活自动布局:

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);
于 2020-08-31T01:58:53.467 回答