navy1978
navy1978

Reputation: 1439

swt: How to update(redraw) only a portion of a canvas

I'm trying to update just a portion of a canvas in SWT, but I don't understand how to do it.

I read tht I have to use the setClipping, the documentation indeed says: "Sets the area of the receiver which can be changed by drawing operations to the rectangular area specified by the argument. Specifying null for the rectangle reverts the receiver's clipping area to its original value."

So I have just tried but with no luck, here a simple example:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;


public class SimpleCanvas {

    boolean manualDraw=false;
    public void run() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Canvas Example");
        createContents(shell);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    /**
     * Creates the main window's contents
     * 
     * @param shell the main window
     */
    private void createContents(Shell shell) {
        shell.setLayout(new FillLayout());

        // Create a canvas
        Canvas canvas = new Canvas(shell, SWT.NONE);

        // Create a button on the canvas
        Button button = new Button(shell, SWT.PUSH);
        button.setBounds(10, 10, 300, 40);
        button.setText("TEST");
        button.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                switch (e.type) {
                case SWT.Selection:
                    manualDraw=true;
                    canvas.redraw();
                    break;
                }
            }
        });

        // Create a paint handler for the canvas
        canvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {

                if (manualDraw){
                    e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GREEN));
                    e.gc.setClipping(90,90,60,60);
                    e.gc.drawRectangle(90,90,30,30);
                    return ;
                }


                Rectangle rect = ((Canvas) e.widget).getBounds();
                e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_RED));
                e.gc.drawText("DRAW TEXT", 0, 0);
                e.gc.dispose();
            }
        });
    }

    /**
     * The application entry point
     * 
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new SimpleCanvas().run();
    }
}

Can you please help me to understand what I'm doing wrong?

Thank you in advance.

Upvotes: 2

Views: 1266

Answers (1)

navy1978
navy1978

Reputation: 1439

I found the problem. In order to update only a portion of the canvas I don't have to call :

canvas.redraw();

and drawing there a portion of the canvas, but instead get the GC from canvas and use the setClipping there, so invoke something like that:

public void redrawCanvas (Canvas canvas) {
        GC gc = new GC(canvas);
        gc.setClipping(90,90,60,60);
        gc.drawRectangle(90,90,30,30);
        gc.dispose();
    }

Upvotes: 1

Related Questions