Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/org.eclipse.swt.snippets/Snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ To contribute a new snippet, [create a snippet contribution as a pull request](h
- [take a screen shot with a GC](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet215.java) – [(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet215.png "Preview for Snippet 215")
- [draw 2 polylines with different line attributes](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet252.java) – [(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet252.png "Preview for Snippet 252")
- [draw lines with configurable line width, scaling and rotation](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet381.java) – [(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet381.png "Preview for Snippet 381")
- [crop and scale images via source and destination values](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet389.java) – [(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet389.png "Preview for Snippet 389")

### **Gesture, Touch support**
- [create a shell and listen for TouchEvents](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet352.java)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
Comment on lines +1 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
/*******************************************************************************
* Copyright (c) 2026 Yatta Solutions and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/


package org.eclipse.swt.snippets;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet389 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a short documentation like for other snippets. Here is one of the most recent (but the description does not need to be that long): https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet387.java

Most important, the first sentence of the description should be good summary of the snippet to be automatically shown in the SnippetExplorer:
image


private static final String IMAGE_100 = "workset_wiz150.png";
private static final String IMAGE_200 = "workset_wiz300.png";
private static final String IMAGES_ROOT = "resources/Snippet388/";

private static final String IMAGE_PATH_100 = IMAGES_ROOT + IMAGE_100;
private static final String IMAGE_PATH_200 = IMAGES_ROOT + IMAGE_200;

static int srcX, srcY, srcW, srcH;
static int dstX, dstY, dstW, dstH;

public static void main(String[] args) {

ImageFileNameProvider filenameProvider = zoom -> {
switch (zoom) {
case 100:
return IMAGE_PATH_100;
case 200:
return IMAGE_PATH_200;
default:
return null;
}
};

Display display = new Display();
Shell shell = new Shell(display);
shell.setText("GC#drawImage Interactive");
shell.setLayout(new GridLayout(2, false));

Image image = new Image(display, filenameProvider);
Rectangle imgBounds = image.getBounds();

final int fixedDstW = imgBounds.width * 2;
final int fixedDstH = imgBounds.height * 2;
srcX = imgBounds.width / 2;
srcY = imgBounds.height / 2;
srcW = imgBounds.width / 2;
srcH = imgBounds.height / 2;

dstX = 0;
dstY = 0;
dstW = imgBounds.width;
dstH = imgBounds.height;

Group controls = new Group(shell, SWT.NONE);
controls.setText("GC#drawImage Arguments");
controls.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
controls.setLayout(new GridLayout(2, false));

Text tSrcX = field(controls, "srcX", srcX);
Text tSrcY = field(controls, "srcY", srcY);
Text tSrcW = field(controls, "srcWidth", srcW);
Text tSrcH = field(controls, "srcHeight", srcH);

Text tDstX = field(controls, "destX", dstX);
Text tDstY = field(controls, "destY", dstY);
Text tDstW = field(controls, "destWidth", dstW);
Text tDstH = field(controls, "destHeight", dstH);

Button apply = new Button(controls, SWT.PUSH);
apply.setText("Apply");
apply.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

Composite draw = new Composite(shell, SWT.NONE);
draw.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
draw.setLayout(new GridLayout(2, true));

Canvas srcCanvas = new Canvas(draw, SWT.BORDER);
srcCanvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
srcCanvas.addListener(SWT.Paint, e -> {
GC gc = e.gc;
Rectangle ca = srcCanvas.getClientArea();

int ix = (ca.width - imgBounds.width) / 2;
int iy = (ca.height - imgBounds.height) / 2;

gc.drawImage(image, ix, iy);

gc.setLineStyle(SWT.LINE_DASH);
gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
gc.drawRectangle(ix, iy, imgBounds.width, imgBounds.height);

gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
gc.setLineWidth(2);
gc.drawRectangle(ix + srcX, iy + srcY, srcW, srcH);
});

Canvas dstCanvas = new Canvas(draw, SWT.BORDER);
dstCanvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
dstCanvas.addListener(SWT.Paint, e -> {
GC gc = e.gc;
Rectangle ca = dstCanvas.getClientArea();

int px = (ca.width - fixedDstW) / 2;
int py = (ca.height - fixedDstH) / 2;

gc.drawImage(
image,
srcX, srcY, srcW, srcH,
px + dstX, py + dstY,
dstW, dstH
);

gc.setLineStyle(SWT.LINE_DASH);
gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
gc.drawRectangle(px, py, fixedDstW, fixedDstH);

gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
gc.setLineWidth(2);
gc.drawRectangle(px + dstX, py + dstY, dstW, dstH);
});

Label srcLabel = new Label(draw, SWT.CENTER);
srcLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
srcLabel.setText("150 x 150");

Label dstLabel = new Label(draw, SWT.CENTER);
dstLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
dstLabel.setText("300 x 300");

apply.addListener(SWT.Selection, e -> {
srcX = parse(tSrcX);
srcY = parse(tSrcY);
srcW = parse(tSrcW);
srcH = parse(tSrcH);

dstX = parse(tDstX);
dstY = parse(tDstY);
dstW = parse(tDstW);
dstH = parse(tDstH);

srcCanvas.redraw();
dstCanvas.redraw();
});

shell.setSize(1000, 600);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

image.dispose();
display.dispose();
}

static Text field(Composite parent, String label, int value) {
new Label(parent, SWT.NONE).setText(label);
Text t = new Text(parent, SWT.BORDER);
t.setText(String.valueOf(value));
t.setLayoutData(new GridData(70, SWT.DEFAULT));
return t;
}

static int parse(Text t) {
try {
return Integer.parseInt(t.getText());
} catch (NumberFormatException e) {
return 0;
}
}
}
Loading