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:DrawFocusRectangle.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) {
            e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_RED));
            e.gc.drawFocus(5, 5, 200, 10);
        }/*w w w.ja  v a2 s . co  m*/
    });

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

From source file:DrawStringLineTabSWT.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) {
            e.gc.drawString("www.\njava2s\t.com", 5, 5);
        }//w w w .ja v  a2  s . c o m
    });

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

From source file:GCColorChange.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) {

            e.gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
            e.gc.drawText("I'm in blue!", 20, 20);
        }/*ww w  .jav  a  2 s.  c o  m*/
    });

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

From source file:DrawingProcessNewLine.java

public static void main(String[] args) {
    final 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) {
            e.gc.drawText("www.\njava2s\t.com", 5, 5, true);
            e.gc.drawText("www.\njava2s\t.com", 5, 55, false);

        }/*w  w w  . ja  va2  s.c o  m*/
    });

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

From source file:FontSystemGettingSWT.java

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

            e.gc.setFont(display.getSystemFont());

            e.gc.drawText(display.getSystemFont().getFontData()[0].getName(), 5, 5);
        }//from   ww w  .  j  a v  a 2s  .com
    });

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

From source file:DrawPointLineWidth.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 maxX = canvas.getSize().x;
            int maxY = canvas.getSize().y;

            int halfX = (int) maxX / 2;
            int halfY = (int) maxY / 2;

            e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLUE));
            e.gc.setLineWidth(10);/*  ww  w.j ava  2  s. c o m*/
            e.gc.drawLine(halfX, 0, halfX, maxY);
            e.gc.drawLine(0, halfY, maxX, halfY);
        }
    });

    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 www  . j a  v  a 2s  .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.drawRoundRectangle(10, 10, 200, 200, 30, 60);

        }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!d.readAndDispatch())
            d.sleep();
    }
    d.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);/*w w  w.  j ava 2  s  . c om*/

    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.setForeground(e.display.getSystemColor(SWT.COLOR_BLACK));
            e.gc.drawOval(100, 20, 100, 50);

        }
    });

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

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 a2 s  . com
    });

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

From source file:SineFunctionPlotting.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 maxX = canvas.getSize().x;
            int maxY = canvas.getSize().y;

            // Calculate the middle
            int halfX = (int) maxX / 2;
            int halfY = (int) maxY / 2;

            // Set the line color and draw a horizontal axis
            e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLACK));
            e.gc.drawLine(0, halfY, maxX, halfY);

            // Draw the sine wave
            for (int i = 0; i < maxX; i++) {
                e.gc.drawPoint(i, getNormalizedSine(i, halfY, maxX));
            }/*from w  w w  .  j a  v  a  2s  .  c om*/
        }
    });

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