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:ImageEmpty.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, 300, 200);
            GC gc = new GC(image);
            gc.drawLine(10, 10, 200, 200);
            gc.dispose();//from w  w w  .j  a  va  2  s  . c o  m

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

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

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

    Button button = new Button(canvas, SWT.PUSH);
    button.setBounds(10, 10, 300, 40);//w  w w. j  av  a2 s  .  c  o m
    button.setText("You can place widgets on a canvas");

    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            Rectangle rect = ((Canvas) e.widget).getBounds();
            e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_RED));
            e.gc.drawFocus(5, 5, rect.width - 10, rect.height - 10);
            e.gc.drawText("You can draw text directly on a canvas", 60, 60);
        }
    });

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

From source file:FontDataConstruct.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 ww. j a  v a2s .c om
            e.gc.drawText("My Text", 0, 0);
            font.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 d = new Display();
    final Shell shell = new Shell(d);

    shell.setSize(250, 200);//w w  w . j  a  v  a2  s . 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.setLineWidth(10);
            e.gc.drawLine(0, 0, 100, 100);
        }
    });

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

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

            ImageData data = image.getImageData();

            System.out.println(data.height);

            e.gc.drawImage(image, 10, 10);
            image.dispose();//from  w w w  .ja  v a2  s  . co  m
        }
    });

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

From source file:CanvasKeyEvent.java

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

    shell.setLayout(new RowLayout());
    final Canvas canvas = new Canvas(shell, SWT.NULL);
    canvas.setSize(500, 500);/*from w ww .  j a va 2s  .c  o  m*/
    canvas.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));

    canvas.addKeyListener(new KeyListener() {
        public void keyPressed(KeyEvent e) {
            GC gc = new GC(canvas);
            Rectangle rect = canvas.getClientArea();
            gc.fillRectangle(rect.x, rect.y, rect.width, rect.height);

            Font font = new Font(display, "Arial", 32, SWT.BOLD);
            gc.setFont(font);

            gc.drawString("" + e.character, 15, 10);

            gc.dispose();
            font.dispose();
        }

        public void keyReleased(KeyEvent e) {
        }
    });

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

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  .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) {
            // Do some drawing
            Rectangle rect = ((Canvas) e.widget).getBounds();
            e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_RED));
            e.gc.drawFocus(5, 5, rect.width - 10, rect.height - 10);
            e.gc.drawText("www.java2s.com", 5, 50,
                    SWT.DRAW_MNEMONIC | SWT.DRAW_DELIMITER | SWT.DRAW_TAB | SWT.DRAW_TRANSPARENT);
        }
    });

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

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

            ImageData data = image.getImageData();

            data.setPixel(5, 5, 5);// w w  w .  j  a v  a  2s.  c  o  m

            Image anotherImage = new Image(display, data);
            e.gc.drawImage(anotherImage, 10, 10);
            image.dispose();
        }
    });

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

From source file:ImageRotate.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");
            ImageData sd = image.getImageData();

            ImageData dd = new ImageData(sd.height, sd.width, sd.depth, sd.palette);

            int style = SWT.UP;

            boolean up = (style & SWT.UP) == SWT.UP;

            // Run through the horizontal pixels
            for (int sx = 0; sx < sd.width; sx++) {
                // Run through the vertical pixels
                for (int sy = 0; sy < sd.height; sy++) {
                    // Determine where to move pixel to in destination image data
                    int dx = up ? sy : sd.height - sy - 1;
                    int dy = up ? sd.width - sx - 1 : sx;
                    // Swap the x, y source data to y, x in the destination
                    dd.setPixel(dx, dy, sd.getPixel(sx, sy));
                }//  w  w w. ja va 2  s .  c o m
            }

            // Create the vertical image
            Image vertical = new Image(display, dd);

            // Draw the vertical image onto the original GC
            e.gc.drawImage(vertical, 10, 10);

            vertical.dispose();
        }
    });

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

From source file:ImageLoadingFromFileInputStream.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;/*w  w w . j  a  v a  2s .c  om*/
            try {
                image = new Image(display, new FileInputStream("yourFile.gif"));
            } catch (FileNotFoundException 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();
}