|
0
|
1 |
/* |
|
|
2 |
TUIO Java Demo - part of the reacTIVision project |
|
|
3 |
http://reactivision.sourceforge.net/ |
|
|
4 |
|
|
|
5 |
Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu> |
|
|
6 |
|
|
|
7 |
This program is free software; you can redistribute it and/or modify |
|
|
8 |
it under the terms of the GNU General Public License as published by |
|
|
9 |
the Free Software Foundation; either version 2 of the License, or |
|
|
10 |
(at your option) any later version. |
|
|
11 |
|
|
|
12 |
This program is distributed in the hope that it will be useful, |
|
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
15 |
GNU General Public License for more details. |
|
|
16 |
|
|
|
17 |
You should have received a copy of the GNU General Public License |
|
|
18 |
along with this program; if not, write to the Free Software |
|
|
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
|
20 |
*/ |
|
|
21 |
|
|
|
22 |
import java.awt.*; |
|
|
23 |
import java.awt.geom.*; |
|
|
24 |
import java.awt.event.*; |
|
|
25 |
import java.awt.image.*; |
|
|
26 |
import java.util.*; |
|
|
27 |
import javax.swing.*; |
|
|
28 |
import TUIO.*; |
|
|
29 |
|
|
|
30 |
public class TuioDemoComponent extends JComponent implements TuioListener { |
|
|
31 |
|
|
|
32 |
private Hashtable<Long,TuioDemoObject> objectList = new Hashtable<Long,TuioDemoObject>(); |
|
|
33 |
private Hashtable<Long,TuioCursor> cursorList = new Hashtable<Long,TuioCursor>(); |
|
3
|
34 |
private Hashtable<Long,TuioString> stringList = new Hashtable<Long,TuioString>(); |
|
0
|
35 |
|
|
|
36 |
public static final int finger_size = 15; |
|
|
37 |
public static final int object_size = 60; |
|
|
38 |
public static final int table_size = 760; |
|
|
39 |
|
|
|
40 |
public static int width, height; |
|
|
41 |
private float scale = 1.0f; |
|
|
42 |
public boolean verbose = false; |
|
|
43 |
|
|
|
44 |
public void setSize(int w, int h) { |
|
|
45 |
super.setSize(w,h); |
|
|
46 |
width = w; |
|
|
47 |
height = h; |
|
|
48 |
scale = height/(float)TuioDemoComponent.table_size; |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
public void addTuioObject(TuioObject tobj) { |
|
|
52 |
TuioDemoObject demo = new TuioDemoObject(tobj); |
|
|
53 |
objectList.put(tobj.getSessionID(),demo); |
|
|
54 |
|
|
|
55 |
if (verbose) |
|
|
56 |
System.out.println("add obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()); |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
public void updateTuioObject(TuioObject tobj) { |
|
|
60 |
|
|
|
61 |
TuioDemoObject demo = (TuioDemoObject)objectList.get(tobj.getSessionID()); |
|
|
62 |
demo.update(tobj); |
|
|
63 |
|
|
|
64 |
if (verbose) |
|
|
65 |
System.out.println("set obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel()); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
public void removeTuioObject(TuioObject tobj) { |
|
|
69 |
objectList.remove(tobj.getSessionID()); |
|
|
70 |
|
|
|
71 |
if (verbose) |
|
|
72 |
System.out.println("del obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")"); |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
public void addTuioCursor(TuioCursor tcur) { |
|
|
76 |
|
|
|
77 |
if (!cursorList.containsKey(tcur.getSessionID())) { |
|
|
78 |
cursorList.put(tcur.getSessionID(), tcur); |
|
|
79 |
repaint(); |
|
|
80 |
} |
|
|
81 |
|
|
|
82 |
if (verbose) |
|
|
83 |
System.out.println("add cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY()); |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
public void updateTuioCursor(TuioCursor tcur) { |
|
|
87 |
|
|
|
88 |
repaint(); |
|
|
89 |
|
|
|
90 |
if (verbose) |
|
|
91 |
System.out.println("set cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY()+" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel()); |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
public void removeTuioCursor(TuioCursor tcur) { |
|
|
95 |
|
|
|
96 |
cursorList.remove(tcur.getSessionID()); |
|
|
97 |
repaint(); |
|
|
98 |
|
|
|
99 |
if (verbose) |
|
|
100 |
System.out.println("del cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+")"); |
|
|
101 |
} |
|
3
|
102 |
|
|
|
103 |
public void addTuioString(TuioString tstr) { |
|
|
104 |
|
|
|
105 |
if (!stringList.containsKey(tstr.getSessionID())) { |
|
|
106 |
stringList.put(tstr.getSessionID(), tstr); |
|
|
107 |
repaint(); |
|
|
108 |
} |
|
|
109 |
|
|
|
110 |
if (verbose) |
|
|
111 |
System.out.println("add str "+tstr.getStringID()+" ("+tstr.getSessionID()+") "+tstr.getMessage()); |
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
public void updateTuioString(TuioString tstr) { |
|
|
115 |
|
|
|
116 |
repaint(); |
|
|
117 |
|
|
|
118 |
if (verbose) |
|
|
119 |
System.out.println("set str "+tstr.getStringID()+" ("+tstr.getSessionID()+") "+tstr.getMessage()); |
|
|
120 |
} |
|
|
121 |
|
|
|
122 |
public void removeTuioString(TuioString tstr) { |
|
|
123 |
|
|
|
124 |
stringList.remove(tstr.getSessionID()); |
|
|
125 |
repaint(); |
|
|
126 |
|
|
|
127 |
if (verbose) |
|
|
128 |
System.out.println("del str "+tstr.getStringID()+" ("+tstr.getSessionID()+")"); |
|
|
129 |
} |
|
0
|
130 |
|
|
|
131 |
public void refresh(TuioTime frameTime) { |
|
|
132 |
repaint(); |
|
|
133 |
} |
|
|
134 |
|
|
|
135 |
public void paint(Graphics g) { |
|
|
136 |
update(g); |
|
|
137 |
} |
|
|
138 |
|
|
|
139 |
public void update(Graphics g) { |
|
|
140 |
|
|
|
141 |
Graphics2D g2 = (Graphics2D)g; |
|
|
142 |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
|
|
143 |
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); |
|
|
144 |
|
|
|
145 |
g2.setColor(Color.white); |
|
|
146 |
g2.fillRect(0,0,width,height); |
|
|
147 |
|
|
|
148 |
int w = (int)Math.round(width-scale*finger_size/2.0f); |
|
|
149 |
int h = (int)Math.round(height-scale*finger_size/2.0f); |
|
|
150 |
|
|
|
151 |
Enumeration<TuioCursor> cursors = cursorList.elements(); |
|
|
152 |
while (cursors.hasMoreElements()) { |
|
|
153 |
TuioCursor tcur = cursors.nextElement(); |
|
|
154 |
if (tcur==null) continue; |
|
|
155 |
Vector<TuioPoint> path = tcur.getPath(); |
|
|
156 |
TuioPoint current_point = path.elementAt(0); |
|
|
157 |
if (current_point!=null) { |
|
|
158 |
// draw the cursor path |
|
|
159 |
g2.setPaint(Color.blue); |
|
|
160 |
for (int i=0;i<path.size();i++) { |
|
|
161 |
TuioPoint next_point = path.elementAt(i); |
|
|
162 |
g2.drawLine(current_point.getScreenX(w), current_point.getScreenY(h), next_point.getScreenX(w), next_point.getScreenY(h)); |
|
|
163 |
current_point = next_point; |
|
|
164 |
} |
|
|
165 |
} |
|
|
166 |
|
|
|
167 |
// draw the finger tip |
|
|
168 |
g2.setPaint(Color.lightGray); |
|
|
169 |
int s = (int)(scale*finger_size); |
|
|
170 |
g2.fillOval(current_point.getScreenX(w-s/2),current_point.getScreenY(h-s/2),s,s); |
|
|
171 |
g2.setPaint(Color.black); |
|
|
172 |
g2.drawString(tcur.getCursorID()+"",current_point.getScreenX(w),current_point.getScreenY(h)); |
|
|
173 |
} |
|
|
174 |
|
|
|
175 |
// draw the objects |
|
|
176 |
Enumeration<TuioDemoObject> objects = objectList.elements(); |
|
|
177 |
while (objects.hasMoreElements()) { |
|
|
178 |
TuioDemoObject tobj = objects.nextElement(); |
|
|
179 |
if (tobj!=null) tobj.paint(g2, width,height); |
|
|
180 |
} |
|
|
181 |
} |
|
|
182 |
} |