2014-07-22 2 views
1

나는 타원을 만들고 캔버스에 위치를 저장 한 후 캔버스에있는 두 개의 타원 사이에 연결을 만들려고합니다. 이러한 저장된 위치는 연결을 작성하는 데 사용해야합니다. 필자가 작성한 코드는 RectangleFigures에서 작동하지만 타원에서는 사용할 수 없습니다. 두 경우의 차이점을 볼 수 없습니다. 누군가 제발 도와 줄 수 있니? 고마워. * 내 문제를 설명하기 위해 간단한 테스트 가능한 코드를 추가했다. 이 사각형 작업을 참조하려면 방법은 원인을 파악하고 IFigure의이 결함이 대상 것으로, UNCOMMENT 본타원 사이의 폴리 라인 연결 구현

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


    } 

} 
+0

당신이 무엇을 어떻게해야합니까 무엇 기대하니? – Mirco

+0

@ verbose-mode 타원을 사용하면 화살촉이 화면 측면에 나타납니다. RectangleFigures와 내가 예상하는 것은 한 그림에서 다른 그림으로 완성 된 화살입니다. – Asher

답변

2

귀하의 문제는 사실로 인해 발생 말하는 행의 주석을 해제하십시오.

조회를 위해 왼쪽 상단 구석을 저장합니다. RectangleFigure의 경우 제대로 작동합니다. 사각형에 실제로 왼쪽 상단 구석이 있기 때문입니다. 그러나 Ellipse은 그렇지 않습니다 (따라서 PolylineConnection의 출처와 대상이 상위 임).

최상의 이미지로 설명 : 대신 그림의 중심을 저장하는 경우

enter image description here

이 될 겁니다 :

enter image description here

+0

답변 해 주셔서 감사합니다. 나는 다른 방법을 생각할 수 없으므로 드롭 포인트와 폭/높이의 절반을 일반 야구장 그림으로 저장하고이를 사용하고 있습니다. 그래도 그보다 더 좋은 방법을 알고 있니? API와 관련된 내용을 찾을 수 없습니다. – Asher

+0

@Asher 모든 인물에 실제로 중심점이 포함되어 있는지 확신 할 수 있으면 사용하는 것이 좋습니다. – Baz