|
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>(); |
|
34 |
|
35 public static final int finger_size = 15; |
|
36 public static final int object_size = 60; |
|
37 public static final int table_size = 760; |
|
38 |
|
39 public static int width, height; |
|
40 private float scale = 1.0f; |
|
41 public boolean verbose = false; |
|
42 |
|
43 public void setSize(int w, int h) { |
|
44 super.setSize(w,h); |
|
45 width = w; |
|
46 height = h; |
|
47 scale = height/(float)TuioDemoComponent.table_size; |
|
48 } |
|
49 |
|
50 public void addTuioObject(TuioObject tobj) { |
|
51 TuioDemoObject demo = new TuioDemoObject(tobj); |
|
52 objectList.put(tobj.getSessionID(),demo); |
|
53 |
|
54 if (verbose) |
|
55 System.out.println("add obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()); |
|
56 } |
|
57 |
|
58 public void updateTuioObject(TuioObject tobj) { |
|
59 |
|
60 TuioDemoObject demo = (TuioDemoObject)objectList.get(tobj.getSessionID()); |
|
61 demo.update(tobj); |
|
62 |
|
63 if (verbose) |
|
64 System.out.println("set obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel()); |
|
65 } |
|
66 |
|
67 public void removeTuioObject(TuioObject tobj) { |
|
68 objectList.remove(tobj.getSessionID()); |
|
69 |
|
70 if (verbose) |
|
71 System.out.println("del obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")"); |
|
72 } |
|
73 |
|
74 public void addTuioCursor(TuioCursor tcur) { |
|
75 |
|
76 if (!cursorList.containsKey(tcur.getSessionID())) { |
|
77 cursorList.put(tcur.getSessionID(), tcur); |
|
78 repaint(); |
|
79 } |
|
80 |
|
81 if (verbose) |
|
82 System.out.println("add cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY()); |
|
83 } |
|
84 |
|
85 public void updateTuioCursor(TuioCursor tcur) { |
|
86 |
|
87 repaint(); |
|
88 |
|
89 if (verbose) |
|
90 System.out.println("set cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY()+" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel()); |
|
91 } |
|
92 |
|
93 public void removeTuioCursor(TuioCursor tcur) { |
|
94 |
|
95 cursorList.remove(tcur.getSessionID()); |
|
96 repaint(); |
|
97 |
|
98 if (verbose) |
|
99 System.out.println("del cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+")"); |
|
100 } |
|
101 |
|
102 public void refresh(TuioTime frameTime) { |
|
103 repaint(); |
|
104 } |
|
105 |
|
106 public void paint(Graphics g) { |
|
107 update(g); |
|
108 } |
|
109 |
|
110 public void update(Graphics g) { |
|
111 |
|
112 Graphics2D g2 = (Graphics2D)g; |
|
113 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
|
114 g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); |
|
115 |
|
116 g2.setColor(Color.white); |
|
117 g2.fillRect(0,0,width,height); |
|
118 |
|
119 int w = (int)Math.round(width-scale*finger_size/2.0f); |
|
120 int h = (int)Math.round(height-scale*finger_size/2.0f); |
|
121 |
|
122 Enumeration<TuioCursor> cursors = cursorList.elements(); |
|
123 while (cursors.hasMoreElements()) { |
|
124 TuioCursor tcur = cursors.nextElement(); |
|
125 if (tcur==null) continue; |
|
126 Vector<TuioPoint> path = tcur.getPath(); |
|
127 TuioPoint current_point = path.elementAt(0); |
|
128 if (current_point!=null) { |
|
129 // draw the cursor path |
|
130 g2.setPaint(Color.blue); |
|
131 for (int i=0;i<path.size();i++) { |
|
132 TuioPoint next_point = path.elementAt(i); |
|
133 g2.drawLine(current_point.getScreenX(w), current_point.getScreenY(h), next_point.getScreenX(w), next_point.getScreenY(h)); |
|
134 current_point = next_point; |
|
135 } |
|
136 } |
|
137 |
|
138 // draw the finger tip |
|
139 g2.setPaint(Color.lightGray); |
|
140 int s = (int)(scale*finger_size); |
|
141 g2.fillOval(current_point.getScreenX(w-s/2),current_point.getScreenY(h-s/2),s,s); |
|
142 g2.setPaint(Color.black); |
|
143 g2.drawString(tcur.getCursorID()+"",current_point.getScreenX(w),current_point.getScreenY(h)); |
|
144 } |
|
145 |
|
146 // draw the objects |
|
147 Enumeration<TuioDemoObject> objects = objectList.elements(); |
|
148 while (objects.hasMoreElements()) { |
|
149 TuioDemoObject tobj = objects.nextElement(); |
|
150 if (tobj!=null) tobj.paint(g2, width,height); |
|
151 } |
|
152 } |
|
153 } |