00001 package edu.stanford.hci.r3.pen;
00002
00003 import java.io.Serializable;
00004 import java.text.DecimalFormat;
00005 import java.util.Date;
00006
00007 import edu.stanford.hci.r3.units.PatternDots;
00008 import edu.stanford.hci.r3.util.MathUtils;
00009
00022 public class PenSample implements Serializable {
00023
00027 private static final DecimalFormat FORMATTER = PatternDots.FORMATTER;
00028
00032 public int force;
00033
00037 public boolean penUp;
00038
00042 public long timestamp;
00043
00047 public double x;
00048
00052 public double y;
00053
00060 public PenSample(double xVal, double yVal, int f, long ts) {
00061 this(xVal, yVal, f, ts, false );
00062 }
00063
00072 public PenSample(double theX, double theY, int theForce, long theTimestamp, boolean isPenUp) {
00073 timestamp = theTimestamp;
00074 x = theX;
00075 y = theY;
00076 force = theForce;
00077 penUp = isPenUp;
00078 }
00079
00080 public double getDistanceFrom(PenSample otherSample) {
00081 return MathUtils.distance(x, y, otherSample.x, otherSample.y);
00082 }
00083
00087 public int getForce() {
00088 return force;
00089 }
00090
00094 public Date getTime() {
00095 return new Date(timestamp);
00096 }
00097
00101 public long getTimestamp() {
00102 return timestamp;
00103 }
00104
00108 public double getX() {
00109 return x;
00110 }
00111
00115 public double getY() {
00116 return y;
00117 }
00118
00122 public boolean isPenUp() {
00123 return penUp;
00124 }
00125
00129 public void setForce(int f) {
00130 force = f;
00131 }
00132
00139 public void setPenUp(boolean b) {
00140 penUp = b;
00141 }
00142
00146 public void setTimestamp(long t) {
00147 timestamp = t;
00148 }
00149
00153 public void setX(double xVal) {
00154 x = xVal;
00155 }
00156
00160 public void setY(double yVal) {
00161 y = yVal;
00162 }
00163
00167 public String toCommaSeparatedString() {
00168 final DecimalFormat df = new DecimalFormat("#.####");
00169 final String xString = df.format(x);
00170 final String yString = df.format(y);
00171 return "" + timestamp + "," + xString + "," + yString + "," + force + ","
00172 + (isPenUp() ? "UP" : "DOWN");
00173 }
00174
00178 public String toString() {
00179 return "Sample: [" + FORMATTER.format(x) + ", " + FORMATTER.format(y) + "] F=" + force + " T="
00180 + timestamp + " P=" + (isPenUp() ? "UP" : "DOWN");
00181 }
00182
00188 public String toXMLString() {
00189 return "<p x=\"" + x + "\" y=\"" + y + "\" f=\"" + force + "\" t=\"" + timestamp + "\" p=\""
00190 + (isPenUp() ? "U" : "D") + "\"/>";
00191 }
00192 }