/** {@inheritDoc} */ public boolean setBounds( final double x, final double y, final double width, final double height) { if (lineShape == null || !super.setBounds(x, y, width, height)) { return false; } final Rectangle2D lineBounds = lineShape.getBounds2D(); final Rectangle2D lineStrokeBounds = getLineBoundsWithStroke(); final double strokeOutset = Math.max( lineStrokeBounds.getWidth() - lineBounds.getWidth(), lineStrokeBounds.getHeight() - lineBounds.getHeight()); double adjustedX = x + strokeOutset / 2; double adjustedY = y + strokeOutset / 2; double adjustedWidth = width - strokeOutset; double adjustedHeight = height - strokeOutset; TEMP_TRANSFORM.setToIdentity(); TEMP_TRANSFORM.translate(adjustedX, adjustedY); TEMP_TRANSFORM.scale( adjustedWidth / lineBounds.getWidth(), adjustedHeight / lineBounds.getHeight()); TEMP_TRANSFORM.translate(-lineBounds.getX(), -lineBounds.getY()); lineShape.transformPoints(TEMP_TRANSFORM); return true; }
/** * Calculates the bounds of the line taking stroke width into account. * * @return rectangle representing the bounds of the line taking stroke width into account */ public Rectangle2D getLineBoundsWithStroke() { if (stroke != null) { return stroke.createStrokedShape(lineShape).getBounds2D(); } else { return lineShape.getBounds2D(); } }
/** Recalculates the bounds when a change to the underlying line occurs. */ public void updateBoundsFromLine() { if (lineShape.getPointCount() == 0) { resetBounds(); } else { final Rectangle2D b = getLineBoundsWithStroke(); super.setBounds(b.getX(), b.getY(), b.getWidth(), b.getHeight()); } }
/** * Returns the point at the provided index. If dst is not null, it will populate it with the * point's coordinates rather than create a new point. * * @param pointIndex index of desired point in line * @param dst point to populate, may be null * @return the desired point, or dst populate with its coordinates */ public Point2D getPoint(final int pointIndex, final Point2D dst) { final Point2D result; if (dst == null) { result = new Point2D.Double(); } else { result = dst; } return lineShape.getPoint(pointIndex, result); }
/** {@inheritDoc} */ public boolean intersects(final Rectangle2D aBounds) { if (super.intersects(aBounds)) { if (lineShape.intersects(aBounds)) { return true; } else if (stroke != null && strokePaint != null) { return stroke.createStrokedShape(lineShape).intersects(aBounds); } } return false; }
/** Removes all points from the underlying line. */ public void removeAllPoints() { lineShape.removePoints(0, lineShape.getPointCount()); lineChanged(); }
/** * Removes points from the line. * * @param startIndex index from which to remove the points * @param numberOfPoints number of points to remove */ public void removePoints(final int startIndex, final int numberOfPoints) { lineShape.removePoints(startIndex, numberOfPoints); lineChanged(); }
/** * Inserts a point at the provided index. * * @param pointIndex index at which to add the point * @param x x component of new point * @param y y component of new point */ public void addPoint(final int pointIndex, final double x, final double y) { lineShape.addPoint(pointIndex, x, y); lineChanged(); }
/** * Returns the number of points in the line. * * @return number of points in the line */ public int getPointCount() { return lineShape.getPointCount(); }