Reputation: 11012
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 ?
Upvotes: 2
Views: 624
Reputation: 11
Very good question. I have just started using JAVA SWT and I have encountered the same issue.
The solution I have come up with is to replace the Canvas with a new one, identical one each time I have to empty its contents without affecting anything else.
To this end, I am using the canvas.dispose() command and redraw and repack the Shell using shell.redraw() and shell.pack() so that the window is resized properly. These commands are called from another event, like the press of a button (an Enter button in the example provided below). Also, please note that in the example below I am using a GridLayout (for more information, please refer to http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html) and I am creating a polyline using an array of integers.
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();
}
});
I hope this helped. If I manage to find a way of exclusively deleting / undoing the last paint object drawn, I'll be sure to let you know.
Cheers!
Upvotes: 1