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

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

Introduction

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

Prototype

public void setBounds(int x, int y, int width, int height) 

Source Link

Document

Sets the receiver's size and location to the rectangular area specified by the arguments.

Usage

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 242");
    shell.setBounds(10, 10, 200, 200);//from   w w w  .j  ava  2  s  .c o m
    Canvas canvas = new Canvas(shell, SWT.BORDER);
    canvas.setBounds(10, 50, 150, 100);
    canvas.addPaintListener(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(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:CanvasTranverseEvent.java

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

    final Canvas c = new Canvas(shell, SWT.BORDER);
    c.setBounds(10, 50, 100, 32);
    c.addListener(SWT.Traverse, new Listener() {
        public void handleEvent(Event e) {
            switch (e.detail) {
            /* Do tab group traversal */
            case SWT.TRAVERSE_ESCAPE:
                System.out.println("SWT.TRAVERSE_ESCAPE");
                break;
            case SWT.TRAVERSE_RETURN:
            case SWT.TRAVERSE_TAB_NEXT:
                System.out.println("SWT.TRAVERSE_TAB_NEXT");
                break;
            case SWT.TRAVERSE_TAB_PREVIOUS:
            case SWT.TRAVERSE_PAGE_NEXT:
            case SWT.TRAVERSE_PAGE_PREVIOUS:
                e.doit = true;/*from  ww w.  j  a v  a 2  s .  co m*/
                break;
            }
        }
    });

    Text t = new Text(shell, SWT.SINGLE | SWT.BORDER);
    t.setBounds(10, 85, 100, 32);

    c.setFocus();
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    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);/* www.j a  v a2  s  .  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:ColorTransparentCreate.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 200, 200);//  w  w  w.ja  v  a 2 s  .  c o  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.Snippet95.java

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

    final Table table = new Table(shell, SWT.MULTI);
    table.setLinesVisible(true);/*from ww w.j a  v a  2s  .c  o m*/
    table.setBounds(10, 10, 100, 100);
    for (int i = 0; i < 9; i++) {
        new TableItem(table, SWT.NONE).setText("item" + i);
    }

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.pack();
    button.setLocation(10, 140);
    button.addListener(SWT.Selection, event -> {
        Point tableSize = table.getSize();
        GC gc = new GC(table);
        final Image image = new Image(display, tableSize.x, tableSize.y);
        gc.copyArea(image, 0, 0);
        gc.dispose();

        Shell popup = new Shell(shell);
        popup.setText("Image");
        popup.addListener(SWT.Close, e -> image.dispose());

        Canvas canvas = new Canvas(popup, SWT.NONE);
        canvas.setBounds(10, 10, tableSize.x + 10, tableSize.y + 10);
        canvas.addPaintListener(e -> e.gc.drawImage(image, 0, 0));
        popup.pack();
        popup.open();
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    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  ww w  .  j  av a 2  s  .  c  o  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();
}

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);//from   w ww.j a v a  2s. 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:ScreenshotCaptureGC.java

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

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.pack();//w w w  .  ja va 2  s  . c  o m
    button.setLocation(10, 140);
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            GC gc = new GC(display);
            final Image image = new Image(display, 400, 400);
            gc.copyArea(image, 0, 0);
            gc.dispose();

            Shell popup = new Shell(shell);
            popup.setText("Image");
            popup.addListener(SWT.Close, new Listener() {
                public void handleEvent(Event e) {
                    image.dispose();
                }
            });

            Canvas canvas = new Canvas(popup, SWT.NONE);
            canvas.setBounds(10, 10, 400 + 10, 400 + 10);
            canvas.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(image, 0, 0);
                }
            });
            popup.pack();
            popup.open();
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet95.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Widget");
    shell.setBounds(10, 10, 200, 200);//from  w w  w.  j a  v a  2  s.c  om

    final Table table = new Table(shell, SWT.MULTI);
    table.setLinesVisible(true);
    table.setBounds(10, 10, 100, 100);
    for (int i = 0; i < 9; i++) {
        new TableItem(table, SWT.NONE).setText("item" + i);
    }

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.setBounds(10, 140, 50, 20);
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            Point tableSize = table.getSize();
            GC gc = new GC(table);
            final Image image = new Image(display, tableSize.x, tableSize.y);
            gc.copyArea(image, 0, 0);
            gc.dispose();

            Shell popup = new Shell(shell);
            popup.setText("Image");
            popup.setBounds(50, 50, 200, 200);
            popup.addListener(SWT.Close, new Listener() {
                public void handleEvent(Event e) {
                    image.dispose();
                }
            });

            Canvas canvas = new Canvas(popup, SWT.NONE);
            canvas.setBounds(10, 10, 150, 150);
            canvas.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(image, 0, 0);
                }
            });
            popup.open();
        }
    });

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

From source file:CaptureWidgetImageGC.java

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

    final Table table = new Table(shell, SWT.MULTI);
    table.setLinesVisible(true);/*from  w w w  .  j  av a  2 s. c o m*/
    table.setBounds(10, 10, 100, 100);
    for (int i = 0; i < 9; i++) {
        new TableItem(table, SWT.NONE).setText("item" + i);
    }

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.pack();
    button.setLocation(10, 140);
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            Point tableSize = table.getSize();
            GC gc = new GC(table);
            final Image image = new Image(display, tableSize.x, tableSize.y);
            gc.copyArea(image, 0, 0);
            gc.dispose();

            Shell popup = new Shell(shell);
            popup.setText("Image");
            popup.addListener(SWT.Close, new Listener() {
                public void handleEvent(Event e) {
                    image.dispose();
                }
            });

            Canvas canvas = new Canvas(popup, SWT.NONE);
            canvas.setBounds(10, 10, tableSize.x + 10, tableSize.y + 10);
            canvas.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(image, 0, 0);
                }
            });
            popup.pack();
            popup.open();
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}