List of usage examples for org.eclipse.swt.widgets Canvas setLayoutData
public void setLayoutData(Object layoutData)
From source file:org.eclipse.swt.snippets.Snippet286.java
public static void main(java.lang.String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 286"); shell.setLayout(new GridLayout()); Canvas blankCanvas = new Canvas(shell, SWT.BORDER); blankCanvas.setLayoutData(new GridData(200, 200)); final Label statusLine = new Label(shell, SWT.NONE); statusLine.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); Menu bar = new Menu(shell, SWT.BAR); shell.setMenuBar(bar);//from ww w . j av a2 s.co m MenuItem menuItem = new MenuItem(bar, SWT.CASCADE); menuItem.setText("Test"); Menu menu = new Menu(bar); menuItem.setMenu(menu); for (int i = 0; i < 5; i++) { MenuItem item = new MenuItem(menu, SWT.PUSH); item.setText("Item " + i); item.addArmListener(e -> statusLine.setText(((MenuItem) e.getSource()).getText())); } shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet338.java
public static void main(String[] args) { Display display = new Display(); Shell parentShell = new Shell(display); parentShell.setText("Snippet 338"); parentShell.setBounds(10, 10, 100, 100); parentShell.open();//from w w w. j a va 2s. co m Shell childShell = new Shell(parentShell); childShell.setLayout(new GridLayout()); TabFolder folder = new TabFolder(childShell, SWT.NONE); folder.setLayout(new FillLayout()); TabItem tab1 = new TabItem(folder, SWT.NONE); tab1.setText("Tab &1"); new TabItem(folder, SWT.NONE).setText("Tab &2"); Composite composite = new Composite(folder, SWT.NONE); composite.setLayout(new GridLayout()); tab1.setControl(composite); Text text1 = new Text(composite, SWT.SINGLE); /* canvas represents a custom control */ final Canvas canvas = new Canvas(composite, SWT.BORDER); canvas.setLayoutData(new GridData(300, 200)); canvas.addListener(SWT.Paint, event -> { if (canvas.isFocusControl()) { event.gc.drawText( "focus is here, custom traverse keys:\n\tN: Tab next\n\tP: Tab previous\n\tR: Return\n\tE: Esc\n\tT: Tab Folder next page", 0, 0); } else { event.gc.drawString("focus is not in this control", 0, 0); } }); canvas.addListener(SWT.KeyDown, event -> { int traversal = SWT.NONE; switch (event.keyCode) { case 'n': traversal = SWT.TRAVERSE_TAB_NEXT; break; case 'p': traversal = SWT.TRAVERSE_TAB_PREVIOUS; break; case 'r': traversal = SWT.TRAVERSE_RETURN; break; case 'e': traversal = SWT.TRAVERSE_ESCAPE; break; case 't': traversal = SWT.TRAVERSE_PAGE_NEXT; break; } if (traversal != SWT.NONE) { event.doit = true; /* this will be the Traverse event's initial doit value */ canvas.traverse(traversal, event); } }); canvas.addFocusListener(focusLostAdapter(e -> canvas.redraw())); canvas.addFocusListener(focusGainedAdapter(e -> canvas.redraw())); Text text2 = new Text(composite, SWT.SINGLE); Button button = new Button(childShell, SWT.PUSH); button.setText("Default &Button"); button.addListener(SWT.Selection, event -> System.out.println("Default button selected")); childShell.setDefaultButton(button); Listener printTraverseListener = event -> { StringBuilder buffer = new StringBuilder("Traverse "); buffer.append(event.widget); buffer.append(" type="); switch (event.detail) { case SWT.TRAVERSE_ARROW_NEXT: buffer.append("TRAVERSE_ARROW_NEXT"); break; case SWT.TRAVERSE_ARROW_PREVIOUS: buffer.append("TRAVERSE_ARROW_NEXT"); break; case SWT.TRAVERSE_ESCAPE: buffer.append("TRAVERSE_ESCAPE"); break; case SWT.TRAVERSE_MNEMONIC: buffer.append("TRAVERSE_MNEMONIC"); break; case SWT.TRAVERSE_PAGE_NEXT: buffer.append("TRAVERSE_PAGE_NEXT"); break; case SWT.TRAVERSE_PAGE_PREVIOUS: buffer.append("TRAVERSE_PAGE_PREVIOUS"); break; case SWT.TRAVERSE_RETURN: buffer.append("TRAVERSE_RETURN"); break; case SWT.TRAVERSE_TAB_NEXT: buffer.append("TRAVERSE_TAB_NEXT"); break; case SWT.TRAVERSE_TAB_PREVIOUS: buffer.append("TRAVERSE_TAB_PREVIOUS"); break; } buffer.append(" doit=" + event.doit); buffer.append(" keycode=" + event.keyCode); buffer.append(" char=" + event.character); buffer.append(" stateMask=" + event.stateMask); System.out.println(buffer.toString()); }; childShell.addListener(SWT.Traverse, printTraverseListener); folder.addListener(SWT.Traverse, printTraverseListener); composite.addListener(SWT.Traverse, printTraverseListener); canvas.addListener(SWT.Traverse, printTraverseListener); button.addListener(SWT.Traverse, printTraverseListener); text1.addListener(SWT.Traverse, printTraverseListener); text2.addListener(SWT.Traverse, printTraverseListener); childShell.pack(); childShell.open(); text1.setFocus(); while (!parentShell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:SWTTest.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setSize(350, 350);/*from ww w .ja va 2s .co m*/ shell.setLayout(new GridLayout()); Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND); GridData data = new GridData(GridData.FILL_BOTH); canvas.setLayoutData(data); final Graphics2DRenderer renderer = new Graphics2DRenderer(); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { Point controlSize = ((Control) e.getSource()).getSize(); GC gc = e.gc; // gets the SWT graphics context from the event renderer.prepareRendering(gc); // prepares the Graphics2D // renderer // gets the Graphics2D context and switch on the antialiasing Graphics2D g2d = renderer.getGraphics2D(); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // paints the background with a color gradient g2d.setPaint(new GradientPaint(0.0f, 0.0f, java.awt.Color.yellow, (float) controlSize.x, (float) controlSize.y, java.awt.Color.white)); g2d.fillRect(0, 0, controlSize.x, controlSize.y); // draws rotated text g2d.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 16)); g2d.setColor(java.awt.Color.blue); g2d.translate(controlSize.x / 2, controlSize.y / 2); int nbOfSlices = 18; for (int i = 0; i < nbOfSlices; i++) { g2d.drawString("Angle = " + (i * 360 / nbOfSlices) + "\u00B0", 30, 0); g2d.rotate(-2 * Math.PI / nbOfSlices); } // now that we are done with Java2D, renders Graphics2D // operation // on the SWT graphics context renderer.render(gc); // now we can continue with pure SWT paint operations gc.drawOval(0, 0, controlSize.x, controlSize.y); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); renderer.dispose(); System.exit(0); }
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 w w w.ja v a 2 s . 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:org.eclipse.swt.snippets.Snippet341.java
static void capture(GLCanvas glCanvas) { final int PAD = 4; Display display = glCanvas.getDisplay(); Rectangle bounds = glCanvas.getBounds(); int size = bounds.width * PAD * bounds.height; ByteBuffer buffer = ByteBuffer.allocateDirect(size); GL11.glReadPixels(0, 0, bounds.width, bounds.height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); byte[] bytes = new byte[size]; buffer.get(bytes);//from w ww . j a v a 2 s .c o m /* * In OpenGL (0,0) is at the bottom-left corner, and y values ascend in the upward * direction. This is opposite to SWT which defines (0,0) to be at the top-left * corner with y values ascendingin the downwards direction. Re-order the OpenGL- * provided bytes to SWT's expected order so that the Image will not appear inverted. */ byte[] temp = new byte[bytes.length]; for (int i = 0; i < bytes.length; i += bounds.width * PAD) { System.arraycopy(bytes, bytes.length - i - bounds.width * PAD, temp, i, bounds.width * PAD); } bytes = temp; ImageData data = new ImageData(bounds.width, bounds.height, 32, new PaletteData(0xFF000000, 0xFF0000, 0xFF00), PAD, bytes); final Image image = new Image(display, data); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); Canvas canvas = new Canvas(shell, SWT.NONE); canvas.setLayoutData(new GridData(bounds.width, bounds.height)); canvas.addListener(SWT.Paint, event -> event.gc.drawImage(image, 0, 0)); shell.pack(); shell.open(); shell.addListener(SWT.Dispose, event -> image.dispose()); }
From source file:org.eclipse.swt.examples.paint.PaintExample.java
/** * Creates the GUI.//from w w w .j av a2 s .c o m */ public void createGUI(Composite parent) { GridLayout gridLayout; GridData gridData; /*** Create principal GUI layout elements ***/ Composite displayArea = new Composite(parent, SWT.NONE); gridLayout = new GridLayout(); gridLayout.numColumns = 1; displayArea.setLayout(gridLayout); // Creating these elements here avoids the need to instantiate the GUI elements // in strict layout order. The natural layout ordering is an artifact of using // SWT layouts, but unfortunately it is not the same order as that required to // instantiate all of the non-GUI application elements to satisfy referential // dependencies. It is possible to reorder the initialization to some extent, but // this can be very tedious. // paint canvas final Canvas paintCanvas = new Canvas(displayArea, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND); gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL); paintCanvas.setLayoutData(gridData); paintCanvas.setBackground(paintColorWhite); // color selector frame final Composite colorFrame = new Composite(displayArea, SWT.NONE); gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL); colorFrame.setLayoutData(gridData); // tool settings frame final Composite toolSettingsFrame = new Composite(displayArea, SWT.NONE); gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL); toolSettingsFrame.setLayoutData(gridData); // status text final Text statusText = new Text(displayArea, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY); gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL); statusText.setLayoutData(gridData); /*** Create the remaining application elements inside the principal GUI layout elements ***/ // paintSurface paintSurface = new PaintSurface(paintCanvas, statusText, paintColorWhite); // finish initializing the tool data tools[Pencil_tool].data = new PencilTool(toolSettings, paintSurface); tools[Airbrush_tool].data = new AirbrushTool(toolSettings, paintSurface); tools[Line_tool].data = new LineTool(toolSettings, paintSurface); tools[PolyLine_tool].data = new PolyLineTool(toolSettings, paintSurface); tools[Rectangle_tool].data = new RectangleTool(toolSettings, paintSurface); tools[RoundedRectangle_tool].data = new RoundedRectangleTool(toolSettings, paintSurface); tools[Ellipse_tool].data = new EllipseTool(toolSettings, paintSurface); tools[Text_tool].data = new TextTool(toolSettings, paintSurface); // colorFrame gridLayout = new GridLayout(); gridLayout.numColumns = 3; gridLayout.marginHeight = 0; gridLayout.marginWidth = 0; colorFrame.setLayout(gridLayout); // activeForegroundColorCanvas, activeBackgroundColorCanvas activeForegroundColorCanvas = new Canvas(colorFrame, SWT.BORDER); gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); gridData.heightHint = 24; gridData.widthHint = 24; activeForegroundColorCanvas.setLayoutData(gridData); activeBackgroundColorCanvas = new Canvas(colorFrame, SWT.BORDER); gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); gridData.heightHint = 24; gridData.widthHint = 24; activeBackgroundColorCanvas.setLayoutData(gridData); // paletteCanvas final Canvas paletteCanvas = new Canvas(colorFrame, SWT.BORDER | SWT.NO_BACKGROUND); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.heightHint = 24; paletteCanvas.setLayoutData(gridData); paletteCanvas.addListener(SWT.MouseDown, new Listener() { @Override public void handleEvent(Event e) { Rectangle bounds = paletteCanvas.getClientArea(); Color color = getColorAt(bounds, e.x, e.y); if (e.button == 1) setForegroundColor(color); else setBackgroundColor(color); } private Color getColorAt(Rectangle bounds, int x, int y) { if (bounds.height <= 1 && bounds.width <= 1) return paintColorWhite; final int row = (y - bounds.y) * numPaletteRows / bounds.height; final int col = (x - bounds.x) * numPaletteCols / bounds.width; return paintColors[Math.min(Math.max(row * numPaletteCols + col, 0), paintColors.length - 1)]; } }); Listener refreshListener = e -> { if (e.gc == null) return; Rectangle bounds = paletteCanvas.getClientArea(); for (int row = 0; row < numPaletteRows; ++row) { for (int col = 0; col < numPaletteCols; ++col) { final int x = bounds.width * col / numPaletteCols; final int y = bounds.height * row / numPaletteRows; final int width = Math.max(bounds.width * (col + 1) / numPaletteCols - x, 1); final int height = Math.max(bounds.height * (row + 1) / numPaletteRows - y, 1); e.gc.setBackground(paintColors[row * numPaletteCols + col]); e.gc.fillRectangle(bounds.x + x, bounds.y + y, width, height); } } }; paletteCanvas.addListener(SWT.Resize, refreshListener); paletteCanvas.addListener(SWT.Paint, refreshListener); //paletteCanvas.redraw(); // toolSettingsFrame gridLayout = new GridLayout(); gridLayout.numColumns = 4; gridLayout.marginHeight = 0; gridLayout.marginWidth = 0; toolSettingsFrame.setLayout(gridLayout); Label label = new Label(toolSettingsFrame, SWT.NONE); label.setText(getResourceString("settings.AirbrushRadius.text")); final Scale airbrushRadiusScale = new Scale(toolSettingsFrame, SWT.HORIZONTAL); airbrushRadiusScale.setMinimum(5); airbrushRadiusScale.setMaximum(50); airbrushRadiusScale.setSelection(toolSettings.airbrushRadius); airbrushRadiusScale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL)); airbrushRadiusScale.addSelectionListener(widgetSelectedAdapter(e -> { toolSettings.airbrushRadius = airbrushRadiusScale.getSelection(); updateToolSettings(); })); label = new Label(toolSettingsFrame, SWT.NONE); label.setText(getResourceString("settings.AirbrushIntensity.text")); final Scale airbrushIntensityScale = new Scale(toolSettingsFrame, SWT.HORIZONTAL); airbrushIntensityScale.setMinimum(1); airbrushIntensityScale.setMaximum(100); airbrushIntensityScale.setSelection(toolSettings.airbrushIntensity); airbrushIntensityScale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL)); airbrushIntensityScale.addSelectionListener(widgetSelectedAdapter(e -> { toolSettings.airbrushIntensity = airbrushIntensityScale.getSelection(); updateToolSettings(); })); }
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;/*ww w .jav a 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"); } })); }
From source file:BrowserExample.java
/** * Creates an instance of a ControlExample embedded inside the supplied * parent Composite./*from w w w . j a v a 2s . com*/ * * @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:PaintExample.java
/** * Creates the GUI.//from w ww .j av a 2 s. c o m */ public void createGUI(Composite parent) { GridLayout gridLayout; GridData gridData; /*** Create principal GUI layout elements ***/ Composite displayArea = new Composite(parent, SWT.NONE); gridLayout = new GridLayout(); gridLayout.numColumns = 1; displayArea.setLayout(gridLayout); // Creating these elements here avoids the need to instantiate the GUI elements // in strict layout order. The natural layout ordering is an artifact of using // SWT layouts, but unfortunately it is not the same order as that required to // instantiate all of the non-GUI application elements to satisfy referential // dependencies. It is possible to reorder the initialization to some extent, but // this can be very tedious. // paint canvas final Canvas paintCanvas = new Canvas(displayArea, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND); gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL); paintCanvas.setLayoutData(gridData); paintCanvas.setBackground(paintColorWhite); // color selector frame final Composite colorFrame = new Composite(displayArea, SWT.NONE); gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL); colorFrame.setLayoutData(gridData); // tool settings frame final Composite toolSettingsFrame = new Composite(displayArea, SWT.NONE); gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL); toolSettingsFrame.setLayoutData(gridData); // status text final Text statusText = new Text(displayArea, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY); gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL); statusText.setLayoutData(gridData); /*** Create the remaining application elements inside the principal GUI layout elements ***/ // paintSurface paintSurface = new PaintSurface(paintCanvas, statusText, paintColorWhite); // finish initializing the tool data tools[Pencil_tool].data = new PencilTool(toolSettings, paintSurface); tools[Airbrush_tool].data = new AirbrushTool(toolSettings, paintSurface); tools[Line_tool].data = new LineTool(toolSettings, paintSurface); tools[PolyLine_tool].data = new PolyLineTool(toolSettings, paintSurface); tools[Rectangle_tool].data = new RectangleTool(toolSettings, paintSurface); tools[RoundedRectangle_tool].data = new RoundedRectangleTool(toolSettings, paintSurface); tools[Ellipse_tool].data = new EllipseTool(toolSettings, paintSurface); tools[Text_tool].data = new TextTool(toolSettings, paintSurface); // colorFrame gridLayout = new GridLayout(); gridLayout.numColumns = 3; gridLayout.marginHeight = 0; gridLayout.marginWidth = 0; colorFrame.setLayout(gridLayout); // activeForegroundColorCanvas, activeBackgroundColorCanvas activeForegroundColorCanvas = new Canvas(colorFrame, SWT.BORDER); gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); gridData.heightHint = 24; gridData.widthHint = 24; activeForegroundColorCanvas.setLayoutData(gridData); activeBackgroundColorCanvas = new Canvas(colorFrame, SWT.BORDER); gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); gridData.heightHint = 24; gridData.widthHint = 24; activeBackgroundColorCanvas.setLayoutData(gridData); // paletteCanvas final Canvas paletteCanvas = new Canvas(colorFrame, SWT.BORDER | SWT.NO_BACKGROUND); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.heightHint = 24; paletteCanvas.setLayoutData(gridData); paletteCanvas.addListener(SWT.MouseDown, new Listener() { public void handleEvent(Event e) { Rectangle bounds = paletteCanvas.getClientArea(); Color color = getColorAt(bounds, e.x, e.y); if (e.button == 1) setForegroundColor(color); else setBackgroundColor(color); } private Color getColorAt(Rectangle bounds, int x, int y) { if (bounds.height <= 1 && bounds.width <= 1) return paintColorWhite; final int row = (y - bounds.y) * numPaletteRows / bounds.height; final int col = (x - bounds.x) * numPaletteCols / bounds.width; return paintColors[Math.min(Math.max(row * numPaletteCols + col, 0), paintColors.length - 1)]; } }); Listener refreshListener = new Listener() { public void handleEvent(Event e) { if (e.gc == null) return; Rectangle bounds = paletteCanvas.getClientArea(); for (int row = 0; row < numPaletteRows; ++row) { for (int col = 0; col < numPaletteCols; ++col) { final int x = bounds.width * col / numPaletteCols; final int y = bounds.height * row / numPaletteRows; final int width = Math.max(bounds.width * (col + 1) / numPaletteCols - x, 1); final int height = Math.max(bounds.height * (row + 1) / numPaletteRows - y, 1); e.gc.setBackground(paintColors[row * numPaletteCols + col]); e.gc.fillRectangle(bounds.x + x, bounds.y + y, width, height); } } } }; paletteCanvas.addListener(SWT.Resize, refreshListener); paletteCanvas.addListener(SWT.Paint, refreshListener); //paletteCanvas.redraw(); // toolSettingsFrame gridLayout = new GridLayout(); gridLayout.numColumns = 4; gridLayout.marginHeight = 0; gridLayout.marginWidth = 0; toolSettingsFrame.setLayout(gridLayout); Label label = new Label(toolSettingsFrame, SWT.NONE); label.setText(getResourceString("settings.AirbrushRadius.text")); final Scale airbrushRadiusScale = new Scale(toolSettingsFrame, SWT.HORIZONTAL); airbrushRadiusScale.setMinimum(5); airbrushRadiusScale.setMaximum(50); airbrushRadiusScale.setSelection(toolSettings.airbrushRadius); airbrushRadiusScale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL)); airbrushRadiusScale.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { toolSettings.airbrushRadius = airbrushRadiusScale.getSelection(); updateToolSettings(); } }); label = new Label(toolSettingsFrame, SWT.NONE); label.setText(getResourceString("settings.AirbrushIntensity.text")); final Scale airbrushIntensityScale = new Scale(toolSettingsFrame, SWT.HORIZONTAL); airbrushIntensityScale.setMinimum(1); airbrushIntensityScale.setMaximum(100); airbrushIntensityScale.setSelection(toolSettings.airbrushIntensity); airbrushIntensityScale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL)); airbrushIntensityScale.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { toolSettings.airbrushIntensity = airbrushIntensityScale.getSelection(); updateToolSettings(); } }); }