diff options
author | Scott Kovatch <skovatch> | 2011-01-29 00:04:09 +0000 |
---|---|---|
committer | Scott Kovatch <skovatch> | 2011-01-29 00:04:09 +0000 |
commit | 64349bf9cd19222d7f7bef44cb4d0e244ca7ed8f (patch) | |
tree | fd9d3dbb758912556de0d225aa717e7f8b97cbbb | |
parent | dcc0e2e76a96518f2bd60f127e49f8a4bd5ba452 (diff) | |
download | eclipse.platform.swt-64349bf9cd19222d7f7bef44cb4d0e244ca7ed8f.tar.gz eclipse.platform.swt-64349bf9cd19222d7f7bef44cb4d0e244ca7ed8f.tar.xz eclipse.platform.swt-64349bf9cd19222d7f7bef44cb4d0e244ca7ed8f.zip |
Fill in touch example snippet.
-rw-r--r-- | examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet352.java | 97 |
1 files changed, 86 insertions, 11 deletions
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet352.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet352.java index e393cfadd5..f76fc873f3 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet352.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet352.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 IBM Corporation and others. + * Copyright (c) 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,24 +10,99 @@ *******************************************************************************/ package org.eclipse.swt.snippets; -/* - * Touch example +/* + * Touch example: create a shell and listen for TouchEvents. Paint + * circles where the user touched. * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ - * + * * @since 3.7 - */ + */ +import java.util.*; + +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class Snippet352 { + + private static class CircleInfo { + + public CircleInfo(Point inCenter, Color inColor) { + this.center = inCenter; + this.color = inColor; + } + + Point center; + Color color; + } + + static Map<Long, CircleInfo> touchLocations = new HashMap<Long, CircleInfo>(); + static int colorIndex = 0; + static final int PAINTABLE_COLORS = 15; + static final int CIRCLE_RADIUS = 20; + + public static void main (String [] args) { + final Display display = new Display (); + final Shell shell = new Shell (display); + shell.setLayout(new FillLayout(SWT.HORIZONTAL)); + shell.setText ("Touch demonstration"); + + TouchListener tl = new TouchListener() { + public void touch(TouchEvent e) { + + Touch touches[] = e.touches; + + for (int i = 0; i < touches.length; i++) { + Touch currTouch = touches[i]; + + if ((currTouch.state & (SWT.TOUCHSTATE_UP)) != 0) { + touchLocations.remove(currTouch.id); + } else { + CircleInfo info = touchLocations.get(currTouch.id); + Point newPoint = Display.getCurrent().map(null, (Control)e.widget, new Point((int)currTouch.x, (int)currTouch.y)); + + if (info == null) { + info = new CircleInfo(newPoint, display.getSystemColor((colorIndex + 2) % PAINTABLE_COLORS)); + colorIndex++; + } + + info.center = newPoint; + touchLocations.put(currTouch.id, info); + } + } + + Control c = (Control)e.widget; + c.redraw(); + } + + }; + + PaintListener pl = new PaintListener() { + public void paintControl(PaintEvent e) { + Iterator<Map.Entry<Long, CircleInfo>> iter = touchLocations.entrySet().iterator(); + while (iter.hasNext()) { + CircleInfo ci = iter.next().getValue(); + e.gc.setBackground(ci.color); + e.gc.fillOval(ci.center.x - CIRCLE_RADIUS, ci.center.y - CIRCLE_RADIUS, CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2); + } + } + }; + + Canvas c = new Canvas(shell, SWT.NONE); + c.setTouchEventsEnabled(true); + c.setSize(800, 800); + c.addTouchListener(tl); + c.addPaintListener(pl); - public static void main(String [] args) { - Display display = new Display(); - Shell shell = new Shell(display); - while (!shell.isDisposed()) { - if (!display.readAndDispatch()) display.sleep(); + shell.setSize (800, 800); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); } - display.dispose(); + display.dispose (); } }
\ No newline at end of file |