List of usage examples for org.eclipse.swt.widgets Canvas Canvas
public Canvas(Composite parent, int style)
From source file:SimpleCanvas.java
/** * Creates the main window's contents//from w w w . ja v a 2 s . com * * @param shell the main window */ private void createContents(Shell shell) { shell.setLayout(new FillLayout()); // Create a canvas Canvas canvas = new Canvas(shell, SWT.NONE); // Create a button on the canvas Button button = new Button(canvas, SWT.PUSH); button.setBounds(10, 10, 300, 40); button.setText("You can place widgets on a canvas"); // Create a paint handler for the canvas 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("You can draw text directly on a canvas", 60, 60); } }); }
From source file:PointExample.java
/** * Creates the main window's contents//from w w w. j av a 2 s .c o m * * @param shell the main window */ private void createContents(Shell shell) { shell.setLayout(new FillLayout()); // Create the canvas for drawing on Canvas canvas = new Canvas(shell, SWT.NONE); // Add the paint handler to draw the sine wave canvas.addPaintListener(new PointExamplePaintListener()); // Use a white background canvas.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE)); }
From source file:PolygonExample.java
/** * Creates the main window's contents//from w w w . j a v a 2 s .co m * * @param shell the main window */ private void createContents(Shell shell) { shell.setLayout(new FillLayout(SWT.VERTICAL)); // Create the canvas to draw the polygons on Canvas drawingCanvas = new Canvas(shell, SWT.NONE); drawingCanvas.addPaintListener(new PolygonExamplePaintListener()); }
From source file:Draw2DSample.java
protected Control createContents(Composite parent) { Composite composite = new Composite(parent, SWT.NULL); composite.setLayout(new FillLayout()); Canvas canvas = new Canvas(composite, SWT.NULL); LightweightSystem lws = new LightweightSystem(canvas); Button button = new Button("Button", new Image(getShell().getDisplay(), "java2s.gif")); lws.setContents(button);/*from w w w . ja v a 2 s.c o m*/ return composite; }
From source file:Animations.java
public Animations() { shell.setLayout(new FillLayout()); ImageLoader imageLoader = new ImageLoader(); final ImageData[] imageDatas = imageLoader.load("java2s.gif"); final Image image = new Image(display, imageDatas[0].width, imageDatas[0].height); final Canvas canvas = new Canvas(shell, SWT.NULL); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawImage(image, 0, 0); }//from w ww. j av a 2 s . co m }); final GC gc = new GC(image); final Thread thread = new Thread() { int frameIndex = 0; public void run() { while (!isInterrupted()) { frameIndex %= imageDatas.length; final ImageData frameData = imageDatas[frameIndex]; display.asyncExec(new Runnable() { public void run() { Image frame = new Image(display, frameData); gc.drawImage(frame, frameData.x, frameData.y); frame.dispose(); canvas.redraw(); } }); try { // delay Thread.sleep(imageDatas[frameIndex].delayTime * 10); } catch (InterruptedException e) { return; } frameIndex += 1; } } }; shell.addShellListener(new ShellAdapter() { public void shellClosed(ShellEvent e) { thread.interrupt(); } }); shell.setSize(400, 200); shell.open(); thread.start(); // 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:Extents.java
/** * Creates the main window's contents/*from w ww . ja v a 2 s. co m*/ * * @param shell the main window */ private void createContents(final Shell shell) { // Create a canvas to draw on final Canvas canvas = new Canvas(shell, SWT.NONE); // Add a listener to the shell to resize the canvas to fill the window // any time the window is resized shell.addControlListener(new ControlAdapter() { public void controlResized(ControlEvent event) { canvas.setBounds(shell.getClientArea()); } }); // Add a listener to the canvas. This is where we draw the text. canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent event) { // Set the font into the gc event.gc.setFont(font); // Calcalute the width (nad height) of the string Point pt = event.gc.stringExtent(STRING); // Figure out how big our drawing area is Rectangle rect = canvas.getBounds(); // Calculate the height of the font. We could have used pt.y, // but this demonstrates FontMetrics int height = event.gc.getFontMetrics().getHeight(); // Outside loop goes from the top of the window to the bottom. // Since the (x, y) passed to drawString represents the upper left // corner, subtract the height of the font from the height of the // drawing area, so we don't have any partial drawing. for (int i = 0, n = rect.height - height; i < n; i += height) { // Inside loop goes from the left to the right, stopping far enough // from the right to ensure no partial string drawing. for (int j = 0, m = rect.width - pt.x; j < m; j += pt.x) { // Draw the string event.gc.drawString(STRING, j, i); } } } }); // 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); // Add a listener to the combo, so that when the selection changes, // we change the font and redraw the canvas 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); }
From source file:RoundRectangleExample.java
/** * Creates the main window's contents//www .j ava 2 s . co m * * @param shell the main window */ private void createContents(Shell shell) { shell.setLayout(new FillLayout(SWT.VERTICAL)); // Create the composite that holds the input fields Composite widgetComposite = new Composite(shell, SWT.NONE); widgetComposite.setLayout(new GridLayout(2, false)); // Create the input fields new Label(widgetComposite, SWT.NONE).setText("Arc Width:"); txtArcWidth = new Text(widgetComposite, SWT.BORDER); new Label(widgetComposite, SWT.NONE).setText("Arc Height"); txtArcHeight = new Text(widgetComposite, SWT.BORDER); // Create the button that launches the redraw Button button = new Button(widgetComposite, SWT.PUSH); button.setText("Redraw"); shell.setDefaultButton(button); // Create the canvas to draw the round rectangle on final Canvas drawingCanvas = new Canvas(shell, SWT.NONE); drawingCanvas.addPaintListener(new RoundRectangleExamplePaintListener()); // Add a handler to redraw the round rectangle when pressed button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { drawingCanvas.redraw(); } }); }
From source file:Animator.java
/** * Creates the main window's contents//from w ww . j a va 2 s. com * * @param shell the main window */ private void createContents(final Shell shell) { shell.setLayout(new FillLayout()); // Create the canvas for drawing canvas = new Canvas(shell, SWT.NO_BACKGROUND); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent event) { // Draw the background event.gc.fillRectangle(canvas.getBounds()); // Set the color of the ball event.gc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED)); // Draw the ball event.gc.fillOval(x, y, IMAGE_WIDTH, IMAGE_WIDTH); } }); }
From source file:AnimatorDoubleBuffer.java
/** * Creates the main window's contents//from www . j a v a2s. c o m * * @param shell the main window */ private void createContents(final Shell shell) { shell.setLayout(new FillLayout()); // Create the canvas for drawing canvas = new Canvas(shell, SWT.NO_BACKGROUND); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent event) { // Create the image to fill the canvas Image image = new Image(shell.getDisplay(), canvas.getBounds()); // Set up the offscreen gc GC gcImage = new GC(image); // Draw the background gcImage.setBackground(event.gc.getBackground()); gcImage.fillRectangle(image.getBounds()); // Set the color of the ball gcImage.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED)); // Draw the ball gcImage.fillOval(x, y, IMAGE_WIDTH, IMAGE_WIDTH); // Draw the offscreen buffer to the screen event.gc.drawImage(image, 0, 0); // Clean up image.dispose(); gcImage.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(); ////from ww w . ja v a 2 s.co 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(); }