Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My answer is somehow similar to the one I gave here:</p> <p><a href="https://stackoverflow.com/a/12420458/1449199">SWTChart mouse event handling</a></p> <p>The code below should give you an idea on where to start. It basically remembers the location of a <code>MouseDown</code> event. Each time the mouse is then dragged, a rectangle is drawn from the starting location to the current location. Once <code>MouseUp</code> happens, everything stops.</p> <pre><code>public class DrawExample { private static boolean drag = false; private static int startX; private static int startY; private static int endX; private static int endY; public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Drawing Example"); final Canvas canvas = new Canvas(shell, SWT.NONE); canvas.setSize(150, 150); canvas.setLocation(20, 20); shell.open(); shell.setSize(200, 220); canvas.addListener(SWT.MouseDown, new Listener() { @Override public void handleEvent(Event event) { startX = event.x; startY = event.y; drag = true; } }); canvas.addListener(SWT.MouseUp, new Listener() { @Override public void handleEvent(Event event) { endX = event.x; endY = event.y; drag = false; canvas.redraw(); } }); canvas.addListener(SWT.MouseMove, new Listener() { @Override public void handleEvent(Event event) { if(drag) { endX = event.x; endY = event.y; canvas.redraw(); } } }); canvas.addListener(SWT.Paint, new Listener() { @Override public void handleEvent(Event event) { if(drag) { GC gc = event.gc; gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); gc.setAlpha(128); int minX = Math.min(startX, endX); int minY = Math.min(startY, endY); int maxX = Math.max(startX, endX); int maxY = Math.max(startY, endY); int width = maxX - minX; int height = maxY - minY; gc.fillRectangle(minX, minY, width, height); } } }); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } </code></pre> <p>It looks like this:</p> <p><img src="https://i.stack.imgur.com/kz3XE.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload