/** Creates the "Example" widgets. */ void createExampleWidgets() { /* Compute the widget style */ int style = getDefaultStyle(); if (horizontalButton.getSelection()) style |= SWT.H_SCROLL; if (verticalButton.getSelection()) style |= SWT.V_SCROLL; if (borderButton.getSelection()) style |= SWT.BORDER; if (noBackgroundButton.getSelection()) style |= SWT.NO_BACKGROUND; if (noFocusButton.getSelection()) style |= SWT.NO_FOCUS; if (noMergePaintsButton.getSelection()) style |= SWT.NO_MERGE_PAINTS; if (noRedrawResizeButton.getSelection()) style |= SWT.NO_REDRAW_RESIZE; /* Create the example widgets */ paintCount = 0; cx = 0; cy = 0; canvas = new Canvas(canvasGroup, style); canvas.addPaintListener( new PaintListener() { public void paintControl(PaintEvent e) { paintCount++; GC gc = e.gc; if (fillDamageButton.getSelection()) { Color color = e.display.getSystemColor(colors[paintCount % colors.length]); gc.setBackground(color); gc.fillRectangle(e.x, e.y, e.width, e.height); } Point size = canvas.getSize(); gc.drawArc(cx + 1, cy + 1, size.x - 2, size.y - 2, 0, 360); gc.drawRectangle(cx + (size.x - 10) / 2, cy + (size.y - 10) / 2, 10, 10); } }); canvas.addControlListener( new ControlAdapter() { public void controlResized(ControlEvent event) { Point size = canvas.getSize(); maxX = size.x * 3 / 2; maxY = size.y * 3 / 2; resizeScrollBars(); } }); ScrollBar bar = canvas.getHorizontalBar(); if (bar != null) { bar.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scrollHorizontal((ScrollBar) event.widget); } }); } bar = canvas.getVerticalBar(); if (bar != null) { bar.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scrollVertical((ScrollBar) event.widget); } }); } }
/** * Scrolls the canvas vertically. * * @param scrollBar */ void scrollVertical(ScrollBar scrollBar) { Rectangle bounds = canvas.getClientArea(); int y = -scrollBar.getSelection(); if (y + maxY < bounds.height) { y = bounds.height - maxY; } canvas.scroll(cx, y, cx, cy, maxX, maxY, false); cy = y; }
/** * Scrolls the canvas horizontally. * * @param scrollBar */ void scrollHorizontal(ScrollBar scrollBar) { Rectangle bounds = canvas.getClientArea(); int x = -scrollBar.getSelection(); if (x + maxX < bounds.width) { x = bounds.width - maxX; } canvas.scroll(x, cy, cx, cy, maxX, maxY, false); cx = x; }
public static void scribbleTab() { TabItem tab = new TabItem(folder, SWT.CLOSE); tab.setText("Scribble"); tab.setToolTipText("Simple graphics: drawing"); final Canvas canvas = new Canvas(folder, SWT.NONE); ScribbleMouseListener sml = new ScribbleMouseListener(); canvas.addMouseListener(sml); canvas.addMouseMoveListener(sml); tab.setControl(canvas); }
/** Resizes the maximum and thumb of both scrollbars. */ void resizeScrollBars() { Rectangle clientArea = canvas.getClientArea(); ScrollBar bar = canvas.getHorizontalBar(); if (bar != null) { bar.setMaximum(maxX); bar.setThumb(clientArea.width); bar.setPageIncrement(clientArea.width); } bar = canvas.getVerticalBar(); if (bar != null) { bar.setMaximum(maxY); bar.setThumb(clientArea.height); bar.setPageIncrement(clientArea.height); } }
/** Sets or clears the caret in the "Example" widget. */ void setCaret() { Caret oldCaret = canvas.getCaret(); if (caretButton.getSelection()) { Caret newCaret = new Caret(canvas, SWT.NONE); Font font = canvas.getFont(); newCaret.setFont(font); GC gc = new GC(canvas); gc.setFont(font); newCaret.setBounds(1, 1, 1, gc.getFontMetrics().getHeight()); gc.dispose(); canvas.setCaret(newCaret); canvas.setFocus(); } else { canvas.setCaret(null); } if (oldCaret != null) oldCaret.dispose(); }
/** Initialize the canvas in the appropriate part of the window */ public void initCanvas() { canvas = new Canvas(shell, SWT.NO_BACKGROUND | SWT.BORDER); GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; canvas.setLayoutData(gridData); }
/** Sets the state of the "Example" widgets. */ void setExampleWidgetState() { super.setExampleWidgetState(); horizontalButton.setSelection((canvas.getStyle() & SWT.H_SCROLL) != 0); verticalButton.setSelection((canvas.getStyle() & SWT.V_SCROLL) != 0); borderButton.setSelection((canvas.getStyle() & SWT.BORDER) != 0); noBackgroundButton.setSelection((canvas.getStyle() & SWT.NO_BACKGROUND) != 0); noFocusButton.setSelection((canvas.getStyle() & SWT.NO_FOCUS) != 0); noMergePaintsButton.setSelection((canvas.getStyle() & SWT.NO_MERGE_PAINTS) != 0); noRedrawResizeButton.setSelection((canvas.getStyle() & SWT.NO_REDRAW_RESIZE) != 0); setCaret(); }
public static void main(String[] args) { final ImageFileNameProvider filenameProvider = new ImageFileNameProvider() { @Override public String getImagePath(int zoom) { switch (zoom) { case 150: return IMAGE_PATH_150; case 200: return IMAGE_PATH_200; default: return IMAGE_PATH_100; } } }; final ImageDataProvider imageDataProvider = new ImageDataProvider() { @Override public ImageData getImageData(int zoom) { switch (zoom) { case 150: return new ImageData(IMAGE_PATH_150); case 200: return new ImageData(IMAGE_PATH_200); default: return new ImageData(IMAGE_PATH_100); } } }; final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new GridLayout(2, false)); new Label(shell, SWT.NONE).setText(IMAGE_200 + ":"); new Label(shell, SWT.NONE).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 Label(shell, SWT.NONE).setText(IMAGE_100 + ":"); new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100)); new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); new Label(shell, SWT.NONE).setText("ImageFileNameProvider:"); new Label(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 Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); new Label(shell, SWT.NONE).setText("Canvas\n(PaintListener)"); final Point size = new Point(550, 35); final Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener( new PaintListener() { @Override public void paintControl(PaintEvent e) { Point size = canvas.getSize(); paintImage(e.gc, size); } }); canvas.setLayoutData(new GridData(size.x, size.y)); new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); new Label(shell, SWT.NONE).setText("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(); } new Label(shell, SWT.NONE).setImage(image); new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); new Label(shell, SWT.NONE).setText("Painted image\n(multi-res, unzoomed paint)"); new Label(shell, SWT.NONE) .setImage( new Image( display, new ImageDataProvider() { @Override public ImageData getImageData(int zoom) { Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100); GC gc = new GC(temp); try { paintImage(gc, size); return temp.getImageData(); } finally { gc.dispose(); temp.dispose(); } } })); new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); new Label(shell, SWT.NONE).setText("Painted image\n(multi-res, zoomed paint)"); new Label(shell, SWT.NONE) .setImage( new Image( display, new ImageDataProvider() { @Override public ImageData getImageData(int zoom) { Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100); GC gc = new GC(temp); try { paintImage2( gc, new Point(size.x * zoom / 100, size.y * zoom / 100), zoom / 100); return temp.getImageData(); } finally { gc.dispose(); temp.dispose(); } } })); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }