简体   繁体   English

如何在draw2d中的特定位置添加图形?

[英]How to add figure at particular location in draw2d?

I want to add label at start or end position of connection. 我想在连接的开始或结束位置添加标签。 But here I did not found locator except ManhattanMidpointLocator. 但是在这里我找不到除ManhattanMidpointLocator之外的定位器。 So how can I do this? 那我该怎么做呢? How can I place label at where on connection? 如何在连接的位置放置标签?
Please find my code as below, 请找到我的代码如下

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";

The above code shows label at (0,0) position. 上面的代码显示标签在(0,0)位置。 plz help me out. 请帮我。

I'm not sure about what you mean by "any location" but if you implement your own locator it's pretty straightforward. 我不确定“任何位置”是什么意思,但是如果您实现自己的定位器,则非常简单。

Look at the code of the ManhattanMidpointLocator. 查看ManhattanMidpointLocator的代码。 In the relocate function you know everything about the connection on the canvas. 在重定位功能中,您了解有关画布上连接的所有信息。 From that you just invent a calculation for the position of the label. 由此,您只需发明一个计算标签位置的方法。

Now working with Graphiti in place of Draw2D but the code for your locator should look as follow (didn't tested) : 现在使用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);
};

Use: 采用:

Connection.getStartPoint() to determine the start poit of the connection instead to retrieve all segments of the connection. Connection.getStartPoint()确定连接的起始点,而不是检索连接的所有段。

Greetings 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM