Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 实现椭圆之间的多段线连接_Java_User Interface_Swt_Ellipse_Draw2d - Fatal编程技术网

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

Java 实现椭圆之间的多段线连接,java,user-interface,swt,ellipse,draw2d,Java,User Interface,Swt,Ellipse,Draw2d,在创建椭圆并将其位置存储在画布上之后,我尝试在画布上的两个椭圆之间创建连接。需要使用这些存储的位置来创建连接。我写的代码适用于矩形图形,但不适用于椭圆。我似乎看不出这两种情况之间的区别。有人能帮忙吗?谢谢。*我添加了简短的可测试代码来说明我的问题。要查看它在矩形中的工作情况,请取消注释说uncommentthis的行 import java.util.ArrayList; import org.eclipse.draw2d.ColorConstants; import o

在创建椭圆并将其位置存储在画布上之后,我尝试在画布上的两个椭圆之间创建连接。需要使用这些存储的位置来创建连接。我写的代码适用于矩形图形,但不适用于椭圆。我似乎看不出这两种情况之间的区别。有人能帮忙吗?谢谢。*我添加了简短的可测试代码来说明我的问题。要查看它在矩形中的工作情况,请取消注释说uncommentthis的行

    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);


    }

}

您的问题是由这样一个事实引起的,即您确定源和目标的方法
IFigure
s存在缺陷

存储查找的左上角。这对于
矩形图来说效果很好,因为该矩形实际上包含左上角。然而,
椭圆
却没有(这就是为什么
多段线连接的源和目标是它们的父对象)

最好用图片来说明:

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


你得到了什么?你期望得到什么?@verbose模式带有省略号,箭头正好位于屏幕的一侧。矩形图形和我期望的,是从一个图形到另一个图形的完整箭头。谢谢你的回答!我想不出任何其他方法,所以我只是使用落点加上一半的宽度/高度作为一个一般的大概数字,并存储它。你知道有比这更好的方法吗?在API中找不到任何相关内容。@如果您可以确定所有图形实际上都包含中心点,那么使用它就可以了。