|
plot() 调用了两个重要内部函数来帮助绘制图形中的点:populateNodesAndEdges() 和 drawDotsAndConnections()。在您发现这两个函数到底完成什么功能之前,让我们来看一下 DirectedGraph。
DirectedGraph 是什么?为了使用 Draw2D 进行绘图,事实上您必须先创建一个图形,定义将要绘制的点和线。一旦创建好这个图形,就可以使用它实际在画布上进行绘图。您可以将 DirectedGraph 形象化为拥有有限数量的 Node 的一个 2-D 图形,在该图形中,每个 Node 都位于一些 Point 上,相邻的 Node 是通过 Edges 连接在一起的。
您可以通过以下代码行来了解创建 DirectedGraph 的关键所在。首先,创建一个 Node 列表和一个 Edges 列表。然后,创建一个新的 DirectedGraph,并通过刚才创建的 NodeList 和 EdgeList 设置其成员(Nodes 和 Edges)。现在,使用 GraphVisitor 来访问这个 DirectedGraph。为了简便起见,包 org.eclipse.draw2d.internal.graph 中有许多 GraphVisitor 实现,这些 GraphVisitor 有一些用来访问图形的特定算法。
因此,创建 DirectedGraph 的示例代码类似于下面这样: 清单 5. 示例 DirectedGraph
//This is a sample, you will need to add actual Node(s) to this NodeList.
NodeList nodes = new NodeList(); //create a list of nodes.
//This is a sample, you will need to add actual Edge(s) to this EdgeList.
EdgeList edges = new EdgeList(); //create a list of edges.
DirectedGraph graph = new DirectedGraph();
graph.nodes = nodes;
graph.edges = edges;
new BreakCycles().visit(graph);//ask BreakCycles to visit the graph.
//now our "graph" is ready to be used.
| 现在,已经知道 DirectedGraph 包含许多 Node,其中,每个 Node 都可能包含一些数据,并且还存储了这些数据的 X 坐标和 Y 坐标,以及一个 Edges 的列表,每个 Edge 都知道在自己的两端分别有一个 Node,您可以通过以下技术,使用这些信息来绘图,其中涉及两个部分: XMLns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 部分 A —— 通过以下步骤填充 Node 和 Edge:
- 创建一个 NodeList,在该列表中,集合中的每个元素都有一个 Node,集合 {10,20,30,40} 需要 4 个 Node。
- 找出每个元素的 X 坐标和 Y 坐标,将它们存储在 node.x 和 node.y 成员变量中。
- 创建一个 EdgeList,在该列表中,有 n -1 个 Edge,其中,n 是集合中的元素的数量。例如,集合 {10,20,30,40} 需要三个 Edge。
- 将 Node 与每个 Edge 的左右端相关联,并相应地设置 edge.start 和 edge.end 成员变量。
部分 B —— 通过以下步骤绘制表示 Node 和 Edge 的图形:
- 绘制一个 Dot 图来表示每个 Node。
- 绘制一个 PolylineConnection 图形来表示每个 Edge。
- 界定每个 PolylineConnection 图形,以固定 Dot 图的左右端。
上一页 [1] [2] [3] [4] [5] [6] [7] 下一页
 【责编:Youping】 |