List of usage examples for org.eclipse.swt.widgets Canvas getClientArea
public Rectangle getClientArea()
From source file:org.eclipse.swt.examples.accessibility.AccessibleValueExample.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); shell.setText("Accessible Value Example"); final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED); canvas.addPaintListener(e -> {/*from w ww . j av a 2 s . c om*/ Rectangle rect = canvas.getClientArea(); String val = String.valueOf(value); Point size = e.gc.stringExtent(val); e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_LIST_SELECTION)); e.gc.fillRectangle(0, 0, rect.width * value / (max - min), rect.height); e.gc.drawString(val, rect.x + (rect.width - size.x) / 2, rect.y + (rect.height - size.y) / 2, true); }); Accessible accessible = canvas.getAccessible(); accessible.addAccessibleListener(new AccessibleAdapter() { @Override public void getName(AccessibleEvent e) { e.result = "The value of this canvas is " + value; } }); accessible.addAccessibleControlListener(new AccessibleControlAdapter() { @Override public void getRole(AccessibleControlEvent e) { e.detail = ACC.ROLE_PROGRESSBAR; } }); accessible.addAccessibleValueListener(new AccessibleValueAdapter() { @Override public void setCurrentValue(AccessibleValueEvent e) { value = e.value.intValue(); canvas.redraw(); } @Override public void getMinimumValue(AccessibleValueEvent e) { e.value = Integer.valueOf(min); } @Override public void getMaximumValue(AccessibleValueEvent e) { e.value = Integer.valueOf(max); } @Override public void getCurrentValue(AccessibleValueEvent e) { e.value = Integer.valueOf(value); } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:CanvasKeyEvent.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout()); final Canvas canvas = new Canvas(shell, SWT.NULL); canvas.setSize(500, 500);//from w ww. java2 s .c om canvas.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); canvas.addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { GC gc = new GC(canvas); Rectangle rect = canvas.getClientArea(); gc.fillRectangle(rect.x, rect.y, rect.width, rect.height); Font font = new Font(display, "Arial", 32, SWT.BOLD); gc.setFont(font); gc.drawString("" + e.character, 15, 10); gc.dispose(); font.dispose(); } public void keyReleased(KeyEvent e) { } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } }
From source file:org.eclipse.swt.examples.accessibility.AccessibleActionExample.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); shell.setText("Accessible Action Example"); Button button = new Button(shell, SWT.PUSH); button.setText("Button"); final Canvas customButton = new Canvas(shell, SWT.NONE) { @Override//from w ww .j ava 2s . c o m public Point computeSize(int wHint, int hHint, boolean changed) { GC gc = new GC(this); Point point = gc.stringExtent(buttonText); gc.dispose(); point.x += MARGIN; point.y += MARGIN; return point; } }; customButton.addPaintListener(e -> { Rectangle clientArea = customButton.getClientArea(); Point stringExtent = e.gc.stringExtent(buttonText); int x = clientArea.x + (clientArea.width - stringExtent.x) / 2; int y = clientArea.y + (clientArea.height - stringExtent.y) / 2; e.gc.drawString(buttonText, x, y); }); customButton.addMouseListener(MouseListener.mouseDownAdapter(e -> { int actionIndex = (e.button == 1) ? 0 : 1; customButtonAction(actionIndex); })); customButton.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int modifierKeys = e.stateMask & SWT.MODIFIER_MASK; if (modifierKeys == SWT.CTRL || modifierKeys == 0) { if (e.character == '1') customButtonAction(0); else if (e.character == '2') customButtonAction(1); } } }); Accessible accessible = customButton.getAccessible(); accessible.addAccessibleListener(new AccessibleAdapter() { @Override public void getName(AccessibleEvent e) { e.result = buttonText; } @Override public void getKeyboardShortcut(AccessibleEvent e) { e.result = "CTRL+1"; // default action is 'action 1' } }); accessible.addAccessibleControlListener(new AccessibleControlAdapter() { @Override public void getRole(AccessibleControlEvent e) { e.detail = ACC.ROLE_PUSHBUTTON; } }); accessible.addAccessibleActionListener(new AccessibleActionAdapter() { @Override public void getActionCount(AccessibleActionEvent e) { e.count = 2; } @Override public void getName(AccessibleActionEvent e) { if (0 <= e.index && e.index <= 1) { if (e.localized) { e.result = AccessibleActionExample.getResourceString("action" + e.index); } else { e.result = "Action" + e.index; //$NON-NLS-1$ } } } @Override public void getDescription(AccessibleActionEvent e) { if (0 <= e.index && e.index <= 1) { e.result = AccessibleActionExample.getResourceString("action" + e.index + "description"); } } @Override public void doAction(AccessibleActionEvent e) { if (0 <= e.index && e.index <= 1) { customButtonAction(e.index); e.result = ACC.OK; } } @Override public void getKeyBinding(AccessibleActionEvent e) { switch (e.index) { case 0: e.result = "1;CTRL+1"; break; case 1: e.result = "2;CTRL+2"; break; default: e.result = null; } } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet48.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 48"); shell.setLayout(new FillLayout()); Image originalImage = null;//from w w w . ja v a2 s. c o m FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Open an image file or cancel"); String string = dialog.open(); if (string != null) { originalImage = new Image(display, string); } if (originalImage == null) { int width = 150, height = 200; originalImage = new Image(display, width, height); GC gc = new GC(originalImage); gc.fillRectangle(0, 0, width, height); gc.drawLine(0, 0, width, height); gc.drawLine(0, height, width, 0); gc.drawText("Default Image", 10, 10); gc.dispose(); } final Image image = originalImage; final Point origin = new Point(0, 0); final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL); final ScrollBar hBar = canvas.getHorizontalBar(); hBar.addListener(SWT.Selection, e -> { int hSelection = hBar.getSelection(); int destX = -hSelection - origin.x; Rectangle rect = image.getBounds(); canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false); origin.x = -hSelection; }); final ScrollBar vBar = canvas.getVerticalBar(); vBar.addListener(SWT.Selection, e -> { int vSelection = vBar.getSelection(); int destY = -vSelection - origin.y; Rectangle rect = image.getBounds(); canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false); origin.y = -vSelection; }); canvas.addListener(SWT.Resize, e -> { Rectangle rect = image.getBounds(); Rectangle client = canvas.getClientArea(); hBar.setMaximum(rect.width); vBar.setMaximum(rect.height); hBar.setThumb(Math.min(rect.width, client.width)); vBar.setThumb(Math.min(rect.height, client.height)); int hPage = rect.width - client.width; int vPage = rect.height - client.height; int hSelection = hBar.getSelection(); int vSelection = vBar.getSelection(); if (hSelection >= hPage) { if (hPage <= 0) hSelection = 0; origin.x = -hSelection; } if (vSelection >= vPage) { if (vPage <= 0) vSelection = 0; origin.y = -vSelection; } canvas.redraw(); }); canvas.addListener(SWT.Paint, e -> { GC gc = e.gc; gc.drawImage(image, origin.x, origin.y); Rectangle rect = image.getBounds(); Rectangle client = canvas.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); } }); Rectangle rect = image.getBounds(); shell.setSize(Math.max(200, rect.width - 100), Math.max(150, rect.height - 100)); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } originalImage.dispose(); display.dispose(); }
From source file:ImageScrollFlickerFree.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Image originalImage = null;/*from ww w.j ava 2 s .c o m*/ FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Open an image file or cancel"); String string = dialog.open(); if (string != null) { originalImage = new Image(display, string); } if (originalImage == null) { int width = 150, height = 200; originalImage = new Image(display, width, height); GC gc = new GC(originalImage); gc.fillRectangle(0, 0, width, height); gc.drawLine(0, 0, width, height); gc.drawLine(0, height, width, 0); gc.drawText("Default Image", 10, 10); gc.dispose(); } final Image image = originalImage; final Point origin = new Point(0, 0); final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL); final ScrollBar hBar = canvas.getHorizontalBar(); hBar.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { int hSelection = hBar.getSelection(); int destX = -hSelection - origin.x; Rectangle rect = image.getBounds(); canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false); origin.x = -hSelection; } }); final ScrollBar vBar = canvas.getVerticalBar(); vBar.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { int vSelection = vBar.getSelection(); int destY = -vSelection - origin.y; Rectangle rect = image.getBounds(); canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false); origin.y = -vSelection; } }); canvas.addListener(SWT.Resize, new Listener() { public void handleEvent(Event e) { Rectangle rect = image.getBounds(); Rectangle client = canvas.getClientArea(); hBar.setMaximum(rect.width); vBar.setMaximum(rect.height); hBar.setThumb(Math.min(rect.width, client.width)); vBar.setThumb(Math.min(rect.height, client.height)); int hPage = rect.width - client.width; int vPage = rect.height - client.height; int hSelection = hBar.getSelection(); int vSelection = vBar.getSelection(); if (hSelection >= hPage) { if (hPage <= 0) hSelection = 0; origin.x = -hSelection; } if (vSelection >= vPage) { if (vPage <= 0) vSelection = 0; origin.y = -vSelection; } canvas.redraw(); } }); canvas.addListener(SWT.Paint, new Listener() { public void handleEvent(Event e) { GC gc = e.gc; gc.drawImage(image, origin.x, origin.y); Rectangle rect = image.getBounds(); Rectangle client = canvas.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); } } }); shell.setSize(200, 150); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } originalImage.dispose(); display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet174.java
static void resize(Canvas canvas) { Rectangle rect = canvas.getClientArea(); int width = rect.width; int height = Math.max(rect.height, 1); GL.glViewport(0, 0, width, height);/*from w w w . j a v a 2s . c om*/ GL.glMatrixMode(GL.GL_PROJECTION); GL.glLoadIdentity(); float aspect = (float) width / (float) height; GLU.gluPerspective(45.0f, aspect, 0.5f, 400.0f); GL.glMatrixMode(GL.GL_MODELVIEW); GL.glLoadIdentity(); }
From source file:FocusTraversal.java
private void init() { shell.setLayout(new RowLayout()); Composite composite1 = new Composite(shell, SWT.BORDER); composite1.setLayout(new RowLayout()); composite1.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); Button button1 = new Button(composite1, SWT.PUSH); button1.setText("Button1"); List list = new List(composite1, SWT.MULTI | SWT.BORDER); list.setItems(new String[] { "Item-1", "Item-2", "Item-3" }); Button radioButton1 = new Button(composite1, SWT.RADIO); radioButton1.setText("radio-1"); Button radioButton2 = new Button(composite1, SWT.RADIO); radioButton2.setText("radio-2"); Composite composite2 = new Composite(shell, SWT.BORDER); composite2.setLayout(new RowLayout()); composite2.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); Button button2 = new Button(composite2, SWT.PUSH); button2.setText("Button2"); final Canvas canvas = new Canvas(composite2, SWT.NULL); canvas.setSize(50, 50);//from ww w . j a va 2 s . co m canvas.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); Combo combo = new Combo(composite2, SWT.DROP_DOWN); combo.add("combo"); combo.select(0); canvas.addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { GC gc = new GC(canvas); // Erase background first. Rectangle rect = canvas.getClientArea(); gc.fillRectangle(rect.x, rect.y, rect.width, rect.height); Font font = new Font(display, "Arial", 32, SWT.BOLD); gc.setFont(font); gc.drawString("" + e.character, 15, 10); gc.dispose(); font.dispose(); } public void keyReleased(KeyEvent e) { } }); canvas.addTraverseListener(new TraverseListener() { public void keyTraversed(TraverseEvent e) { if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) e.doit = true; } }); composite1.setTabList(new Control[] { button1, list }); composite2.setTabList(new Control[] { button2, canvas, combo }); shell.setTabList(new Control[] { composite2, composite1 }); }
From source file:org.eclipse.swt.examples.paint.PaintExample.java
/** * Creates the GUI.//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;//from w ww . ja v 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:PaintExample.java
/** * Creates the GUI.//from w w w. j ava 2s . 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(); } }); }