2

Consider -

public class MainCanvas extends Canvas {...}

And

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 ..

Now I want to delete the shape painted by myGC.fillOval(10,20,30,40) ; from the canvas .

Is there any command to delete the last paint , or command to clear the canvas ?

4

1 回答 1

1

非常好的问题。我刚开始使用 JAVA SWT,遇到了同样的问题。

我想出的解决方案是用一个新的 Canvas 替换它,每次我必须清空它的内容而不影响其他任何东西时,它都是相同的。

为此,我正在使用canvas.dispose()命令并使用shell.redraw()shell.pack()重绘和重新打包 Shell,以便正确调整窗口大小。这些命令是从另一个事件中调用的,例如按下按钮(下面提供的示例中的 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();
        }
    });

我希望这会有所帮助。如果我设法找到一种专门删除/撤消绘制的最后一个绘画对象的方法,我一定会让你知道。

干杯!

于 2013-09-23T14:30:29.027 回答