Example usage for org.eclipse.swt.widgets Canvas redraw

List of usage examples for org.eclipse.swt.widgets Canvas redraw

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Canvas redraw.

Prototype


public void redraw(int x, int y, int width, int height, boolean all) 

Source Link

Document

Causes the rectangular area of the receiver specified by the arguments to be marked as needing to be redrawn.

Usage

From source file:org.eclipse.swt.snippets.Snippet275.java

public static void main(String[] args) {
    final int INTERVAL = 888;
    final Display display = new Display();
    final Image image = new Image(display, 750, 750);
    GC gc = new GC(image);
    gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
    gc.fillRectangle(image.getBounds());
    gc.dispose();/*w  ww.  ja v a2 s  .  co  m*/

    Shell shell = new Shell(display);
    shell.setText("Snippet 275");
    shell.setBounds(10, 10, 790, 790);
    final Canvas canvas = new Canvas(shell, SWT.NONE);
    canvas.setBounds(10, 10, 750, 750);
    canvas.addListener(SWT.Paint, event -> {
        value = String.valueOf(System.currentTimeMillis());
        event.gc.drawImage(image, 0, 0);
        event.gc.drawString(value, 10, 10, true);
    });
    display.timerExec(INTERVAL, new Runnable() {
        @Override
        public void run() {
            if (canvas.isDisposed())
                return;
            // canvas.redraw (); // <-- bad, damages more than is needed
            GC gc = new GC(canvas);
            Point extent = gc.stringExtent(value + '0');
            gc.dispose();
            canvas.redraw(10, 10, extent.x, extent.y, false);
            display.timerExec(INTERVAL, this);
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    image.dispose();
    display.dispose();
}