Mam problem z rysowaniem figur. Stworzyłem prostą aplikację, która daje możliwość rysowania prostych figur.
public class Menu implements SelectionListener{
public Menu(Shell shell, Display display) {
super();
this.shell = shell;
this.display = display;
this.init();
this.ustawKolor();
}
private Shell shell;
private Display display;
private Canvas canvas;
private Group group01, group02;
private Label label11, label12;
private Button button01, button02, button03;
private Button button04, button05, button06, button07, button08;
private void init() {
shell.setSize(800,600);
shell.setText("Zadanie nr 1");
//grupa wybor figury
group01 = new Group(shell, SWT.SHADOW_IN);
group01.setBounds(650, 20, 130, 90);
group01.setText("Wybierz figure");
//ustawienie buttonow w grupie 1
button01 = new Button(group01, SWT.RADIO);
button01.setBounds(0, 10, 100, 20);
button01.setText("Okrag");
button02 = new Button(group01, SWT.RADIO);
button02.setBounds(0, 30, 100, 20);
button02.setText("Kwadrat");
button03 = new Button(group01, SWT.RADIO);
button03.setBounds(0, 50, 100, 20);
button03.setText("Trojkat");
//grupa wybor koloru
group02 = new Group(shell, SWT.SHADOW_IN);
group02.setBounds(650, 130, 130, 120);
group02.setText("Wybierz kolor");
button04 = new Button(group02, SWT.RADIO);
button04.setBounds(0, 10, 100, 20);
button04.setText("Czerwony");
button05 = new Button(group02, SWT.RADIO);
button05.setBounds(0, 30, 100, 20);
button05.setText("Zielony");
button06 = new Button(group02, SWT.RADIO);
button06.setBounds(0, 50, 100, 20);
button06.setText("Niebieski");
button07 = new Button(group02, SWT.RADIO);
button07.setBounds(0, 70, 100, 20);
button07.setText("Zolty");
//przycisk ustaw
button08 = new Button(shell, SWT.CENTER);
button08.setBounds(660, 250, 100, 30);
button08.setText("USTAW");
//test
label11 = new Label(shell, SWT.LEFT);
label11.setBounds(650, 390, 100, 15);
//ustawienie plotna
ustawCanvas();
}
private void ustawKolor() {
button08.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent event) {
System.out.println("TEKST");
if (button01.getSelection() == true) {
canvas.addPaintListener(new RysujKwadrat());
canvas.redraw();
}
if (button02.getSelection() == true){
canvas.addPaintListener(new RysujOkrag());
canvas.redraw();
}
if (button03.getSelection() == true){
canvas.addPaintListener(new RysujTrojkat());
canvas.redraw();
}
}
});
}
}
Po wybraniu odpowiedniego kształtu poprzez kliknięcie przycisku pojawiają się odpowiedni figury, jednak z płaszczyzny canvas nie znika poprzednio wyświetlona. W jaki sposób można czyścić powierzchnię canvas, żeby po każdym kliknięciu pojawiała się wybrana figura?