Posted on 2008-04-29 09:41
zDevil(Dengues Studio) 閱讀(1276)
評論(0) 編輯 收藏
一般來說我們很難計算兩個節點之間的顯示名稱的位置,而且還要包括名稱字符串的大小。這個是在Dengues里面實現的效果:
圖一:

圖二:

你可以看Dengues源代碼:
org.dengues.designer.ui.process.models.CompConnectionLabel
1 /**
2 * Qiang.Zhang.Adolf@gmail.com Comment method "changeLocation".
3 */
4 public void changeLocation() {
5 Point point = new Point();
6
7 int x = connection.getSource().getLocation().x;
8 int x2 = connection.getTarget().getLocation().x;
9
10 int width = x2 > x ? connection.getSource().getSize().width : connection.getTarget().getSize().width;
11 int abs = Math.abs(x2 - x) / 2 - width / 2;
12 int px = x2 > x ? x + abs + width : x2 + abs + width;
13 //這個作用是計算字符串getCompName()的大小。
14 GC gc = new GC(Display.getDefault());
15 org.eclipse.swt.graphics.Point stringExtent = gc.stringExtent(getCompName());
16 gc.dispose();
17 px = px - stringExtent.x / 2;
18 point.x = px;
19
20 int y = connection.getSource().getLocation().y;
21 int y2 = connection.getTarget().getLocation().y;
22
23 int height = y2 > y ? connection.getSource().getSize().height : connection.getTarget().getSize().height;
24 abs = Math.abs(y2 - y) / 2 - height / 2;
25 int py = y2 > y ? y + abs + height : y2 + abs + height;
26 py = py - stringExtent.y;
27 point.y = py;
28 setLocation(point);
29 }
這樣的好就可以計算中間的位置。
Dengues論壇(http://groups.google.com/group/dengues/),一個很好的Eclipse開發者樂園.