在Ellipses之间实现折线连接

时间:2014-07-22 08:50:51

标签: java user-interface swt ellipse draw2d

我试图在画布上的两个椭圆之间创建连接,之后创建了椭圆并在画布上存储它们的位置。需要使用这些存储的位置来创建连接。我编写的代码适用于RectangleFigures,但不适用于Ellipses。我似乎无法看到两种情况之间的区别。有人可以帮忙吗?谢谢。*我已经添加了简短的可测试代码来说明我的问题。要查看它使用矩形,请取消注释说明UNCOMMENT THIS

    import java.util.ArrayList;
    import org.eclipse.draw2d.ColorConstants;
    import org.eclipse.draw2d.ChopboxAnchor;
    import org.eclipse.draw2d.Ellipse;
    import org.eclipse.draw2d.Figure;
    import org.eclipse.draw2d.IFigure;
    import org.eclipse.draw2d.LightweightSystem;
    import org.eclipse.draw2d.PolygonDecoration;
    import org.eclipse.draw2d.PolylineConnection;
    import org.eclipse.draw2d.RectangleFigure;
    import org.eclipse.draw2d.geometry.Point;
    import org.eclipse.draw2d.geometry.Rectangle;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.dnd.DND;
    import org.eclipse.swt.dnd.DragSource;
    import org.eclipse.swt.dnd.DragSourceEvent;
    import org.eclipse.swt.dnd.DragSourceListener;
    import org.eclipse.swt.dnd.DropTarget;
    import org.eclipse.swt.dnd.DropTargetEvent;
    import org.eclipse.swt.dnd.DropTargetListener;
    import org.eclipse.swt.dnd.TextTransfer;
    import org.eclipse.swt.dnd.Transfer;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Layout;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Group;
    import org.eclipse.swt.widgets.Canvas;
    import org.eclipse.swt.widgets.Label;



    public class EllipseProblem
   { 
    private Shell        shell;
    private Display      display;
    private final Label  lblUnicorn;
    private final Canvas canvas;
    final IFigure panel;
    public static ArrayList canvasElements= new ArrayList(); 

    public static void main(String args[])
    {
        new EllipseProblem();
    }

    public EllipseProblem()
    {
        display = new Display();
        shell = new Shell(display);
        shell.setText("SWT Application");
        shell.setLayout(new GridLayout(2, false));

        Group grpPalette = new Group(shell, SWT.NONE);
        grpPalette.setText("Palette");
        grpPalette.setLayout(new GridLayout());
        grpPalette.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, true));

        lblUnicorn = new Label(grpPalette, SWT.BORDER | SWT.HORIZONTAL | SWT.CENTER);
        lblUnicorn.setText("UNICORN");
        lblUnicorn.setAlignment(SWT.CENTER);

        final Group grpCanvas = new Group(shell, SWT.NONE);
        grpCanvas.setText("Canvas");
        grpCanvas.setLayout(new GridLayout());
        grpCanvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        canvas = new Canvas(grpCanvas, SWT.NONE);
        canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Button btnCreate = new Button(grpPalette, SWT.CENTER);
        GridData gd_btnCreate = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_btnCreate.widthHint = 87;
        gd_btnCreate.heightHint = 33;
        btnCreate.setLayoutData(gd_btnCreate);
        btnCreate.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                drawConnection(); 
            }
        });
        btnCreate.setText("Make Connection");

        LightweightSystem lws = new LightweightSystem(canvas); 
        panel = new Figure(); 
        lws.setContents(panel);

        DragSource dragSource1 = new DragSource(lblUnicorn, DND.DROP_COPY);
        Transfer[] transfers1 = new Transfer[] { TextTransfer.getInstance() };
        dragSource1.setTransfer(transfers1);
        dragSource1.addDragListener(new DragSourceListener()
        {
            public void dragStart(DragSourceEvent event)
            {
                if (lblUnicorn.getText().length() == 0)
                {
                    event.doit = false;
                }
            }

            public void dragSetData(DragSourceEvent event)
            {
                if (TextTransfer.getInstance().isSupportedType(event.dataType))
                {
                    event.data = lblUnicorn.getText();
                }
            }

            public void dragFinished(DragSourceEvent event)
            {
            }
        });

        Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
        DropTarget dropTarget = new DropTarget(canvas, DND.DROP_COPY | DND.DROP_DEFAULT);
        dropTarget.setTransfer(types);

        dropTarget.addDropListener(new DropTargetListener()
        {
            public void dragEnter(DropTargetEvent event)
            {
                if (event.detail == DND.DROP_DEFAULT)
                {
                    if ((event.operations & DND.DROP_COPY) != 0)
                    {
                        event.detail = DND.DROP_COPY;
                    }
                    else
                    {
                        event.detail = DND.DROP_NONE;
                    }
                }
            }

            public void dragLeave(DropTargetEvent event)
            {
            }

            public void dragOperationChanged(DropTargetEvent event)
            {
            }

            public void dragOver(DropTargetEvent event)
            {
            }

            public void drop(DropTargetEvent event)
            {
            }

            public void dropAccept(final DropTargetEvent event)
            {

                if (TextTransfer.getInstance().isSupportedType(event.currentDataType))
                {
                    String d = (String) TextTransfer.getInstance().nativeToJava(event.currentDataType);

                    final String finald= d; 
                    org.eclipse.swt.graphics.Point droppoint = canvas.toControl(event.x, event.y);
                    canvasElements.add(droppoint);
                    Rectangle rect=new Rectangle(droppoint.x, droppoint.y, 40, 20);
                    Rectangle rect2=new Rectangle(droppoint.x+20, droppoint.y, 100, 25);

                    Ellipse node= new Ellipse();
                    //RectangleFigure node= new RectangleFigure(); UNCOMMENT THIS

                    node.setBounds(rect); 
                    System.out.println(node.getBounds());
                    node.setLocation(new Point(droppoint.x, droppoint.y));

                    node.setBackgroundColor(ColorConstants.red);
                    panel.add(node);
                    org.eclipse.draw2d.Label droppedName= 
                            new org.eclipse.draw2d.Label(finald);
                    droppedName.setLocation(new Point(droppoint.x, droppoint.y)); //draw2d. point
                    droppedName.setBounds(rect2);

                    panel.add(node);
                    panel.add(droppedName);

                    canvas.redraw();
                }
            }
        });

        shell.pack();
        shell.setSize(400, 300);
        shell.open();

        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
    }

    protected void drawConnection()
    {

        org.eclipse.swt.graphics.Point swt_origin= (org.eclipse.swt.graphics.Point) canvasElements.get(0);
        org.eclipse.draw2d.geometry.Point origin_point= new org.eclipse.draw2d.geometry.Point(swt_origin.x, swt_origin.y); 
        org.eclipse.swt.graphics.Point swt_destination= (org.eclipse.swt.graphics.Point) canvasElements.get(1);
        org.eclipse.draw2d.geometry.Point destination_point= new org.eclipse.draw2d.geometry.Point(swt_destination.x, swt_destination.y); 

        IFigure origin = panel.findFigureAt(origin_point);
        IFigure destination = panel.findFigureAt(destination_point);

        PolylineConnection conn = new PolylineConnection();

        conn.setSourceAnchor(new ChopboxAnchor(origin));
        conn.setTargetAnchor(new ChopboxAnchor(destination));
        conn.setTargetDecoration(new PolygonDecoration());
        panel.add(conn);


    }

}

1 个答案:

答案 0 :(得分:2)

您的问题是由于您确定来源和目标IFigure的方法存在缺陷这一事实造成的。

存储左上角以进行查找。这适用于RectangleFigure,因为该矩形实际上包含左上角。但是Ellipse没有(这就是为什么PolylineConnection的来源和目标是他们的父母)。

最佳图片说明:

enter image description here

如果你改为存储图形的中心,你最终会得到这个:

enter image description here