本文介绍了实际如何用GraphStream在秋千内绘制图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实施挥杆内的教程图,但失败了.
I am trying to implement drawing of tutorial graph inside swing, but failing.
代码如下:
包tests.graphstream;
package tests.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");
}
});
}
}
它绘制了这个:
如果拖动节点,它将变为:
If drag nodes, it turns to:
如何获得结果,接近graph.display()
?
推荐答案
这是一个副作用,因为默认情况下节点不具有x
和y
坐标.
This is a side effect due to the fact that nodes do not have x
and y
coordinates by default.
为防止这种情况,您应该:
To prevent this you should whether :
- 使用
viewer.enableAutoLayout();
激活 - 或为每个节点自己指定一些
x
和y
属性.
viewer
对象上的autolayout
- activate the
autolayout
on yourviewer
object withviewer.enableAutoLayout();
- or specify by yourself some
x
andy
attributes for each node.
// ...
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);
// ...
这篇关于实际如何用GraphStream在秋千内绘制图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!