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

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

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

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

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

w80xi6nr

w80xi6nr1#

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

  1. viewer.enableAutoLayout();

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

  1. SpringBox l = new SpringBox();

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

  1. l.setStabilizationLimit(0);

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

  1. SpringBox l = new SpringBox(); // The layout algorithm
  2. l.setStabilizationLimit(0);
  3. Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
  4. viewer.enableAutoLayout(l); // Add the layout algorithm to the viewer
  5. // Build your UI
  6. add(viewer.addDefaultView(false), BorderLayout.CENTER); // Your class should extends JFrame
  7. setSize(800, 600);
  8. setVisible(true);
展开查看全部

相关问题