List of usage examples for org.eclipse.swt.widgets Canvas redraw
public void redraw()
From source file:BrowserExample.java
/** * Creates an instance of a ControlExample embedded inside the supplied * parent Composite./* w w w . j a v a 2 s . co m*/ * * @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")); } }
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(); //// w w w.j a v a 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:Extents.java
/** * Creates the main window's contents//from w ww . j a 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:org.eclipse.swt.examples.clipboard.ClipboardExample.java
void createImageTransfer(Composite copyParent, Composite pasteParent) { final Image[] copyImage = new Image[] { null }; Label l = new Label(copyParent, SWT.NONE); l.setText("ImageTransfer:"); //$NON-NLS-1$ GridData data = new GridData(); data.verticalSpan = 2;/*w ww . ja va 2 s. c o m*/ l.setLayoutData(data); final Canvas copyImageCanvas = new Canvas(copyParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); data = new GridData(GridData.FILL_BOTH); data.verticalSpan = 2; data.widthHint = HSIZE; data.heightHint = VSIZE; copyImageCanvas.setLayoutData(data); final Point copyOrigin = new Point(0, 0); final ScrollBar copyHBar = copyImageCanvas.getHorizontalBar(); copyHBar.setEnabled(false); copyHBar.addListener(SWT.Selection, e -> { if (copyImage[0] != null) { int hSelection = copyHBar.getSelection(); int destX = -hSelection - copyOrigin.x; Rectangle rect = copyImage[0].getBounds(); copyImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false); copyOrigin.x = -hSelection; } }); final ScrollBar copyVBar = copyImageCanvas.getVerticalBar(); copyVBar.setEnabled(false); copyVBar.addListener(SWT.Selection, e -> { if (copyImage[0] != null) { int vSelection = copyVBar.getSelection(); int destY = -vSelection - copyOrigin.y; Rectangle rect = copyImage[0].getBounds(); copyImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false); copyOrigin.y = -vSelection; } }); copyImageCanvas.addListener(SWT.Paint, e -> { if (copyImage[0] != null) { GC gc = e.gc; gc.drawImage(copyImage[0], copyOrigin.x, copyOrigin.y); Rectangle rect = copyImage[0].getBounds(); Rectangle client = copyImageCanvas.getClientArea(); int marginWidth = client.width - rect.width; if (marginWidth > 0) { gc.fillRectangle(rect.width, 0, marginWidth, client.height); } int marginHeight = client.height - rect.height; if (marginHeight > 0) { gc.fillRectangle(0, rect.height, client.width, marginHeight); } gc.dispose(); } }); Button openButton = new Button(copyParent, SWT.PUSH); openButton.setText("Open Image"); openButton.addSelectionListener(widgetSelectedAdapter(e -> { FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Open an image file or cancel"); String string = dialog.open(); if (string != null) { if (copyImage[0] != null) { System.out.println("CopyImage"); copyImage[0].dispose(); } copyImage[0] = new Image(e.display, string); copyVBar.setEnabled(true); copyHBar.setEnabled(true); copyOrigin.x = 0; copyOrigin.y = 0; Rectangle rect = copyImage[0].getBounds(); Rectangle client = copyImageCanvas.getClientArea(); copyHBar.setMaximum(rect.width); copyVBar.setMaximum(rect.height); copyHBar.setThumb(Math.min(rect.width, client.width)); copyVBar.setThumb(Math.min(rect.height, client.height)); copyImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true); copyVBar.setSelection(0); copyHBar.setSelection(0); copyImageCanvas.redraw(); } })); Button b = new Button(copyParent, SWT.PUSH); b.setText("Copy"); b.addSelectionListener(widgetSelectedAdapter(e -> { if (copyImage[0] != null) { status.setText(""); // Fetch ImageData at current zoom and save in the clip-board. clipboard.setContents(new Object[] { copyImage[0].getImageDataAtCurrentZoom() }, new Transfer[] { ImageTransfer.getInstance() }); } else { status.setText("No image to copy"); } })); final Image[] pasteImage = new Image[] { null }; l = new Label(pasteParent, SWT.NONE); l.setText("ImageTransfer:"); final Canvas pasteImageCanvas = new Canvas(pasteParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); data = new GridData(GridData.FILL_BOTH); data.widthHint = HSIZE; data.heightHint = VSIZE; pasteImageCanvas.setLayoutData(data); final Point pasteOrigin = new Point(0, 0); final ScrollBar pasteHBar = pasteImageCanvas.getHorizontalBar(); pasteHBar.setEnabled(false); pasteHBar.addListener(SWT.Selection, e -> { if (pasteImage[0] != null) { int hSelection = pasteHBar.getSelection(); int destX = -hSelection - pasteOrigin.x; Rectangle rect = pasteImage[0].getBounds(); pasteImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false); pasteOrigin.x = -hSelection; } }); final ScrollBar pasteVBar = pasteImageCanvas.getVerticalBar(); pasteVBar.setEnabled(false); pasteVBar.addListener(SWT.Selection, e -> { if (pasteImage[0] != null) { int vSelection = pasteVBar.getSelection(); int destY = -vSelection - pasteOrigin.y; Rectangle rect = pasteImage[0].getBounds(); pasteImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false); pasteOrigin.y = -vSelection; } }); pasteImageCanvas.addListener(SWT.Paint, e -> { if (pasteImage[0] != null) { GC gc = e.gc; gc.drawImage(pasteImage[0], pasteOrigin.x, pasteOrigin.y); Rectangle rect = pasteImage[0].getBounds(); Rectangle client = pasteImageCanvas.getClientArea(); int marginWidth = client.width - rect.width; if (marginWidth > 0) { gc.fillRectangle(rect.width, 0, marginWidth, client.height); } int marginHeight = client.height - rect.height; if (marginHeight > 0) { gc.fillRectangle(0, rect.height, client.width, marginHeight); } } }); b = new Button(pasteParent, SWT.PUSH); b.setText("Paste"); b.addSelectionListener(widgetSelectedAdapter(e -> { ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance()); if (imageData != null) { if (pasteImage[0] != null) { System.out.println("PasteImage"); pasteImage[0].dispose(); } status.setText(""); // Consume the ImageData at current zoom as-is. pasteImage[0] = new Image(e.display, new AutoScaleImageDataProvider(imageData)); pasteVBar.setEnabled(true); pasteHBar.setEnabled(true); pasteOrigin.x = 0; pasteOrigin.y = 0; Rectangle rect = pasteImage[0].getBounds(); Rectangle client = pasteImageCanvas.getClientArea(); pasteHBar.setMaximum(rect.width); pasteVBar.setMaximum(rect.height); pasteHBar.setThumb(Math.min(rect.width, client.width)); pasteVBar.setThumb(Math.min(rect.height, client.height)); pasteImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true); pasteVBar.setSelection(0); pasteHBar.setSelection(0); pasteImageCanvas.redraw(); } else { status.setText("No image to paste"); } })); }