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

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

Introduction

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

Prototype

public Point getSize() 

Source Link

Document

Returns a point describing the receiver's size.

Usage

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);/*from   ww  w  . j  av  a 2s  . 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: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  www  .j  a  va  2 s .c o m*/
        }
    });

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

From source file:org.eclipse.swt.snippets.Snippet367.java

public static void main(String[] args) {
    final ImageFileNameProvider filenameProvider = zoom -> {
        switch (zoom) {
        case 100:
            return IMAGE_PATH_100;
        case 150:
            return IMAGE_PATH_150;
        case 200:
            return IMAGE_PATH_200;
        default:/*from  ww w  .j a va 2s  .  c o  m*/
            return null;
        }
    };
    final ImageDataProvider imageDataProvider = zoom -> {
        switch (zoom) {
        case 100:
            return new ImageData(IMAGE_PATH_100);
        case 150:
            return new ImageData(IMAGE_PATH_150);
        case 200:
            return new ImageData(IMAGE_PATH_200);
        default:
            return null;
        }
    };

    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet367");
    shell.setLayout(new GridLayout(3, false));

    Menu menuBar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(menuBar);
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
    fileItem.setText("&File");
    Menu fileMenu = new Menu(menuBar);
    fileItem.setMenu(fileMenu);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
    exitItem.setText("&Exit");
    exitItem.addListener(SWT.Selection, e -> shell.close());

    new Label(shell, SWT.NONE).setText(IMAGE_200 + ":");
    new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_200));
    new Button(shell, SWT.PUSH).setImage(new Image(display, IMAGE_PATH_200));

    new Label(shell, SWT.NONE).setText(IMAGE_150 + ":");
    new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_150));
    new Button(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_150));

    new Label(shell, SWT.NONE).setText(IMAGE_100 + ":");
    new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100));
    new Button(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100));

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("ImageFileNameProvider:");
    new Label(shell, SWT.NONE).setImage(new Image(display, filenameProvider));
    new Button(shell, SWT.NONE).setImage(new Image(display, filenameProvider));

    new Label(shell, SWT.NONE).setText("ImageDataProvider:");
    new Label(shell, SWT.NONE).setImage(new Image(display, imageDataProvider));
    new Button(shell, SWT.NONE).setImage(new Image(display, imageDataProvider));

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("1. Canvas\n(PaintListener)");
    final Point size = new Point(550, 40);
    final Canvas canvas = new Canvas(shell, SWT.NONE);
    canvas.addPaintListener(e -> {
        Point size1 = canvas.getSize();
        paintImage(e.gc, size1);
    });
    GridData gridData = new GridData(size.x, size.y);
    gridData.horizontalSpan = 2;
    canvas.setLayoutData(gridData);

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("2. Painted image\n (default resolution)");
    Image image = new Image(display, size.x, size.y);
    GC gc = new GC(image);
    try {
        paintImage(gc, size);
    } finally {
        gc.dispose();
    }
    Label imageLabel = new Label(shell, SWT.NONE);
    imageLabel.setImage(image);
    imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("3. Painted image\n(multi-res, unzoomed paint)");
    imageLabel = new Label(shell, SWT.NONE);
    imageLabel.setImage(new Image(display, (ImageDataProvider) zoom -> {
        Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100);
        GC gc1 = new GC(temp);
        try {
            paintImage(gc1, size);
            return temp.getImageData();
        } finally {
            gc1.dispose();
            temp.dispose();
        }
    }));
    imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("4. Painted image\n(multi-res, zoomed paint)");
    imageLabel = new Label(shell, SWT.NONE);
    imageLabel.setImage(new Image(display, (ImageDataProvider) zoom -> {
        Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100);
        GC gc1 = new GC(temp);
        try {
            paintImage2(gc1, new Point(size.x * zoom / 100, size.y * zoom / 100), zoom / 100);
            return temp.getImageData();
        } finally {
            gc1.dispose();
            temp.dispose();
        }
    }));
    imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));

    createSeparator(shell);

    new Label(shell, SWT.NONE).setText("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")");
    Label box = new Label(shell, SWT.NONE);
    box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
    box.setLayoutData(new GridData(50, 50));

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

From source file:LineExample.java

/**
 * Creates the main window's contents/*w w  w  .  j  ava  2s. c o  m*/
 * 
 * @param shell the main window
 */
private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());

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

    // Add a handler to do the drawing
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            // Get the canvas and its size
            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 drawing color to blue
            e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLUE));

            // Set the width of the lines to draw
            e.gc.setLineWidth(10);

            // Draw a vertical line halfway across the canvas
            e.gc.drawLine(halfX, 0, halfX, maxY);

            // Draw a horizontal line halfway down the canvas
            e.gc.drawLine(0, halfY, maxX, halfY);
        }
    });
}

From source file:DrawTextDemo.java

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

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

    final Image image = new Image(display, "java2s.gif");
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            Rectangle size = image.getBounds();
            // Draws the background image.
            e.gc.drawImage(image, 0, 0, size.width, size.height, 0, 0, canvas.getSize().x, canvas.getSize().y);

            Font font = new Font(display, "Tahoma", 18, SWT.BOLD);
            e.gc.setFont(font);/* ww w.  j a v  a 2s . com*/
            e.gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
            e.gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

            String english = "SWT rocks!";
            String chinese = "\u4e2d\u6587\u6c49\u5b57\u6d4b\u8bd5";

            e.gc.drawString(english, 10, 10);
            e.gc.drawString(chinese, 10, 80, true);

            String text = "Text to be drawn in the center";
            Point textSize = e.gc.textExtent(text);
            e.gc.drawText(text, (canvas.getSize().x - textSize.x) / 2, (canvas.getSize().y - textSize.y) / 2);

            font.dispose();
        }
    });

    shell.setSize(300, 150);
    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();
}

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();
    //// www .  ja  va  2  s  .  c  o  m
    //            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();
}

From source file:BrowserExample.java

/**
 * Creates an instance of a ControlExample embedded inside the supplied
 * parent Composite./*  w ww.  j  a v a  2  s . c  om*/
 * 
 * @param parent
 *            the container of the example
 */
public BrowserExample(Composite parent) {

    final Display display = parent.getDisplay();
    FormLayout layout = new FormLayout();
    parent.setLayout(layout);
    ToolBar toolbar = new ToolBar(parent, SWT.NONE);
    final ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
    itemBack.setText(getResourceString("Back"));
    final ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
    itemForward.setText(getResourceString("Forward"));
    final ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
    itemStop.setText(getResourceString("Stop"));
    final ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
    itemRefresh.setText(getResourceString("Refresh"));
    final ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
    itemGo.setText(getResourceString("Go"));

    location = new Text(parent, SWT.BORDER);

    images = new Image[] { new Image(display, "java2s.gif") };

    final Canvas canvas = new Canvas(parent, SWT.NO_BACKGROUND);
    final Rectangle rect = images[0].getBounds();
    canvas.addListener(SWT.Paint, new Listener() {
        public void handleEvent(Event e) {
            Point pt = canvas.getSize();
            e.gc.drawImage(images[index], 0, 0, rect.width, rect.height, 0, 0, pt.x, pt.y);
        }
    });
    canvas.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            browser.setUrl(getResourceString("Startup"));
        }
    });

    display.asyncExec(new Runnable() {
        public void run() {
            if (canvas.isDisposed())
                return;
            if (busy) {
                index++;
                if (index == images.length)
                    index = 0;
                canvas.redraw();
            }
            display.timerExec(150, this);
        }
    });

    final Label status = new Label(parent, SWT.NONE);
    final ProgressBar progressBar = new ProgressBar(parent, SWT.NONE);

    FormData data = new FormData();
    data.top = new FormAttachment(0, 5);
    toolbar.setLayoutData(data);

    data = new FormData();
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    data.top = new FormAttachment(canvas, 5, SWT.DEFAULT);
    data.bottom = new FormAttachment(status, -5, SWT.DEFAULT);
    try {
        browser = new Browser(parent, SWT.NONE);
        browser.setLayoutData(data);
    } catch (SWTError e) {
        /* Browser widget could not be instantiated */
        Label label = new Label(parent, SWT.CENTER | SWT.WRAP);
        label.setText(getResourceString("BrowserNotCreated"));
        label.setLayoutData(data);
    }

    data = new FormData();
    data.width = 24;
    data.height = 24;
    data.top = new FormAttachment(0, 5);
    data.right = new FormAttachment(100, -5);
    canvas.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(toolbar, 0, SWT.TOP);
    data.left = new FormAttachment(toolbar, 5, SWT.RIGHT);
    data.right = new FormAttachment(canvas, -5, SWT.DEFAULT);
    location.setLayoutData(data);

    data = new FormData();
    data.left = new FormAttachment(0, 5);
    data.right = new FormAttachment(progressBar, 0, SWT.DEFAULT);
    data.bottom = new FormAttachment(100, -5);
    status.setLayoutData(data);

    data = new FormData();
    data.right = new FormAttachment(100, -5);
    data.bottom = new FormAttachment(100, -5);
    progressBar.setLayoutData(data);

    if (browser != null) {
        itemBack.setEnabled(browser.isBackEnabled());
        itemForward.setEnabled(browser.isForwardEnabled());

        Listener listener = new Listener() {
            public void handleEvent(Event event) {
                ToolItem item = (ToolItem) event.widget;
                if (item == itemBack)
                    browser.back();
                else if (item == itemForward)
                    browser.forward();
                else if (item == itemStop)
                    browser.stop();
                else if (item == itemRefresh)
                    browser.refresh();
                else if (item == itemGo)
                    browser.setUrl(location.getText());
            }
        };
        browser.addLocationListener(new LocationListener() {
            public void changed(LocationEvent event) {
                busy = true;
                if (event.top)
                    location.setText(event.location);
            }

            public void changing(LocationEvent event) {
            }
        });
        browser.addProgressListener(new ProgressListener() {
            public void changed(ProgressEvent event) {
                if (event.total == 0)
                    return;
                int ratio = event.current * 100 / event.total;
                progressBar.setSelection(ratio);
                busy = event.current != event.total;
                if (!busy) {
                    index = 0;
                    canvas.redraw();
                }
            }

            public void completed(ProgressEvent event) {
                itemBack.setEnabled(browser.isBackEnabled());
                itemForward.setEnabled(browser.isForwardEnabled());
                progressBar.setSelection(0);
                busy = false;
                index = 0;
                canvas.redraw();
            }
        });
        browser.addStatusTextListener(new StatusTextListener() {
            public void changed(StatusTextEvent event) {
                status.setText(event.text);
            }
        });
        if (parent instanceof Shell) {
            final Shell shell = (Shell) parent;
            browser.addTitleListener(new TitleListener() {
                public void changed(TitleEvent event) {
                    shell.setText(event.title + " - " + getResourceString("window.title"));
                }
            });
        }
        itemBack.addListener(SWT.Selection, listener);
        itemForward.addListener(SWT.Selection, listener);
        itemStop.addListener(SWT.Selection, listener);
        itemRefresh.addListener(SWT.Selection, listener);
        itemGo.addListener(SWT.Selection, listener);
        location.addListener(SWT.DefaultSelection, new Listener() {
            public void handleEvent(Event e) {
                browser.setUrl(location.getText());
            }
        });

        initialize(display, browser);
        browser.setUrl(getResourceString("Startup"));
    }
}