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: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 ww  . ja v 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:DrawExample.java

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

    Canvas canvas = new Canvas(shell, SWT.NONE);
    canvas.setSize(150, 150);/*  w  w  w  .  j  a va 2s  . c o  m*/
    canvas.setLocation(20, 20);
    shell.open();
    shell.setSize(200, 220);

    GC gc = new GC(canvas);
    gc.drawRectangle(10, 10, 40, 45);
    gc.drawOval(65, 10, 30, 35);
    gc.drawLine(130, 10, 90, 80);
    gc.drawPolygon(new int[] { 20, 70, 45, 90, 70, 70 });
    gc.drawPolyline(new int[] { 10, 120, 70, 100, 100, 130, 130, 75 });
    gc.dispose();

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

From source file:GCCreateFrom.java

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

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

    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            GC gc = new GC(canvas);
            gc.setForeground(e.display.getSystemColor(SWT.COLOR_RED));
            gc.drawFocus(5, 5, 200, 10);
            gc.drawText("You can draw text directly on a canvas", 60, 60);
            gc.dispose();// w w w . ja va2 s.c o m
        }
    });

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

From source file:FontChangeSWT.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(), "Helvetica", 18, SWT.NORMAL);
            e.gc.setFont(font);// w w w .j a v  a 2  s.  c  o  m
            e.gc.drawText("My Text", 0, 0);
            font.dispose();

        }
    });

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

From source file:ImageLoadingFromFile.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");

            e.gc.drawImage(image, 10, 10);

            image.dispose();//  w  ww . j  a va  2s .com
        }
    });

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

From source file:ImageDisable.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");

            Image disable = new Image(display, image, SWT.IMAGE_DISABLE);

            e.gc.drawImage(disable, 10, 10);

            e.gc.drawImage(image, 10, 50);

            image.dispose();//from  w  w w .  j  a  v  a  2 s .  c o m
            disable.dispose();
        }
    });

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

From source file:ImageGrayScale.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");

            Image disable = new Image(display, image, SWT.IMAGE_GRAY);

            e.gc.drawImage(disable, 10, 10);

            e.gc.drawImage(image, 10, 50);

            image.dispose();/*from ww  w. java  2 s .c  o m*/
            disable.dispose();
        }
    });

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

From source file:ImageCopyCreation.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");

            Image copy = new Image(display, image, SWT.IMAGE_COPY);

            e.gc.drawImage(copy, 10, 10);

            e.gc.drawImage(copy, 10, 50);

            image.dispose();/*from w  ww . j  a va  2s  . c o m*/
            copy.dispose();
        }
    });

    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 display = new Display();
    final Shell shell = new Shell(display);

    shell.setSize(250, 200);//from w  w w . ja v  a2s  .  co  m

    shell.setLayout(new FillLayout());

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

    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            e.gc.setFont(new Font(display, "Helvetica", 18, SWT.NORMAL));
            e.gc.drawText("www.java2s.com", 0, 0);
        }
    });

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

From source file:ImageLoadFromClassStream.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 = null;/*from ww  w  . j a  va 2s .  c  om*/
            try {
                image = new Image(display, ImageLoadFromClassStream.class.getResourceAsStream("yourFile.gif"));

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            e.gc.drawImage(image, 10, 10);

            image.dispose();
        }
    });

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