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

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

Introduction

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

Prototype

public Rectangle getBounds() 

Source Link

Document

Returns a rectangle describing the receiver's size and location relative to its parent (or its display if its parent is null), unless the receiver is a shell.

Usage

From source file:DrawPolygonSWT.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Canvas Example");
    shell.setLayout(new FillLayout());

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

    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            Canvas canvas = (Canvas) e.widget;
            int x = canvas.getBounds().width;
            int y = canvas.getBounds().height;

            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK));

            // Create the points for drawing a triangle in the upper left
            int[] upper_left = { 0, 0, 200, 0, 0, 200 };

            // Create the points for drawing a triangle in the lower right
            int[] lower_right = { x, y, x, y - 200, x - 200, y };

            // Draw the triangles
            e.gc.fillPolygon(upper_left);
            e.gc.fillPolygon(lower_right);
        }/*  w ww .  j  av  a 2 s .  c om*/
    });

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

From source file:MainClass.java

public static void main(String[] a) {

    final Display d = new Display();
    final Shell shell = new Shell(d);

    shell.setSize(250, 200);//from   w w w .j  av  a  2  s  .  c o m

    shell.setLayout(new FillLayout());

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

    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            Canvas canvas = (Canvas) e.widget;
            int x = canvas.getBounds().width;
            int y = canvas.getBounds().height;

            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK));

            int[] upper_left = { 0, 0, 200, 0, 0, 200 };

            int[] lower_right = { x, y, x, y - 200, x - 200, y };

            e.gc.fillPolygon(upper_left);
            e.gc.fillPolygon(lower_right);

        }
    });

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

From source file:MainClass.java

public void paintControl(PaintEvent e) {
    Canvas canvas = (Canvas) e.widget;
    int x = canvas.getBounds().width;
    int y = canvas.getBounds().height;

    e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK));

    e.gc.fillArc((x - 200) / 2, (y - 200) / 2, 200, 200, 0, 90);
}

From source file:Extents.java

/**
 * Creates the main window's contents/*from  ww w  .j a  va2s . co  m*/
 * 
 * @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);
}

From source file:DoubleBuffer.java

public DoubleBuffer() {
    shell.setLayout(new FillLayout());

    final Image imageEclipse = new Image(display, "java2s.gif");

    //      final Canvas canvas = new Canvas(shell, SWT.NULL);
    //      canvas.addPaintListener(new PaintListener() {
    //         public void paintControl(PaintEvent e) {
    //            Point size = canvas.getSize();
    ////from w w  w . ja va  2  s .  c om
    //            int x1 = (int) (Math.random() * size.x);
    //            int y1 = (int) (Math.random() * size.y);
    //            int x2 = Math.max(canvas.getBounds().width - x1 - 10, 50);
    //            int y2 = Math.max(canvas.getBounds().height - y1 - 10, 50);
    //
    //            
    //            e.gc.drawRoundRectangle(x1, y1, x2, y2, 5, 5);
    //
    //            display.timerExec(100, new Runnable() {
    //               public void run() {
    //                  canvas.redraw();
    //               }
    //            });
    //            
    //         }
    //      });

    final Canvas doubleBufferedCanvas = new Canvas(shell, SWT.NO_BACKGROUND);

    doubleBufferedCanvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            // Creates new image only absolutely necessary.
            Image image = (Image) doubleBufferedCanvas.getData("double-buffer-image");
            if (image == null || image.getBounds().width != doubleBufferedCanvas.getSize().x
                    || image.getBounds().height != doubleBufferedCanvas.getSize().y) {
                image = new Image(display, doubleBufferedCanvas.getSize().x, doubleBufferedCanvas.getSize().y);
                doubleBufferedCanvas.setData("double-buffer-image", image);
            }

            // Initializes the graphics context of the image. 
            GC imageGC = new GC(image);
            imageGC.setBackground(e.gc.getBackground());
            imageGC.setForeground(e.gc.getForeground());
            imageGC.setFont(e.gc.getFont());

            // Fills background. 
            Rectangle imageSize = image.getBounds();
            imageGC.fillRectangle(0, 0, imageSize.width + 1, imageSize.height + 1);

            // Performs actual drawing here ...
            Point size = doubleBufferedCanvas.getSize();

            int x1 = (int) (Math.random() * size.x);
            int y1 = (int) (Math.random() * size.y);
            int x2 = Math.max(doubleBufferedCanvas.getBounds().width - x1 - 10, 50);
            int y2 = Math.max(doubleBufferedCanvas.getBounds().height - y1 - 10, 50);

            imageGC.drawRoundRectangle(x1, y1, x2, y2, 5, 5);

            // Draws the buffer image onto the canvas. 
            e.gc.drawImage(image, 0, 0);

            imageGC.dispose();

            display.timerExec(100, new Runnable() {
                public void run() {
                    doubleBufferedCanvas.redraw();
                }
            });
        }
    });

    shell.setSize(300, 200);
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}