我想创建代表人们之间业务交互的图表,我使用 Java 的 GraphStream 库。我写了这样的代码:
EventQueue.invokeLater(new Runnable() {
public void run() {
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
Graph graph = new SingleGraph("Tutorial 1");
String styleSheet = "node {\n"
+ "fill-color: black;\n"
+ "}\n"
+ "node.marked {\n"
+ "fill-color: red;"
+ "size: 25px;\n"
+ "}";
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.stylesheet", styleSheet);
graph.getNode("A").addAttribute("ui.label", "A Node");
graph.getNode("A").addAttribute("ui.class", "marked");
graph.getNode("B").addAttribute("ui.clicked", "marked");
graph.setAutoCreate(true);
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_SWING_THREAD);
View view = viewer.addDefaultView(false);/**/ // false indicates "no JFrame".
View defaultView = viewer.getDefaultView();
JFrame frame = new JFrame("Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.add(new JScrollPane(defaultView, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
frame.setVisible(true);
}
});
当我运行这个程序时,会显示一个窗口,并且我会在同一点获得三角形的所有顶点。然后,当我拖动三角形的顶点时,我得到一个 3D 三角形。有人可以解释为什么我在拖动三角形的顶点后会得到一个 3D 三角形吗?如何获得二维图形?
Viewer viewer1 = graph.display();
View view1 = viewer1.addDefaultView(false);
上面的代码工作正常,但我不明白为什么它在第一个示例中不起作用感谢阅读这篇文章。最好的祝愿。