2012-12-30 1 views
2

이 고려 commad -

Display display = new Display(); 
display = new Display(); 
myShell = new Shell(display); 
myCanvas = new MainCanvas(myShell, SWT.NO); 
GC myGC = new GC(myShell); 
myGC.fillOval(10,20,30,40) ; //paint shape .. 

public class MainCanvas extends Canvas {...} 

그리고

을 지금은 캔버스에서 myGC.fillOval(10,20,30,40) ; 그린 모양을 삭제할.

마지막 페인트를 삭제하거나 캔버스를 지우라는 명령이 있습니까?

+2

왜 전체 GC를 덮는 직사각형을 채우지 않을까요? –

+0

'javax.swing.undo'는 Swing에서 실행 취소/다시 실행 기능을 제공합니다. SWT에서 대안이 확실하지 않습니다. –

+3

할 수있는 작업은 각 그리기 작업 후에 캔버스를 이미지로 저장하고 실행 취소. –

답변

1

아주 좋은 질문입니다. 방금 JAVA SWT를 사용하기 시작했고 같은 문제가 발생했습니다.

필자가 생각해 낸 해결책은 캔버스를 다른 것으로 영향을 미치지 않고 내용을 비워야 할 때마다 동일한 캔버스를 새로운 캔버스로 교체하는 것입니다. 이를 위해

, 나는() 및 shell.pack()를 canvas.dispose() 명령을 사용하고 다시 그리기 및 shell.redraw를 사용하여 셸을 재 포장하고 그래서 윈도우가 제대로 크기가 조정된다. 이러한 명령은 버튼 누름과 같은 다른 이벤트에서 호출됩니다 (아래 제공된 예제의 Enter 버튼). 또한 아래 예제에서 GridLayout을 사용하고 있습니다 (자세한 내용은 http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html을 참조하십시오). 정수 배열을 사용하여 폴리 라인을 작성 중입니다.

myCanvas = new Canvas(shell, SWT.BORDER); // create the initial instance of the Canvas 
    gridData = new GridData(GridData.FILL, GridData.FILL, true, true); 
    gridData.widthHint = 1100; // set desired width 
    gridData.heightHint = 800; // set desired height 
    gridData.verticalSpan = 3; // set number of columns it will occupy 
    myCanvas.setLayoutData(gridData); 


    myEnter_Button.addSelectionListener(new SelectionAdapter() { 
     public void widgetSelected(SelectionEvent mainEvent) { 
      myCanvas.dispose(); // delete the Canvas 
      myCanvas = new Canvas(shell, SWT.BORDER); 
      GridData redrawGridData = new GridData(GridData.FILL, GridData.FILL, true, true); 
      redrawGridData.widthHint = 1100; 
      redrawGridData.heightHint = 800; 
      redrawGridData.verticalSpan = 3; 
      myCanvas.setLayoutData(redrawGridData); 
      shell.redraw(); 
      shell.pack(); // pack shell again 

    myCanvas.addPaintListener(new PaintListener() { 
       public void paintControl(final PaintEvent event) { 
        // coordinateIntegerArray not displayed in this example 
        event.gc.drawPolyline(coordinateIntegerArray);//draw something 

        } 
       } 
      }); 

      myCanvas.redraw(); 
     } 
    }); 

이 도움이 되었기를 바랍니다. 마지막으로 그린 ​​페인트 객체를 독점적으로 삭제/실행 취소하는 방법을 찾으면 알려 드리겠습니다.

건배!