在Draw2D中向PolylineConnection添加标签

时间:2018-08-03 07:59:08

标签: eclipse swt eclipse-gef draw2d

我正在尝试在Draw2d中向PolylineConnection添加标签。我以java2s中的示例为基础。问题是,即使我可以通过使用PathFigure对象(扩展了PolylineConnection)的paintFigure方法上的graphics.drawText()创建文本,在大多数情况下也会删除标签,如以下捕获所示:

see 'finish' label cut

both labels are cut

在我看来,图形的边界似乎将文本的一部分留在了绘制区域之外,因为它确实确实在具有较大边界的对角箭头中正确绘制了。

我试图在构造函数和绘画方法中显式设置对象的边界,但似乎PolylineConnection忽略了它们。关于如何解决此问题或是否有另一种方式获得这种标签的想法?

1 个答案:

答案 0 :(得分:3)

请使用下图作为您的连接图。

import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MidpointLocator;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;

public class LabelConnectionFigure extends PolylineConnection {
    protected Label label;

    public LabelConnectionFigure() {
        setTargetDecoration(new PolygonDecoration());
        MidpointLocator labelLocator = new MidpointLocator(this, 0);
        label = new Label("1");
        label.setOpaque(true);
        add(label, labelLocator);
    }

    public void setLabelText(String labelText) {
        label.setText(labelText);
    }

    public String getLabelText() {
        return label.getText();
    }
}