Example 23.21: The Line class

Example 23.21 is a class that represents a line, or, more precisely, a line segment. The line is stored as two Point objects which represent it endpoints. Notice that the draw method uses the Point class's non-public fields x, y and z. This is permissible because the Line class and the Point class are in the same package, elharo.vrml.

package elharo.vrml;

public class Line extends shape {

  // a line is defined by two points
  Point p1;
  Point p2;

  public Line(Point p1, Point p2) {
    this.p1 = p1;
    this.p2 = p2;
  }

  public Line(double x1, double y1, double z1,
   double x2, double y2, double z2) {
    p1 = new Point(x1, y1, z1);
    p2 = new Point(x2, y2, z2);
  }

  public String draw() {
  
    String s1 = "Coordinate3 { point [ ";  
    String s2 = p1.x + " " + p1.y + " " + p1.z + ",\n";
    String s3 = p2.x + " " + p2.y + " " + p2.z + ",\n";
    String s4 = "] }\n\n"; 
    String s5 = "IndexedLineSet {\n coordIndex [ " +
      "0, 1\n] \n}";       
  
    return s1 + s2 + s3 + s4 + s5;
    
  }

}

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home