FixedConnectionAnchor

public class HelloWorld1 {
	public static void main(String args[]) {
		Shell shell = new Shell();
		shell.setText("Draw2d Hello World");
		shell.setSize(400, 400);
		shell.open();
		
		// create content 4 shell.
		createContent4Shell(shell);
		
		while (!shell.isDisposed ()) {
			if (!Display.getDefault().readAndDispatch ())
				Display.getDefault().sleep ();
		}
	}

	private static void createContent4Shell(Shell shell) {
		Panel rootFigure = new Panel();
		rootFigure.setLayoutManager(new XYLayout());
		
		IFigure figure1 = new Ellipse();
		IFigure figure2 = new org.eclipse.draw2d.RectangleFigure();
//		IFigure figure2 = new Triangle();
		
		// --------------------------------------------------------
		// add connection
		PolylineConnection connection = new PolylineConnection();
		connection.setSourceAnchor(new ChopboxAnchor(figure1));
		
		FixedConnectionAnchor anchor = new FixedConnectionAnchor(figure2);
//		anchor.leftToRight = false;
//		anchor.topDown = false;
//		anchor.offsetH = 10;
		anchor.offsetV = 10;
		connection.setTargetAnchor(anchor);
		// add connection
		// --------------------------------------------------------

		rootFigure.add(figure1,new Rectangle(10,10,60,30));
		rootFigure.add(figure2,new Rectangle(180,10,60,60));
		rootFigure.add(connection);
		
		LightweightSystem lws = new LightweightSystem(shell);
		lws.setContents(rootFigure);
	}
}

class FixedConnectionAnchor extends AbstractConnectionAnchor {

	public boolean leftToRight = true;
	public int offsetH;
	public int offsetV;
	public boolean topDown = true;

	public FixedConnectionAnchor(IFigure owner) {
		super(owner);
	}

	/**
	 * @see org.eclipse.draw2d.AbstractConnectionAnchor#ancestorMoved(IFigure)
	 */
	public void ancestorMoved(IFigure figure) {
		if (figure instanceof ScalableFigure)
			return;
		super.ancestorMoved(figure);
	}

	public Point getLocation(Point reference) {
		Rectangle r = getOwner().getBounds();
		int x, y;
		if (topDown)
			y = r.y + offsetV;
		else
			y = r.bottom() - 1 - offsetV;

		if (leftToRight)
			x = r.x + offsetH;
		else
			x = r.right() - 1 - offsetH;

		Point p = new PrecisionPoint(x, y);
		getOwner().translateToAbsolute(p);
		return p;
	}

	public Point getReferencePoint() {
		return getLocation(null);
	}

	/**
	 * @param offsetH
	 *            The offsetH to set.
	 */
	public void setOffsetH(int offsetH) {
		this.offsetH = offsetH;
		fireAnchorMoved();
	}

	/**
	 * @param offsetV
	 *            The offsetV to set.
	 */
	public void setOffsetV(int offsetV) {
		this.offsetV = offsetV;
		fireAnchorMoved();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object o) {
		if (o instanceof FixedConnectionAnchor) {
			FixedConnectionAnchor fa = (FixedConnectionAnchor) o;

			if (fa.leftToRight == this.leftToRight
					&& fa.topDown == this.topDown && fa.offsetH == this.offsetH
					&& fa.offsetV == this.offsetV
					&& fa.getOwner() == this.getOwner()) {
				return true;
			}
		}

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return ((this.leftToRight ? 31 : 0) + (this.topDown ? 37 : 0)
				+ this.offsetH * 43 + this.offsetV * 47)
				^ this.getOwner().hashCode();
	}

}

 

FixedConnectionAnchor 可以指定确定的点为Anchor。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值