⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 polyline.java

📁 Beginning Java 2, SDK 1.4 Edition Exercise Code samples for this book
💻 JAVA
字号:
// Chapter 6 Exercises 1 & 2

// DEFINES A POLYLINE AT POINT POSITION IN THE BASE CLASS.
// We will define the second and subsequent points relative to
// position so that the inherited move() method works.

public class PolyLine extends Shape {
  private ListPoint start;     // Second point in the polyline.
  private ListPoint end;       // Last ListPoint in the list.

  // Construct a polyline from an array of points:
  public PolyLine(Point[] points) {
    if(points.length<2) {
      System.out.println("\nAt least two points are required to define a polyline"+
                         "\nProgram terminated");
      System.exit(1);                   // Exit the program.
    }

    // Create a one point list:
    position = new Point(points[0]);    // 1st point is the reference point.

    // Create a one point list of the second point:
    start = new ListPoint(points[1].x-position.x, points[1].y-position.y);      
    end = start;                        // end is the same as the start.

    // Now add the other points:
    for(int i = 2; i < points.length; i++)
      addPoint(points[i]);
  }

  // Add a Point object to the list:
  public void addPoint(Point point) {
    // Create a new ListPoint:
    ListPoint newEnd = new ListPoint(point.x-position.x, point.y-position.y);
    end.setNext(newEnd);    // Set next variable for old end as new end.
    end = newEnd;           // Store new point as end.
  }

  // Overrrides method inherited from Object:
  public String toString() {
    String str = "PolyLine:\n" + position + " ";
    
    int count = 1;                        // Count of points.
    ListPoint nextPoint = start;	  // Set the 1st point as start.

    while(nextPoint != null) {
      str += nextPoint.add(position)+" "; // Add the string representation of the next point.

      if(++count%4==0)                    // After every fourth point
        str += "\n";                      // add a newline character.
      nextPoint = nextPoint.getNext();    // Get the reference to the next ListPoint.
    }
    return str;                           // Return the string.	
  }

  // Output the polyline:
  public void show() {
    System.out.println("\n" + toString());
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -