Java Reference
In-Depth Information
Example 13−2: Scribble.java (continued)
if (Double.isNaN(points[i])) continue;
points[i++] += x;
points[i] += y;
}
minX += x; maxX += x;
minY += y; maxY += y;
}
/** An internal method to make more room in the data array */
protected void reallocate() {
double[] newpoints = new double[points.length * 2];
System.arraycopy(points, 0, newpoints, 0, numPoints);
points = newpoints;
}
/** Clone a Scribble object and its internal array of data */
public Object clone() {
try {
Scribble s = (Scribble) super.clone(); // make a copy of all fields
s.points = (double[]) points.clone(); // copy the entire array
return s;
}
catch (CloneNotSupportedException e) { // This should never happen
return this;
}
}
/** Convert the scribble data to a textual format */
public String toString() {
StringBuffer b = new StringBuffer();
for(int i = 0; i < numPoints; i++) {
if (Double.isNaN(points[i])) {
b.append("m ");
}
else {
b.append(points[i]);
b.append(' ');
}
}
return b.toString();
}
/**
* Create a new Scribble object and initialize it by parsing a string of
* coordinate data in the format produced by toString()
**/
public static Scribble parse(String s) throws NumberFormatException {
StringTokenizer st = new StringTokenizer(s);
Scribble scribble = new Scribble();
while(st.hasMoreTokens()) {
String t = st.nextToken();
if (t.charAt(0) == 'm') {
scribble.moveto(Double.parseDouble(st.nextToken()),
Double.parseDouble(st.nextToken()));
}
else {
scribble.lineto(Double.parseDouble(t),
Double.parseDouble(st.nextToken()));