我正在尝试实现绘图秋千内的教程图 http://graphstream-project.org/doc/Tutorials/Graph-Visualisation_1.1/#integrating-the-viewer-in-your-gui,但失败了。
代码如下:
包测试.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);
public MyFrame() {
setLayout(new BorderLayout());
add(view, 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");
}
});
}
}
它画出了这个:
如果拖动节点,则变为:
如何获得结果,接近graph.display()
?
这是一个副作用,因为节点没有x
and y
默认坐标。
为了防止这种情况,您应该:
- 激活
autolayout
在你的viewer
对象与viewer.enableAutoLayout();
- 或者自己指定一些
x
and y
每个节点的属性。
带自动布局
// ...
public MyFrame() {
setLayout(new BorderLayout());
add(view, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Activate autolayout here :
viewer.enableAutoLayout();
}
// ...
具有节点属性
// In main() ...
Node a = graph.addNode("A");
a.addAttribute("xy", 0, 0);
Node b = graph.addNode("B");
b.addAttribute("xy", 10, 0);
Node c = graph.addNode("C");
c.addAttribute("xy", 10, 10);
// ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)