代码之家  ›  专栏  ›  技术社区  ›  Asher

实现椭圆之间的多段线连接

  •  1
  • Asher  · 技术社区  · 10 年前

    在创建椭圆并将其位置存储在画布上之后,我正在尝试在画布上创建两个椭圆之间的连接。需要使用这些存储的位置来创建连接。我编写的代码适用于矩形图形,但不适用于椭圆。我似乎看不出这两种情况之间的区别。有人能帮忙吗?谢谢。*我添加了简短的可测试代码来说明我的问题。要查看它在Rectangle中的工作情况,请取消注释行“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 回复  |  直到 10 年前
        1
  •  2
  •   Baz    10 年前

    您的问题是由确定源和目标的方法 IFigure s是有缺陷的。

    存储查找的左上角。这对 RectangleFigure ,因为该矩形实际上包含左上角。这个 Ellipse 然而,没有(这就是为什么 PolylineConnection 是他们的父母)。

    最好用图片说明:

    enter image description here

    如果您存储图形的中心,您将得到以下结果:

    enter image description here