Java Reference
In-Depth Information
Example 13−2: Scribble.java (continued)
* and parse() methods write and read this string format
**/
public class Scribble implements Shape, Transferable, Serializable, Cloneable {
protected double[] points = new double[64]; // The scribble data
protected int numPoints = 0;
// The current number of points
double maxX = Double.NEGATIVE_INFINITY;
// The bounding box
double maxY = Double.NEGATIVE_INFINITY;
double minX = Double.POSITIVE_INFINITY;
double minY = Double.POSITIVE_INFINITY;
/**
* Begin a new polyline at (x,y). Note the use of Double.NaN in the
* points array to mark the beginning of a new polyline
**/
public void moveto(double x, double y) {
if (numPoints + 3 > points.length) reallocate();
// Mark this as the beginning of a new line
points[numPoints++] = Double.NaN;
// The rest of this method is just like lineto();
lineto(x, y);
}
/**
* Add the point (x,y) to the end of the current polyline
**/
public void lineto(double x, double y) {
if (numPoints + 2 > points.length) reallocate();
points[numPoints++] = x;
points[numPoints++] = y;
// See if the point enlarges our bounding box
if (x > maxX) maxX = x;
if (x < minX) minX = x;
if (y > maxY) maxY = y;
if (y < minY) minY = y;
}
/**
* Append the Scribble s to this Scribble
**/
public void append(Scribble s) {
int n = numPoints + s.numPoints;
double[] newpoints = new double[n];
System.arraycopy(points, 0, newpoints, 0, numPoints);
System.arraycopy(s.points, 0, newpoints, numPoints, s.numPoints);
points = newpoints;
numPoints = n;
minX = Math.min(minX, s.minX);
maxX = Math.max(maxX, s.maxX);
minY = Math.min(minY, s.minY);
maxY = Math.max(maxY, s.maxY);
}
/**
* Translate the coordinates of all points in the Scribble by x,y
**/
public void translate(double x, double y) {
for(int i = 0; i < numPoints; i++) {
Search WWH ::




Custom Search