用于paint命令的Java swt delete / undo

时间:2012-12-30 12:19:12

标签: java shell graphics swt java-canvas

考虑 -

public class MainCanvas extends Canvas {...}

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

现在我想从画布中删除由myGC.fillOval(10,20,30,40) ;绘制的形状。

是否有删除最后一个绘图的命令,或者用于清除画布的命令?

1 个答案:

答案 0 :(得分: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();
        }
    });

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

干杯!