Package edu.macalester.graphics
Class Line
java.lang.Object
edu.macalester.graphics.GraphicsObject
edu.macalester.graphics.Line
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidFor internal use.Returns the bounding box of this graphics object in its local coordinates.Returns the endpoint of the line segment marked as the “end,” which is (x2, y2).protected ObjectFor internal use only.Returns the endpoint of the line segment marked as the “start,” which is (x1, y1).Returns the color to strokeColor that will be used to stroke this shape if it is stroked.doubleReturns the thickness of the stroke in pixels.doublegetX1()Get the shape's x1 positiondoublegetX2()Get the shape's x2 positiondoublegetY1()Get the shape's y1 positiondoublegetY2()Get the shape's y2 positionbooleanReturns true if the interior of this shape will be stroked with the stroke color.voidsetEndPosition(double x, double y) Sets the line's ending position to (x, y) without affecting the start position.voidSets the line's ending position to the given point without affecting the end position.voidsetStartPosition(double x, double y) Sets the line's starting position to (x, y) without affecting the end position.voidSets the line's starting position to the given point without affecting the end position.voidsetStrokeColor(Paint strokeColor) Sets the stroke color to the given color.voidsetStroked(boolean stroked) Causes the shape to be stroked with the stroke color when it is drawn.voidsetStrokeWidth(double width) Sets how thick the stroke is.booleantestHitInLocalCoordinates(double x, double y) Tests whether the given point is inside the boundary or interior of this object’s shape.toString()Methods inherited from class edu.macalester.graphics.GraphicsObject
addObserver, changed, equals, getAnchor, getBoundsInParent, getCanvas, getCenter, getElementAt, getElementAtLocalCoordinates, getEmbeddedComponent, getHeight, getParent, getPosition, getRotation, getScale, getScaleX, getScaleY, getSize, getSizeInParent, getWidth, getX, getY, hashCode, isInBounds, moveBy, moveBy, removeObserver, renderToBuffer, rotateBy, setAnchor, setAnchor, setCenter, setCenter, setPosition, setPosition, setRotation, setScale, setScale, setScale, setX, setY, testHit
-
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 pointy1- y position of starting pointx2- x position of ending pointy2- y position of ending point
-
Line
Creates a line starting at point1 and ending at point2. The line has a 1 pixel black stroke outline by default.
-
-
Method Details
-
drawInLocalCoordinates
Description copied from class:GraphicsObjectFor internal use. Draws this graphics object on the screen in its local coordinates, without rotation or scaling.- Specified by:
drawInLocalCoordinatesin classGraphicsObject
-
getStrokeColor
Description copied from interface:StrokableReturns 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:
getStrokeColorin interfaceStrokable
-
setStrokeColor
Description copied from interface:StrokableSets the stroke color to the given color. Automatically calls setStroked(true).- Specified by:
setStrokeColorin interfaceStrokable
-
getStrokeWidth
public double getStrokeWidth()Description copied from interface:StrokableReturns the thickness of the stroke in pixels.- Specified by:
getStrokeWidthin interfaceStrokable
-
setStrokeWidth
public void setStrokeWidth(double width) Description copied from interface:StrokableSets how thick the stroke is. Automatically calls setStroked(true).- Specified by:
setStrokeWidthin interfaceStrokable
-
isStroked
public boolean isStroked()Description copied from interface:StrokableReturns true if the interior of this shape will be stroked with the stroke color. -
setStroked
public void setStroked(boolean stroked) Description copied from interface:StrokableCauses the shape to be stroked with the stroke color when it is drawn.- Specified by:
setStrokedin interfaceStrokable
-
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
-
getStartPosition
Returns the endpoint of the line segment marked as the “start,” which is (x1, y1). This method is equivalent to getPosition(), but note that callingsetStartPosition(Point)moves only that endpoint, whereas callingGraphicsObject.setPosition(Point)moves the entire line (i.e. it moves both endpoints by the same amount).- See Also:
-
setStartPosition
public void setStartPosition(double x, double y) Sets the line's starting position to (x, y) without affecting the end position. -
setStartPosition
Sets the line's starting position to the given point without affecting the end position. -
getEndPosition
Returns the endpoint of the line segment marked as the “end,” which is (x2, y2).- See Also:
-
setEndPosition
public void setEndPosition(double x, double y) Sets the line's ending position to (x, y) without affecting the start position. -
setEndPosition
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:GraphicsObjectTests 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:
testHitInLocalCoordinatesin classGraphicsObject
-
getBounds
Description copied from class:GraphicsObjectReturns the bounding box of this graphics object in its local coordinates.- Specified by:
getBoundsin classGraphicsObject
-
getEqualityAttributes
Description copied from class:GraphicsObjectFor 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:
getEqualityAttributesin classGraphicsObject
-
toString
-