/** * Check if a {@link Point} is inside the bounds. * * @param point * - a {@link Point} that needs to be contained in the bounds * @return true if the bounds contain the point, false otherwise */ public boolean contains(Point point) { Point lowerRightCorner = getLowerRightCorner(); boolean containsPointX = point.getX() >= upperLeftCorner.getX() && point.getX() <= lowerRightCorner.getX(); boolean containsPointY = point.getY() >= upperLeftCorner.getY() && point.getY() <= lowerRightCorner.getY(); return containsPointX && containsPointY; }
/** * Gets lower left corner. * * @return the lower left corner of the bounds */ public Point getLowerLeftCorner() { int lowerLeftCornerX = upperLeftCorner.getX(); int lowerLeftCornerY = upperLeftCorner.getY() + height; Point lowerLeftCorner = new Point(lowerLeftCornerX, lowerLeftCornerY); return lowerLeftCorner; }
/** * Check if certain {@link Bounds} parameter is inside the bounds. * * @param bounds * - a {@link Bounds} parameter that needs to be contained in the bounds * @return true if the bounds contain the bounds parameter, false otherwise */ public boolean contains(Bounds bounds) { Point boundsUpperLeftCorner = bounds.getUpperLeftCorner(); int boundsWidth = bounds.getWidth(); int boundsHeight = bounds.getHeight(); boolean containsUpperLeftCorner = contains(boundsUpperLeftCorner); boolean containsWidth = boundsUpperLeftCorner.getX() + boundsWidth <= upperLeftCorner.getX() + width; boolean containsHeight = boundsUpperLeftCorner.getY() + boundsHeight <= upperLeftCorner.getY() + height; return containsUpperLeftCorner && containsWidth && containsHeight; }
/** * Gets the given bounds equivalent relative to the bounds upper left corner; * * @param bounds * - {@link Bounds} for which to get the relative bounds to the bounds upper left corner * @return the bounds equivalent relative to the bounds upper left corner */ public Bounds getRelativeBounds(Bounds bounds) { Point boundsUpperLeftCorner = bounds.getUpperLeftCorner(); int boundsWidth = bounds.getWidth(); int boundsHeight = bounds.getHeight(); Point relativeUpperLeftCorner = getRelativePoint(boundsUpperLeftCorner); Bounds relativeBounds = new Bounds(relativeUpperLeftCorner, boundsWidth, boundsHeight); return relativeBounds; }
/** * Sets the location of the Point object. * * @param point * - new location for the point. */ public void setLocation(Point point) { setLocation(point.getX(), point.getY()); }
/** * Gets upper left corner. * * @return the upper left corner of the bounds */ public Point getUpperLeftCorner() { return new Point(upperLeftCorner); }
/** * Gets lower right corner. * * @return the lower right corner of the bounds */ public Point getLowerRightCorner() { int lowerRightCornerX = upperLeftCorner.getX() + width; int lowerRightCornerY = upperLeftCorner.getY() + height; Point lowerRightCorner = new Point(lowerRightCornerX, lowerRightCornerY); return lowerRightCorner; }
/** * Sets upper left corner. * * @param upperLeftCorner * - the upper left corner of the bounds */ public void setUpperLeftCorner(Point upperLeftCorner) { this.upperLeftCorner = new Point(upperLeftCorner); }
/** * Gets upper right corner. * * @return the upper right corner of the bounds */ public Point getUpperRightCorner() { int upperRightCornerX = upperLeftCorner.getX() + width; int upperRightCornerY = upperLeftCorner.getY(); Point upperRightCorner = new Point(upperRightCornerX, upperRightCornerY); return upperRightCorner; }
/** * Creates new Bounds instance given upper left corner, width and height. * * @param upperLeftCorner * - the upper left corner of the bounds * @param width * - the width of the bounds * @param height * - the height of the bounds */ public Bounds(Point upperLeftCorner, int width, int height) { this.upperLeftCorner = new Point(upperLeftCorner); this.width = width; this.height = height; }
/** * Gets the given point equivalent relative to the bounds upper left corner. * * @param point * - a {@link Point} for which to get the point equivalent relative to the bounds upper left corner * @return the point equivalent relative to the bounds upper left corner */ public Point getRelativePoint(Point point) { int relativePointX = point.getX() - upperLeftCorner.getX(); int relativePointY = point.getY() - upperLeftCorner.getY(); Point relativePoint = new Point(relativePointX, relativePointY); return relativePoint; }
/** * Calculates the coordinates of a vector with start point A and end point B. * * @param pointA * - start point of the vector. * @param pointB * - end point of the vector. * @return the coordinates of a vector with start point A and end point B. */ public static Point getVector(Point pointA, Point pointB) { int vectorX = pointB.getX() - pointA.getX(); int vectorY = pointB.getY() - pointA.getY(); Point vector = new Point(vectorX, vectorY); return vector; }
/** * Creates new Bounds instance given upper left and lower right corners. * * @param upperLeftCorner * - the upper left corner of the bounds * @param lowerRightCorner * - the lower left corner of the bounds */ public Bounds(Point upperLeftCorner, Point lowerRightCorner) { int calculatedWidth = lowerRightCorner.getX() - upperLeftCorner.getX(); int calculatedHeight = lowerRightCorner.getY() - upperLeftCorner.getY(); this.upperLeftCorner = new Point(upperLeftCorner); this.width = calculatedWidth; this.height = calculatedHeight; }