Source of PolyLine.java


  1: //PolyLine.java
  2: 
  3: import java.io.*;
  4: 
  5: public class PolyLine implements Serializable
  6: {
  7:   //Construct a polyline from an array of coordinate pairs
  8:   public PolyLine(double[][] coords)
  9:   {
 10:     Point[] points = new Point[coords.length];  //Array to hold points
 11: 
 12:     //Create points from the coordinates
 13:     for(int i = 0; i < coords.length ; i++)
 14:       points[i] = new Point(coords[i][0], coords[i][1]);
 15: 
 16:     //Create the polyline from the array of points
 17:     polyline = new LinkedList(points);
 18:   }
 19: 
 20:   //Construct a polyline from an array of points
 21:   public PolyLine(Point[] points)
 22:   {
 23:     polyline = new LinkedList(points);      //Create the polyline
 24:   }
 25: 
 26:   //Add a Point object to the list
 27:   public void addPoint(Point point)
 28:   {
 29:     polyline.addItem(point);                //Add the point to the list
 30:   }
 31: 
 32:   //Add a point from a coordinate pair to the list
 33:   public void addPoint(double x, double y)
 34:   {
 35:     polyline.addItem(new Point(x, y));     //Add the point to the list
 36:   }
 37: 
 38:   //String representation of a polyline
 39:   public String toString()
 40:   {
 41:     StringBuffer str = new StringBuffer("Polyline:");
 42:     Point point = (Point) polyline.getFirst();
 43:                                             //Set the 1st point as start
 44:     while(point != null)
 45:     {
 46:       str.append(" ("+ point+ ")");         //Append the current point
 47:       point = (Point)polyline.getNext();    //Make the next point current
 48:     }
 49:     return str.toString();
 50:   }
 51: 
 52:   private LinkedList polyline;              //The linked list of points
 53: }