-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandTrail.java
executable file
·107 lines (86 loc) · 3.21 KB
/
HandTrail.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// HandTrail.java
// Andrew Davison, November 2011, [email protected]
/* Store hand coordinates in a list (up to a maximum of MAX_POINTS)
and draw them as a trail (actually a polyline).
When MAX_POINTS is reached, the oldest point (at position 0) is
discarded, causing the rendering to show a trail of previous hand
points.
*/
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import org.OpenNI.DepthGenerator;
import org.OpenNI.Point3D;
import org.OpenNI.StatusException;
public class HandTrail {
private static final int MAX_POINTS = 20;
private static final int CIRCLE_SIZE = 20;
private static final int STROKE_SIZE = 15;
private static final Color POINT_COLORS[] = { Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA, Color.PINK, Color.YELLOW };
private Font msgFont; // for the hand ID string
private int handID;
private DepthGenerator depthGen;
private List<Point> coords; // the points that form the trail
public HandTrail(int id, DepthGenerator dg) {
handID = id;
depthGen = dg;
msgFont = new Font("SansSerif", Font.BOLD, 24);
coords = new ArrayList<Point>();
} // end of HandTrail()
public synchronized void addPoint(Point3D realPt)
// add hand coordinate to the coords list
{
try {
Point3D pt = depthGen.convertRealWorldToProjective(realPt);
// convert real-world coordinates to screen form
if (pt == null)
return;
coords.add(new Point((int) pt.getX(), (int) pt.getY())); // discard
// z coord
if (coords.size() > MAX_POINTS) // get rid of the oldest point
coords.remove(0);
} catch (StatusException e) {
System.out.println("Problem converting point");
}
} // end of addPoint()
public synchronized void draw(Graphics2D g2)
/*
* draw trail and large circle on hand (the last point) with the hand ID in
* the center
*/
{
int numPoints = coords.size();
if (numPoints == 0)
return;
drawTrail(g2, coords, numPoints);
// draw large circle on hand (the last point)
Point pt = coords.get(numPoints - 1);
g2.setColor(POINT_COLORS[(handID + 1) % POINT_COLORS.length]);
g2.fillOval(pt.x - CIRCLE_SIZE / 2, pt.y - CIRCLE_SIZE / 2, CIRCLE_SIZE, CIRCLE_SIZE);
// draw the hand ID
g2.setColor(Color.WHITE);
g2.setFont(msgFont);
g2.drawString("" + handID, pt.x - 6, pt.y + 6); // roughly centered
} // end of draw()
private void drawTrail(Graphics2D g2, List<Point> coords, int numPoints)
// draw (x,y) Points list as a polyline (trail)
{
int[] xCoords = new int[numPoints];
int[] yCoords = new int[numPoints];
// fill the integer arrays for x and y points
Point pt;
for (int i = 0; i < numPoints; i++) {
pt = coords.get(i);
xCoords[i] = pt.x;
yCoords[i] = pt.y;
}
g2.setColor(POINT_COLORS[handID % POINT_COLORS.length]);
g2.setStroke(new BasicStroke(STROKE_SIZE));
// System.out.println("No of draw points: " + numPoints);
g2.drawPolyline(xCoords, yCoords, numPoints);
} // end of drawTrail()
} // end of HandTrail class