GEF中的锚点Anchor

GEF中的锚点Anchor

还是昨晚做的小例子。实现的是给图形添加连线、删除连线以及连线的重新定向。

不过,当我试着把图形换成椭圆时,却发现连线不是太好看:

GEF中的锚点Anchor

连线与图形之间竟然会出现空白区域。

原来问题出在锚点(Anchor)上。连线是连接的两个锚点,而不是图形。参考了GEF自带的Shapes例子,在ShapeEditPart类里看见如下变量声明:

private ConnectionAnchor anchor;

而且接下来有一个根据模型类型创建 不同类型锚点的方法:

protected ConnectionAnchor getConnectionAnchor() {
    if (anchor == null) {
        if (getModel() instanceof EllipticalShape)
            anchor = new EllipseAnchor(getFigure());
        else if (getModel() instanceof RectangularShape)
            anchor = new ChopboxAnchor(getFigure());
        else
            // if Shapes gets extended the conditions above must be updated
            throw new IllegalArgumentException("unexpected model");
    }
    return anchor;
}

这样,获得锚点的各种方法就可以调用上面的方法:

public ConnectionAnchor getSourceConnectionAnchor(
        ConnectionEditPart connection) {
    return getConnectionAnchor();
}
public ConnectionAnchor getSourceConnectionAnchor(Request request) {
    return getConnectionAnchor();
}
public ConnectionAnchor getTargetConnectionAnchor(
        ConnectionEditPart connection) {
    return getConnectionAnchor();
}
public ConnectionAnchor getTargetConnectionAnchor(Request request) {
    return getConnectionAnchor();
}

从上边的代码使用了两种锚点:EllipseAnchor与ChopboxAnchor。ChopboxAnchor就是我做的例子中原来使用的;EllipseAnchor嘛,看名字应该是为Ellipse量身定制的吧!

参考上边的代码修改我的例子,果然成功了:

GEF中的锚点Anchor


转自:http://www.codeweblog.com/gef%E4%B8%AD%E7%9A%84%E9%94%9A%E7%82%B9anchor/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值