import org.eclipse.swt.SWT; import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.custom.*; /** * This code probably looks ugly, but whatever. I am now releasing this into the Public Domain. Do with it what you will. * * @author Remy Suen */ public class A { private boolean isDragging = false; private int downX = -1; private int downY = -1; private int upX = -1; private int upY = -1; private GC gc; private Shell shell; public A() { final Display display = new Display(); GC context = new GC(display); final Image image = new Image(display, display.getBounds()); context.copyArea(image, 0, 0); context.dispose(); Color color = new Color(display, 255, 255, 255); shell = new Shell(display, SWT.NO_TRIM); shell.setLayout(new FillLayout()); shell.setText("Image"); shell.setBounds(display.getBounds()); gc = new GC(shell); gc.setForeground(color); shell.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { gc.drawImage(image, 0, 0); } }); shell.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent e) { isDragging = true; System.out.println(e.x + "\t" + e.y); downX = e.x; downY = e.y; } public void mouseUp(MouseEvent e) { isDragging = false; upX = e.x; upY = e.y; gc.drawImage(image, 0, 0); } }); shell.addMouseMoveListener(new MouseMoveListener() { public void mouseMove(MouseEvent e) { if (isDragging) { gc.drawImage(image, 0, 0); gc.drawRectangle(downX, downY, e.x - downX, e.y - downY); } } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } image.dispose(); color.dispose(); display.dispose(); } public static void main(String[] args) { A a = new A(); } }