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:GroupImageAdding.java

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

    Shell shell = new Shell(display);
    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(new PaintListener() {
        public void paintControl(PaintEvent e) {
            e.gc.drawImage(new Image(display, "yourFile.gif"), 0, 0);
        }//  w  w  w .jav  a2  s  .com
    });

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

    display.dispose();
}

From source file:FontMetricsPaintSWT.java

public static void main(String[] args) {
    final Display display = new Display();
    final 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) {

            Font font = new Font(shell.getDisplay(), new FontData("Helvetica", 18, SWT.NORMAL));
            e.gc.setFont(font);/*from   w w w  .  j  a  va 2s.  c  om*/
            e.gc.drawText("My Text", 0, 0);

            FontMetrics fm = e.gc.getFontMetrics();
            int bHeight = fm.getLeading() + fm.getAscent();
            int oHeight = fm.getAscent();
            int yHeight = fm.getAscent() + fm.getDescent();
            int totalHeight = fm.getHeight(); // Equals fm.getLeading() + fm.getAscent()
                                              // + fm.getDescent();

            e.gc.drawLine(10, bHeight, 100, bHeight);
            e.gc.drawLine(10, oHeight, 100, oHeight);
            e.gc.drawLine(10, yHeight, 100, yHeight);
            e.gc.drawLine(10, totalHeight, 100, totalHeight);

            font.dispose();

        }
    });

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

From source file:ImagePartDraw.java

public static void main(String[] args) {
    final Display display = new Display();
    final 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) {
            Image image = new Image(display, "yourFile.gif");
            System.out.println(image.getImageData().scanlinePad);
            image.getImageData().scanlinePad = 40;
            System.out.println(image.getImageData().scanlinePad);

            e.gc.drawImage(image, 0, 0);
            // Determine how big the drawing area is
            Rectangle rect = shell.getClientArea();

            // Get information about the image
            ImageData data = image.getImageData();

            // Calculate drawing values
            int srcX = data.width / 4;
            int srcY = data.height / 4;
            int srcWidth = data.width / 2;
            int srcHeight = data.height / 2;
            int destWidth = 2 * srcWidth;
            int destHeight = 2 * srcHeight;

            // Draw the image
            e.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, rect.width - destWidth,
                    rect.height - destHeight, destWidth, destHeight);
        }/*from   w  w  w  . j av  a2  s.c  o m*/
    });

    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  v  a  2 s.c  om*/
    });
    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: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 a va2s. com
    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: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);
    Button b = new Button(shell, SWT.PUSH);
    b.setBounds(10, 10, 100, 32);/* w ww. j a  v  a2s  .com*/
    b.setText("Button");
    shell.setDefaultButton(b);
    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:
            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, new Listener() {
        public void handleEvent(Event e) {
            c.setBackground(red);
        }
    });
    c.addListener(SWT.FocusOut, new Listener() {
        public void handleEvent(Event e) {
            c.setBackground(blue);
        }
    });
    c.addListener(SWT.KeyDown, new Listener() {
        public void handleEvent(Event e) {
            System.out.println("KEY");
            for (int i = 0; i < 64; i++) {
                Color c1 = red, c2 = blue;
                if (c.isFocusControl()) {
                    c1 = blue;
                    c2 = red;
                }
                c.setBackground(c1);
                c.update();
                c.setBackground(c2);
            }
        }
    });
    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.Snippet286.java

public static void main(java.lang.String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 286");
    shell.setLayout(new GridLayout());

    Canvas blankCanvas = new Canvas(shell, SWT.BORDER);
    blankCanvas.setLayoutData(new GridData(200, 200));
    final Label statusLine = new Label(shell, SWT.NONE);
    statusLine.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    Menu bar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(bar);//w  ww  .jav  a  2  s  .com

    MenuItem menuItem = new MenuItem(bar, SWT.CASCADE);
    menuItem.setText("Test");
    Menu menu = new Menu(bar);
    menuItem.setMenu(menu);

    for (int i = 0; i < 5; i++) {
        MenuItem item = new MenuItem(menu, SWT.PUSH);
        item.setText("Item " + i);
        item.addArmListener(e -> statusLine.setText(((MenuItem) e.getSource()).getText()));
    }

    shell.pack();
    shell.open();

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

From source file: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 www.j a va2 s  . c  o  m*/
    gc.fillRectangle(image.getBounds());
    gc.dispose();
    color.dispose();

    Shell shell = new Shell(display);
    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(new PaintListener() {
        public void paintControl(PaintEvent 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:SheerXYDirection.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);/*from   www .j ava  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);

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

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

            transform.dispose();
        }
    });

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

From source file:Rotate45Degrees.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);/*ww w.  j a v a2s .  co  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);

            // Rotate by 45 degrees 
            //float cos45 = (float)Math.cos(45);
            float cos45 = (float) Math.cos(Math.PI / 4);

            //float sin45 = (float)Math.sin(45);
            float sin45 = (float) Math.sin(Math.PI / 4);

            transform.setElements(cos45, sin45, -sin45, cos45, 0, 0);
            gc.setTransform(transform);
            gc.drawImage(image, 350, 100);

            transform.dispose();
        }
    });

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