2012-07-14 82 views
0

我想在連接的開始或結束位置添加標籤。但是在這裏我沒有找到除ManhattanMidpointLocator之外的定位器。那我該怎麼做?如何將標籤放置在連接處?
請找到下面我的代碼,如何在draw2d的特定位置添加圖形?

draw2d.LabelConnection = function() { 
    draw2d.Connection.call(this); 
    this.sourcePort = null; 
    this.targetPort = null; 
    this.lineSegments = []; 
    this.setColor(new draw2d.Color(0, 255, 0)); 
    this.setLineWidth(2); 

    var label = new draw2d.Label("Message"); 
    label.setBackgroundColor(new draw2d.Color(230, 230, 250)); 
    label.setBorder(new draw2d.LineBorder(1)); 
    this.addFigure(label, new draw2d.Locator()); 
}; 

draw2d.LabelConnection.prototype = new draw2d.Connection(); 
draw2d.LabelConnection.prototype.type = "draw2d.LabelConnection"; 

上面的代碼顯示在(0,0)位置的標籤。 plz幫助我。

回答

0

用途:

Connection.getStartPoint()到確定連接的起始點,而不是 以檢索連接的所有段。

問候

+0

我使用Connection.getStartPoint()方法,它給了我錯誤locator.relocate()不是一個函數。請幫助我。我試圖實施relocate()方法,但它似乎不工作。 – 2012-07-19 06:33:36

0

我不確定你的意思是「任何位置」,但是如果你實現自己的定位器,它非常簡單。

查看ManhattanMidpointLocator的代碼。在重定位功能中,您可以瞭解畫布上連接的所有信息。從中你只需要爲標籤的位置進行計算。

+0

好的,謝謝。我會盡力得到它。 – 2012-07-16 05:47:32

+0

我想在起始位置找到這個標籤,而不是在中間位置。那我該怎麼做呢?我用Locator(),但它找到它(0,0)的位置。 PLZ的幫助? – 2012-07-16 06:35:55

0

現在用Graphiti工作到位Draw2D的,但你的定位器,代碼應該如下(未測試):

draw2d.StartConnectionLocator=function(/*:draw2d.Connection*/ connection) 
{ 
    draw2d.ConnectionLocator.call(this,connection); 
}; 
draw2d.StartConnectionLocator.prototype.relocate=function(/*:draw2d.Figure*/ target) 
{ 
    var conn = this.getConnection(); 

    var points = conn.getPoints(); 
    var index = Math.floor((points.getSize() -2)/2); 
    if (points.getSize() <= index+1) 
     return; 

    var startPoint = points.get(0); 
    var myPosition = new draw2d.Point(); 
    myPosition.x = startPoint.x +5; 
    myPosition.y = startPoint.y +5; 

    target.setPosition(myPosition.x,myPosition.y); 
}; 
+0

由於Andreas建議您可以使用connection.getStartPoint()代替var startPoint = points.get(0); – Zerzio 2012-07-16 15:05:23

+0

我使用connection.getStartPoint(),但它給了我錯誤locator.relocate()不是一個函數。所以請幫助我,我應該如何調用relocate方法。找到我的代碼下面draw2d.ContextmenuConnection =函數(src,目標){ \t draw2d.Connection.call(this); \t this.sourceAnchor.setOwner(src); \t this.targetAnchor.setOwner(target); \t var label = new draw2d.Label(「Message」); \t this.addFigure(label,this.getStartPoint()); \t var arrowConnector = new draw2d.ArrowConnectionDecorator(); \t arrowConnector.setBackgroundColor(new draw2d.Color(0,255,0)); }; – 2012-07-19 07:06:45

相關問題