Line and AngLine

The Line class is defined by two endpoints. The only property that it has that you can customize, other than the Shared Shape Methods, is the line width and stroke. This is discussed more in BasicStroke. The AngLine class is a special subclass of Line that makes it possible to create a Line given a start point, a length, and an angle, instead of a start point and an end point.

Table of Contents

1. Creating a Line or AngLine

The constructors of Line allow you to provide the start and end points either as Location objects, or as coordinate pairs:

public Line(double x1, double y1, double x2, double y2, Color color, DrawingCanvas canvas)
public Line(double x1, double y1, double x2, double y2, DrawingCanvas canvas)
public Line(Location start, Location end, Color color, DrawingCanvas canvas)
public Line(Location start, Location end, DrawingCanvas canvas)

If it would be more convenient to provide a start point, length, and angle, you can construct an AngLine instead:

public AngLine(Location start, double length, double radianAngle, Color color, DrawingCanvas canvas)
public AngLine(Location start, double length, double radianAngle, DrawingCanvas canvas)
public AngLine(double x, double y, double length, double radianAngle, Color color, DrawingCanvas canvas)
public AngLine(double x, double y, double length, double radianAngle, DrawingCanvas canvas)

Once an AngLine is constructed, it behaves just like a Line, with no new capabilities. In particular, it is not possible to retrieve the angle or length from it directly once it is made (although these are easy things to calculate - see Angle and Coordinates. So, the two lines created below will overlap:

Line line1 = new Line(100, 100, 150, 186.6, canvas);
Line line2 = new AngLine(100, 100, 100, -Math.PI / 3, canvas);

It is often convenient to use AngLine when creating some sort of polygon with sides at angles:

double angle = 0;
Location start = new Location(50, 180);
for(int i = 0; i < 5; i++) {
   Line line = new AngLine(start, 100, angle, canvas);
   angle -= 2 * Math.PI / 5;
   start = line.getEnd();
}

2. Getting and setting the properties of a Line

Except for the Shared Shape Methods and the ability to set your line width and BasicStroke, the only new methods you need to know to work with Line are those that get and set the start and end points:

public Location getStart();
public Location getEnd();
public void setStart(Location point);
public void setStart(double x, double y);
public void setEnd(Location point);
public void setEnd(double x, double y);
public void setEndPoints(Location start, Location end);
public void setEndPoints(double x1, double y1, double x2, double y2);

As with rectangular shapes, if you get one of the end Locations and then translate() it, it will move that part of the line.

3. Complete listing of the Line specification

Here are all the constructors and methods that Line has, with the exception of the Shared Shape Methods:

// Constructors:
public Line(double x1, double y1, double x2, double y2, Color color, DrawingCanvas canvas)
public Line(double x1, double y1, double x2, double y2, DrawingCanvas canvas)
public Line(Location start, Location end, Color color, DrawingCanvas canvas)
public Line(Location start, Location end, DrawingCanvas canvas)

public AngLine(Location start, double length, double radianAngle, Color color, DrawingCanvas canvas)
public AngLine(Location start, double length, double radianAngle, DrawingCanvas canvas)
public AngLine(double x, double y, double length, double radianAngle, Color color, DrawingCanvas canvas)

// Methods from Drawable1DInterface:
public Location getStart();
public Location getEnd();
public void setStart(Location point);
public void setStart(double x, double y);
public void setEnd(Location point);
public void setEnd(double x, double y);
public void setEndPoints(Location start, Location end);
public void setEndPoints(double x1, double y1, double x2, double y2);

// Methods from DrawableStrokeInterface:
public double getLineWidth();
public void setLineWidth(double width);
public BasicStroke getStroke();
public void setStroke(BasicStroke stroke);