📄 rectangle2d.java
字号:
//#ifdef j2se public Rectangle2D getBounds2D() { return (Rectangle2D) clone(); }//#else public abstract Rectangle2D getBounds2D();//#endif /** * Tests if a specified coordinate is inside the boundary of this * <code>Rectangle2D</code>. * @param x, y the coordinates to test * @return <code>true</code> if the specified coordinates are * inside the boundary of this <code>Rectangle2D</code>; * <code>false</code> otherwise. */ public boolean contains(double x, double y) { double x0 = getX(); double y0 = getY(); return (x >= x0 && y >= y0 && x < x0 + getWidth() && y < y0 + getHeight()); } /** * Tests if the interior of this <code>Rectangle2D</code> * intersects the interior of a specified set of rectangular * coordinates. * @param x, y the coordinates of the upper left corner * of the specified set of rectangular coordinates * @param w the width of the specified set of rectangular * coordinates * @param h the height of the specified set of rectangular * coordinates * @return <code>true</code> if this <code>Rectangle2D</code> * intersects the interior of a specified set of rectangular * coordinates; <code>false</code> otherwise. */ public boolean intersects(double x, double y, double w, double h) { if (isEmpty() || w <= 0 || h <= 0) { return false; } double x0 = getX(); double y0 = getY(); return (x + w > x0 && y + h > y0 && x < x0 + getWidth() && y < y0 + getHeight()); } /** * Tests if the interior of this <code>Rectangle2D</code> entirely * contains the specified set of rectangular coordinates. * @param x, y the coordinates of the upper left corner * of the specified set of rectangular coordinates * @param w the width of the specified set of rectangular * coordinates * @param h the height of the specified set of rectangular * coordinates * @return <code>true</code> if this <code>Rectangle2D</code> * entirely contains specified set of rectangular * coordinates; <code>false</code> otherwise. */ public boolean contains(double x, double y, double w, double h) { if (isEmpty() || w <= 0 || h <= 0) { return false; } double x0 = getX(); double y0 = getY(); return (x >= x0 && y >= y0 && (x + w) <= x0 + getWidth() && (y + h) <= y0 + getHeight()); } /** * Returns a new <code>Rectangle2D</code> object representing the * intersection of this <code>Rectangle2D</code> with the specified * <code>Rectangle2D</code>. * @param r the <code>Rectangle2D</code> to be intersected with * this <code>Rectangle2D</code> * @return the largest <code>Rectangle2D</code> contained in both * the specified <code>Rectangle2D</code> and in this * <code>Rectangle2D</code>. */ public abstract Rectangle2D createIntersection(Rectangle2D r); /** * Intersects the pair of specified source <code>Rectangle2D</code> * objects and puts the result into the specified destination * <code>Rectangle2D</code> object. One of the source rectangles * can also be the destination to avoid creating a third Rectangle2D * object, but in this case the original points of this source * rectangle will be overwritten by this method. * @param src1 the first of a pair of <code>Rectangle2D</code> * objects to be intersected with each other * @param src2 the second of a pair of <code>Rectangle2D</code> * objects to be intersected with each other * @param dest the <code>Rectangle2D</code> that holds the * results of the intersection of <code>src1</code> and * <code>src2</code> */ public static void intersect(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest) { double x1 = Math.max(src1.getMinX(), src2.getMinX()); double y1 = Math.max(src1.getMinY(), src2.getMinY()); double x2 = Math.min(src1.getMaxX(), src2.getMaxX()); double y2 = Math.min(src1.getMaxY(), src2.getMaxY()); dest.setFrame(x1, y1, x2-x1, y2-y1); } /** * Returns a new <code>Rectangle2D</code> object representing the * union of this <code>Rectangle2D</code> with the specified * <code>Rectangle2D</code>. * @param r the <code>Rectangle2D</code> to be combined with * this <code>Rectangle2D</code> * @return the smallest <code>Rectangle2D</code> containing both * the specified <code>Rectangle2D</code> and this * <code>Rectangle2D</code>. */ public abstract Rectangle2D createUnion(Rectangle2D r); /** * Unions the pair of source <code>Rectangle2D</code> objects * and puts the result into the specified destination * <code>Rectangle2D</code> object. One of the source rectangles * can also be the destination to avoid creating a third Rectangle2D * object, but in this case the original points of this source * rectangle will be overwritten by this method. * @param src1 the first of a pair of <code>Rectangle2D</code> * objects to be combined with each other * @param src2 the second of a pair of <code>Rectangle2D</code> * objects to be combined with each other * @param dest the <code>Rectangle2D</code> that holds the * results of the union of <code>src1</code> and * <code>src2</code> */ public static void union(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest) { double x1 = Math.min(src1.getMinX(), src2.getMinX()); double y1 = Math.min(src1.getMinY(), src2.getMinY()); double x2 = Math.max(src1.getMaxX(), src2.getMaxX()); double y2 = Math.max(src1.getMaxY(), src2.getMaxY()); dest.setFrameFromDiagonal(x1, y1, x2, y2); } /** * Adds a point, specified by the double precision arguments * <code>newx</code> and <code>newy</code>, to this * <code>Rectangle2D</code>. The resulting <code>Rectangle2D</code> * is the smallest <code>Rectangle2D</code> that * contains both the original <code>Rectangle2D</code> and the * specified point. * <p> * After adding a point, a call to <code>contains</code> with the * added point as an argument does not necessarily return * <code>true</code>. The <code>contains</code> method does not * return <code>true</code> for points on the right or bottom * edges of a rectangle. Therefore, if the added point falls on * the left or bottom edge of the enlarged rectangle, * <code>contains</code> returns <code>false</code> for that point. * @param newx, newy the coordinates of the new point */ public void add(double newx, double newy) { double x1 = Math.min(getMinX(), newx); double x2 = Math.max(getMaxX(), newx); double y1 = Math.min(getMinY(), newy); double y2 = Math.max(getMaxY(), newy); setRect(x1, y1, x2 - x1, y2 - y1); } /** * Adds the <code>Point2D</code> object <code>pt</code> to this * <code>Rectangle2D</code>. * The resulting <code>Rectangle2D</code> is the smallest * <code>Rectangle2D</code> that contains both the original * <code>Rectangle2D</code> and the specified <code>Point2D</code>. * <p> * After adding a point, a call to <code>contains</code> with the * added point as an argument does not necessarily return * <code>true</code>. The <code>contains</code> * method does not return <code>true</code> for points on the right * or bottom edges of a rectangle. Therefore, if the added point falls * on the left or bottom edge of the enlarged rectangle, * <code>contains</code> returns <code>false</code> for that point. * @param pt the new <code>Point2D</code> to add to this * <code>Rectangle2D</code>. */ public void add(Point2D pt) { add(pt.getX(), pt.getY()); } /** * Adds a <code>Rectangle2D</code> object to this * <code>Rectangle2D</code>. The resulting <code>Rectangle2D</code> * is the union of the two <code>Rectangle2D</code> objects. * @param r the <code>Rectangle2D</code> to add to this * <code>Rectangle2D</code>. */ public void add(Rectangle2D r) { double x1 = Math.min(getMinX(), r.getMinX()); double x2 = Math.max(getMaxX(), r.getMaxX()); double y1 = Math.min(getMinY(), r.getMinY()); double y2 = Math.max(getMaxY(), r.getMaxY()); setRect(x1, y1, x2 - x1, y2 - y1); }//#ifdef notdef /** * Returns an iteration object that defines the boundary of this * <code>Rectangle2D</code>. * The iterator for this class is multi-threaded safe, which means * that this <code>Rectangle2D</code> class guarantees that * modifications to the geometry of this <code>Rectangle2D</code> * object do not affect any iterations of that geometry that * are already in process. * @param at an optional <code>AffineTransform</code> to be applied to * the coordinates as they are returned in the iteration, or * <code>null</code> if untransformed coordinates are desired * @return the <code>PathIterator</code> object that returns the * geometry of the outline of this * <code>Rectangle2D</code>, one segment at a time. */ public PathIterator getPathIterator(AffineTransform at) { return new RectIterator(this, at); } /** * Returns an iteration object that defines the boundary of the * flattened <code>Rectangle2D</code>. Since rectangles are already * flat, the <code>flatness</code> parameter is ignored. * The iterator for this class is multi-threaded safe, which means * that this <code>Rectangle2D</code> class guarantees that * modifications to the geometry of this <code>Rectangle2D</code> * object do not affect any iterations of that geometry that * are already in process. * @param at an optional <code>AffineTransform</code> to be applied to * the coordinates as they are returned in the iteration, or * <code>null</code> if untransformed coordinates are desired * @param flatness the maximum distance that the line segments used to * approximate the curved segments are allowed to deviate from any * point on the original curve. Since rectangles are already flat, * the <code>flatness</code> parameter is ignored. * @return the <code>PathIterator</code> object that returns the * geometry of the outline of this * <code>Rectangle2D</code>, one segment at a time. */ public PathIterator getPathIterator(AffineTransform at, double flatness) { return new RectIterator(this, at); }//#endif public abstract double getX(); public abstract double getY(); public abstract double getWidth(); public abstract double getHeight(); public abstract boolean isEmpty(); public double getMinX() { return getX(); } public double getMaxX() { return getX() + getWidth(); } public double getMinY() { return getY(); } public double getMaxY() { return getX() + getWidth(); } public void setFrameFromDiagonal(double x1, double y1, double x2, double y2) { if (x2 < x1) { double t = x1; x1 = x2; x2 = t; } if (y2 < y1) { double t = y1; y1 = y2; y2 = t; } setFrame(x1, y1, x2 - x1, y2 - y1); } /** * Returns the hashcode for this <code>Rectangle2D</code>. * @return the hashcode for this <code>Rectangle2D</code>. */ public int hashCode() { long bits = java.lang.Double.doubleToLongBits(getX()); bits += java.lang.Double.doubleToLongBits(getY()) * 37; bits += java.lang.Double.doubleToLongBits(getWidth()) * 43; bits += java.lang.Double.doubleToLongBits(getHeight()) * 47; return (((int) bits) ^ ((int) (bits >> 32))); } /** * Determines whether or not the specified <code>Object</code> is * equal to this <code>Rectangle2D</code>. The specified * <code>Object</code> is equal to this <code>Rectangle2D</code> * if it is an instance of <code>Rectangle2D</code> and if its * location and size are the same as this <code>Rectangle2D</code>. * @param obj an <code>Object</code> to be compared with this * <code>Rectangle2D</code>. * @return <code>true</code> if <code>obj</code> is an instance * of <code>Rectangle2D</code> and has * the same values; <code>false</code> otherwise. */ public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof Rectangle2D) { Rectangle2D r2d = (Rectangle2D) obj; return ((getX() == r2d.getX()) && (getY() == r2d.getY()) && (getWidth() == r2d.getWidth()) && (getHeight() == r2d.getHeight())); } return false; }}//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -