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(Rectangle bounds) 

Source Link

Document

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

Usage

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);/*from w  w  w . j a  v a  2  s  .  c om*/
        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();
}

From source file:ScreenShotWithGC.java

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

            /* Take the screen shot */
            GC gc = new GC(display);
            final Image image = new Image(display, display.getBounds());
            gc.copyArea(image, 0, 0);/*  www .j  av  a  2  s. c  o  m*/
            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, new Listener() {
                public void handleEvent(Event 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(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(image, 0, 0);
                }
            });
            popup.open();
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:FontSizeString.java

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

    final Canvas canvas = new Canvas(shell, SWT.NONE);

    shell.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent event) {
            canvas.setBounds(shell.getClientArea());
        }/*  w w  w  .j a va  2 s  . c o  m*/
    });
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent event) {
            event.gc.setFont(font);
            Point pt = event.gc.stringExtent(STRING);

            System.out.println(pt);
        }
    });

    // Create an editor to house the dropdown
    ControlEditor editor = new ControlEditor(canvas);

    // Create the combo and fill it
    final Combo combo = new Combo(canvas, SWT.READ_ONLY);
    for (int i = 0, n = SIZES.length; i < n; i++) {
        combo.add(SIZES[i]);
    }

    // Set up the editor
    editor.horizontalAlignment = SWT.CENTER;
    editor.verticalAlignment = SWT.TOP;
    Point size = combo.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    editor.minimumWidth = size.x;
    editor.minimumHeight = size.y;
    editor.setEditor(combo);

    combo.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            if (font != null)
                font.dispose();
            font = new Font(shell.getDisplay(), "Helvetica", new Integer(combo.getText()).intValue(), SWT.BOLD);
            canvas.redraw();
        }
    });

    // Select the first item in the combo
    combo.select(0);

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

From source file:Extents.java

/**
 * Creates the main window's contents//from  w  w  w.  j  a va 2 s.com
 * 
 * @param shell the main window
 */
private void createContents(final Shell shell) {
    // Create a canvas to draw on
    final Canvas canvas = new Canvas(shell, SWT.NONE);

    // Add a listener to the shell to resize the canvas to fill the window
    // any time the window is resized
    shell.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent event) {
            canvas.setBounds(shell.getClientArea());
        }
    });

    // Add a listener to the canvas. This is where we draw the text.
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent event) {
            // Set the font into the gc
            event.gc.setFont(font);

            // Calcalute the width (nad height) of the string
            Point pt = event.gc.stringExtent(STRING);

            // Figure out how big our drawing area is
            Rectangle rect = canvas.getBounds();

            // Calculate the height of the font. We could have used pt.y,
            // but this demonstrates FontMetrics
            int height = event.gc.getFontMetrics().getHeight();

            // Outside loop goes from the top of the window to the bottom.
            // Since the (x, y) passed to drawString represents the upper left
            // corner, subtract the height of the font from the height of the
            // drawing area, so we don't have any partial drawing.
            for (int i = 0, n = rect.height - height; i < n; i += height) {
                // Inside loop goes from the left to the right, stopping far enough
                // from the right to ensure no partial string drawing.
                for (int j = 0, m = rect.width - pt.x; j < m; j += pt.x) {
                    // Draw the string
                    event.gc.drawString(STRING, j, i);
                }
            }
        }
    });

    // Create an editor to house the dropdown
    ControlEditor editor = new ControlEditor(canvas);

    // Create the combo and fill it
    final Combo combo = new Combo(canvas, SWT.READ_ONLY);
    for (int i = 0, n = SIZES.length; i < n; i++) {
        combo.add(SIZES[i]);
    }

    // Set up the editor
    editor.horizontalAlignment = SWT.CENTER;
    editor.verticalAlignment = SWT.TOP;
    Point size = combo.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    editor.minimumWidth = size.x;
    editor.minimumHeight = size.y;
    editor.setEditor(combo);

    // Add a listener to the combo, so that when the selection changes,
    // we change the font and redraw the canvas
    combo.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            if (font != null)
                font.dispose();
            font = new Font(shell.getDisplay(), "Helvetica", new Integer(combo.getText()).intValue(), SWT.BOLD);
            canvas.redraw();
        }
    });

    // Select the first item in the combo
    combo.select(0);
}