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

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

Introduction

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

Prototype

public Canvas(Composite parent, int style) 

Source Link

Document

Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

Usage

From source file:TransformReflectionXYAxis.java

public static void main(String[] args) {
    final Display display = new Display();

    final Image image = new Image(display, 110, 60);
    GC gc = new GC(image);
    Font font = new Font(display, "Times", 30, SWT.BOLD);
    gc.setFont(font);//w  ww.j a v  a 2 s.c  o  m
    gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
    gc.fillRectangle(0, 0, 110, 60);
    gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
    gc.drawText("SWT", 10, 10, true);
    font.dispose();
    gc.dispose();

    final Rectangle rect = image.getBounds();
    Shell shell = new Shell(display);
    shell.setText("Matrix Tranformations");
    shell.setLayout(new FillLayout());

    final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            GC gc = e.gc;
            gc.setAdvanced(true);
            if (!gc.getAdvanced()) {
                gc.drawText("Advanced graphics not supported", 30, 30, true);
                return;
            }

            // Original image
            int x = 30, y = 30;
            gc.drawImage(image, x, y);
            x += rect.width + 30;

            Transform transform = new Transform(display);

            // Reflect around the y axis.
            transform.setElements(-1, 0, 0, 1, 0, 0);
            gc.setTransform(transform);
            gc.drawImage(image, -1 * x - rect.width, y);

            x = 30;
            y += rect.height + 30;

            // Reflect around the x axis.
            transform.setElements(1, 0, 0, -1, 0, 0);
            gc.setTransform(transform);
            gc.drawImage(image, x, -1 * y - rect.height);

            transform.dispose();
        }
    });

    shell.setSize(350, 550);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    image.dispose();
    display.dispose();
}

From source file:CursorHideControl.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 200, 200);/*w  w  w.  j a v a 2s .  c o  m*/
    Canvas canvas = new Canvas(shell, SWT.BORDER);
    canvas.setBounds(10, 50, 150, 100);
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            e.gc.drawString("hide Cursor here", 10, 10);
        }
    });

    // create a cursor with a transparent image
    Color white = display.getSystemColor(SWT.COLOR_WHITE);
    Color black = display.getSystemColor(SWT.COLOR_BLACK);
    PaletteData palette = new PaletteData(new RGB[] { white.getRGB(), black.getRGB() });
    ImageData sourceData = new ImageData(16, 16, 1, palette);
    sourceData.transparentPixel = 0;
    Cursor cursor = new Cursor(display, sourceData, 0, 0);

    shell.open();
    canvas.setCursor(cursor);
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    cursor.dispose();
    display.dispose();
}

From source file:AnimationDoubleBuffering.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Animator");

    shell.setLayout(new FillLayout());
    canvas = new Canvas(shell, SWT.NO_BACKGROUND);
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent event) {
            // Create the image to fill the canvas
            Image image = new Image(shell.getDisplay(), canvas.getBounds());
            // Set up the offscreen gc
            GC gcImage = new GC(image);

            gcImage.setBackground(event.gc.getBackground());
            gcImage.fillRectangle(image.getBounds());
            gcImage.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
            gcImage.fillOval(x, y, IMAGE_WIDTH, IMAGE_WIDTH);

            // Draw the offscreen buffer to the screen
            event.gc.drawImage(image, 0, 0);

            image.dispose();//from   w  ww  .  j  ava  2  s  .c om
            gcImage.dispose();
        }
    });

    shell.open();
    Runnable runnable = new Runnable() {
        public void run() {
            animate();
            display.timerExec(TIMER_INTERVAL, this);
        }
    };
    display.timerExec(TIMER_INTERVAL, runnable);

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    // Kill the timer
    display.timerExec(-1, runnable);
    display.dispose();

}

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();//from w ww.j  a  v a2s . com

    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();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Color red = display.getSystemColor(SWT.COLOR_RED);
    final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    Shell shell = new Shell(display);
    shell.setText("Snippet 21");
    Button b = new Button(shell, SWT.PUSH);
    Rectangle clientArea = shell.getClientArea();
    b.setBounds(clientArea.x + 10, clientArea.y + 10, 100, 32);
    b.setText("Button");
    shell.setDefaultButton(b);//w  ww .j av  a2  s .co  m
    final Canvas c = new Canvas(shell, SWT.BORDER);
    c.setBounds(10, 50, 100, 32);
    c.addListener(SWT.Traverse, e -> {
        switch (e.detail) {
        /* Do tab group traversal */
        case SWT.TRAVERSE_ESCAPE:
        case SWT.TRAVERSE_RETURN:
        case SWT.TRAVERSE_TAB_NEXT:
        case SWT.TRAVERSE_TAB_PREVIOUS:
        case SWT.TRAVERSE_PAGE_NEXT:
        case SWT.TRAVERSE_PAGE_PREVIOUS:
            e.doit = true;
            break;
        }
    });
    c.addListener(SWT.FocusIn, e -> c.setBackground(red));
    c.addListener(SWT.FocusOut, e -> c.setBackground(blue));
    c.addListener(SWT.KeyDown, e -> System.out.println("KEY"));
    Text t = new Text(shell, SWT.SINGLE | SWT.BORDER);
    t.setBounds(10, 85, 100, 32);

    Text r = new Text(shell, SWT.MULTI | SWT.BORDER);
    r.setBounds(10, 120, 100, 32);

    c.setFocus();
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Image image = new Image(display, 20, 20);
    Color color = display.getSystemColor(SWT.COLOR_RED);
    GC gc = new GC(image);
    gc.setBackground(color);/*from   w  ww . j a  v  a  2  s  .c  om*/
    gc.fillRectangle(image.getBounds());
    gc.dispose();

    Shell shell = new Shell(display);
    shell.setText("Snippet 112");
    shell.setLayout(new FillLayout());
    Group group = new Group(shell, SWT.NONE);
    group.setLayout(new FillLayout());
    group.setText("a square");
    Canvas canvas = new Canvas(group, SWT.NONE);
    canvas.addPaintListener(e -> e.gc.drawImage(image, 0, 0));

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    image.dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("OpenGL in SWT");
    shell.setLayout(new FillLayout());
    final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);
    canvas.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            resize(canvas);//  w w w.  java 2  s  .  c o m
        }
    });
    final GLContext context = init(canvas);
    shell.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
            context.dispose();
        }
    });
    new Runnable() {
        public void run() {
            if (canvas.isDisposed())
                return;
            render();
            context.swapBuffers();
            canvas.getDisplay().timerExec(50, this);
        }
    }.run();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:ColorTransparentCreate.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 200, 200);/*from w w  w  .  ja  va2s  . co  m*/

    // create a cursor with a transparent image
    Color white = display.getSystemColor(SWT.COLOR_WHITE);
    Color black = display.getSystemColor(SWT.COLOR_BLACK);
    PaletteData palette = new PaletteData(new RGB[] { white.getRGB(), black.getRGB() });
    final ImageData sourceData = new ImageData(30, 30, 1, palette);
    sourceData.transparentPixel = 0;

    Canvas canvas = new Canvas(shell, SWT.BORDER);
    canvas.setBounds(10, 50, 150, 100);

    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            e.gc.drawString("hide Cursor here", 10, 10);
            e.gc.drawImage(new Image(display, sourceData), 0, 0);
        }
    });

    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }

    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();

    final Image image = new Image(display, 110, 60);
    GC gc = new GC(image);
    Font font = new Font(display, "Times", 30, SWT.BOLD);
    gc.setFont(font);// www . j  ava  2s.c  o  m
    gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
    gc.fillRectangle(0, 0, 110, 60);
    gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
    gc.drawText("SWT", 10, 10, true);
    font.dispose();
    gc.dispose();

    final Rectangle rect = image.getBounds();
    Shell shell = new Shell(display);
    shell.setText("Matrix Tranformations");
    shell.setLayout(new FillLayout());
    final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
    canvas.addPaintListener(e -> {
        GC gc1 = e.gc;
        gc1.setAdvanced(true);
        if (!gc1.getAdvanced()) {
            gc1.drawText("Advanced graphics not supported", 30, 30, true);
            return;
        }

        // Original image
        int x = 30, y = 30;
        gc1.drawImage(image, x, y);
        x += rect.width + 30;

        Transform transform = new Transform(display);

        // Note that the tranform is applied to the whole GC therefore
        // the coordinates need to be adjusted too.

        // Reflect around the y axis.
        transform.setElements(-1, 0, 0, 1, 0, 0);
        gc1.setTransform(transform);
        gc1.drawImage(image, -1 * x - rect.width, y);

        x = 30;
        y += rect.height + 30;

        // Reflect around the x axis.
        transform.setElements(1, 0, 0, -1, 0, 0);
        gc1.setTransform(transform);
        gc1.drawImage(image, x, -1 * y - rect.height);

        x += rect.width + 30;

        // Reflect around the x and y axes
        transform.setElements(-1, 0, 0, -1, 0, 0);
        gc1.setTransform(transform);
        gc1.drawImage(image, -1 * x - rect.width, -1 * y - rect.height);

        x = 30;
        y += rect.height + 30;

        // Shear in the x-direction
        transform.setElements(1, 0, -1, 1, 0, 0);
        gc1.setTransform(transform);
        gc1.drawImage(image, 300, y);

        // Shear in y-direction
        transform.setElements(1, -1, 0, 1, 0, 0);
        gc1.setTransform(transform);
        gc1.drawImage(image, 150, 475);

        // Rotate by 45 degrees
        float cos45 = (float) Math.cos(Math.PI / 4);
        float sin45 = (float) Math.sin(Math.PI / 4);
        transform.setElements(cos45, sin45, -sin45, cos45, 0, 0);
        gc1.setTransform(transform);
        gc1.drawImage(image, 400, 60);

        transform.dispose();
    });

    shell.setSize(350, 550);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    image.dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 215");
    shell.setLayout(new FillLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.addListener(SWT.Selection, event -> {

        /* Take the screen shot */
        GC gc = new GC(display);
        final Image image = new Image(display, display.getBounds());
        gc.copyArea(image, 0, 0);/*  ww  w  .j av a 2s .  com*/
        gc.dispose();

        Shell popup = new Shell(shell, SWT.SHELL_TRIM);
        popup.setLayout(new FillLayout());
        popup.setText("Image");
        popup.setBounds(50, 50, 200, 200);
        popup.addListener(SWT.Close, e -> image.dispose());

        ScrolledComposite sc = new ScrolledComposite(popup, SWT.V_SCROLL | SWT.H_SCROLL);
        Canvas canvas = new Canvas(sc, SWT.NONE);
        sc.setContent(canvas);
        canvas.setBounds(display.getBounds());
        canvas.addPaintListener(e -> e.gc.drawImage(image, 0, 0));
        popup.open();
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}