关键字:Draw2d,连线,ChopboxAnchor EllipseAnchor
连线一提到的连线其实并没有连接到图形的边框上,这个问题主要取决于锚点(Anchor)。
如果将代码中的椭圆改为正圆就很容易看出来了。
public class HelloWorld2 { 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 Ellipse(); // IFigure figure2 = new Triangle(); // -------------------------------------------------------- // add connection PolylineConnection connection = new PolylineConnection(); connection.setSourceAnchor(new ChopboxAnchor(figure1)); connection.setTargetAnchor(new ChopboxAnchor(figure2)); // add connection // -------------------------------------------------------- rootFigure.add(figure1,new Rectangle(10,10,60,30)); rootFigure.add(figure2,new Rectangle(80,90,60,60)); rootFigure.add(connection); LightweightSystem lws = new LightweightSystem(shell); lws.setContents(rootFigure); } }
这里的关键的就是锚点,采用了ChopboxAnchor 的方法,ChopboxAnchor 的返回的是和矩形边框的交接点,而不是和所画图形的交点。解决这个问题就要采用EllipseAnchor定锚点的方法了。
将代码的这个改一下就行了。
connection.setTargetAnchor(new EllipseAnchor(figure2));