Class Line

All Implemented Interfaces:
Strokable

public class Line extends GraphicsObject implements Strokable
A line segment that can be drawn on the screen.

A line has a "start point" and an "end point". The only distinction is that the start point is the line’s position. If you call setPosition() or moveBy(), both endpoints will move so the line segment has the same length and angle, and the start point will end up at the position you specify.

To move one endpoint without affecting the other, use setStartPosition() and setEndPosition().

  • Constructor Details

    • Line

      public Line(double x1, double y1, double x2, double y2)
      Creates a line starting at position (x1,y1) and ending at (x2,y2). The line has a 1 pixel black stroke outline by default.
      Parameters:
      x1 - x position of starting point
      y1 - y position of starting point
      x2 - x position of ending point
      y2 - y position of ending point
    • Line

      public Line(Point point1, Point point2)
      Creates a line starting at point1 and ending at point2. The line has a 1 pixel black stroke outline by default.
  • Method Details

    • drawInLocalCoordinates

      protected void drawInLocalCoordinates(Graphics2D gc)
      Description copied from class: GraphicsObject
      For internal use. Draws this graphics object on the screen in its local coordinates, without rotation or scaling.
      Specified by:
      drawInLocalCoordinates in class GraphicsObject
    • getStrokeColor

      public Paint getStrokeColor()
      Description copied from interface: Strokable
      Returns the color to strokeColor that will be used to stroke this shape if it is stroked. Note that there will still be no stroke if isStroked() is false or the stroke width is zero.
      Specified by:
      getStrokeColor in interface Strokable
    • setStrokeColor

      public void setStrokeColor(Paint strokeColor)
      Description copied from interface: Strokable
      Sets the stroke color to the given color. Automatically calls setStroked(true).
      Specified by:
      setStrokeColor in interface Strokable
    • getStrokeWidth

      public double getStrokeWidth()
      Description copied from interface: Strokable
      Returns the thickness of the stroke in pixels.
      Specified by:
      getStrokeWidth in interface Strokable
    • setStrokeWidth

      public void setStrokeWidth(double width)
      Description copied from interface: Strokable
      Sets how thick the stroke is. Automatically calls setStroked(true).
      Specified by:
      setStrokeWidth in interface Strokable
    • isStroked

      public boolean isStroked()
      Description copied from interface: Strokable
      Returns true if the interior of this shape will be stroked with the stroke color.
      Specified by:
      isStroked in interface Strokable
    • setStroked

      public void setStroked(boolean stroked)
      Description copied from interface: Strokable
      Causes the shape to be stroked with the stroke color when it is drawn.
      Specified by:
      setStroked in interface Strokable
    • getX1

      public double getX1()
      Get the shape's x1 position
      Returns:
      x position of starting point
    • getY1

      public double getY1()
      Get the shape's y1 position
      Returns:
      y position of starting point
    • getX2

      public double getX2()
      Get the shape's x2 position
      Returns:
      x position of ending point
    • getY2

      public double getY2()
      Get the shape's y2 position
      Returns:
      y position of ending point
    • setStartPosition

      public void setStartPosition(double x, double y)
      Sets the line's starting position to (x, y) without affecting the end position.
    • setStartPosition

      public void setStartPosition(Point p)
      Sets the line's starting position to the given point without affecting the end position.
    • setEndPosition

      public void setEndPosition(double x, double y)
      Sets the line's ending position to (x, y) without affecting the start position.
    • setEndPosition

      public void setEndPosition(Point p)
      Sets the line's ending position to the given point without affecting the end position.
    • testHitInLocalCoordinates

      public boolean testHitInLocalCoordinates(double x, double y)
      Description copied from class: GraphicsObject
      Tests whether the given point is inside the boundary or interior of this object’s shape. The point is in this object’s local coordinates.
      Specified by:
      testHitInLocalCoordinates in class GraphicsObject
    • getBounds

      public Rectangle2D getBounds()
      Description copied from class: GraphicsObject
      Returns the bounding box of this graphics object in its local coordinates.
      Specified by:
      getBounds in class GraphicsObject
    • getEqualityAttributes

      protected Object getEqualityAttributes()
      Description copied from class: GraphicsObject
      For internal use only. Used to compute equals() and hashCode(). Returns an object whose equals() and hashCode() methods encompass the subclass-specific values that should be used to compute equality for the whole GraphicsObject.
      Specified by:
      getEqualityAttributes in class GraphicsObject
    • toString

      public String toString()
      Overrides:
      toString in class Object