📄 rectangle2d.java
字号:
/* * Copyright (C) 2005, 2006 data2c GmbH (www.data2c.com) * * Author: Wolfgang S. Kechel - wolfgang.kechel@data2c.com * * J2ME version of java.awt.geom.Rectangle2D. *///#ifndef j2sepackage org.awt.geom;/** * The <code>Rectangle2D</code> class describes a rectangle * defined by a location (x, y) and dimension * (w x h). * <p> * This class is only the abstract superclass for all objects that * store a 2D rectangle. * The actual storage representation of the coordinates is left to * the subclass. */public abstract class Rectangle2D//extends RectangularShape//implements Cloneable{ /** * The bitmask that indicates that a point lies to the left of * this <code>Rectangle2D</code>. */ public static final int OUT_LEFT = 1; /** * The bitmask that indicates that a point lies above * this <code>Rectangle2D</code>. */ public static final int OUT_TOP = 2; /** * The bitmask that indicates that a point lies to the right of * this <code>Rectangle2D</code>. */ public static final int OUT_RIGHT = 4; /** * The bitmask that indicates that a point lies below * this <code>Rectangle2D</code>. */ public static final int OUT_BOTTOM = 8; /** * The <code>Float</code> class defines a rectangle specified in float * coordinates. */ public static class Float extends Rectangle2D { /** * The x coordinate of this <code>Rectangle2D</code>. */ public float x; /** * The y coordinate of this <code>Rectangle2D</code>. */ public float y; /** * The width of this <code>Rectangle2D</code>. */ public float width; /** * The height of this <code>Rectangle2D</code>. */ public float height; /** * Constructs a new <code>Rectangle2D</code>, initialized to * location (0.0, 0.0) and size (0.0, 0.0). */ public Float() { } /** * Constructs and initializes a <code>Rectangle2D</code> * from the specified float coordinates. * @param x, y the coordinates of the * upper left corner of the newly constructed * <code>Rectangle2D</code> * @param w the width of the newly constructed * <code>Rectangle2D</code> * @param h the height of the newly constructed * <code>Rectangle2D</code> */ public Float(float x, float y, float w, float h) { setRect(x, y, w, h); }//#ifdef notdef public Object clone() /*throws CloneNotSupportedException*/ { return new Float(x,y,width,height); }//#endif /** * Returns the X coordinate of this <code>Rectangle2D</code> * in double precision. * @return the X coordinate of this <code>Rectangle2D</code>. */ public double getX() { return (double) x; } /** * Returns the Y coordinate of this <code>Rectangle2D</code> * in double precision. * @return the Y coordinate of this <code>Rectangle2D</code>. */ public double getY() { return (double) y; } /** * Returns the width of this <code>Rectangle2D</code> * in double precision. * @return the width of this <code>Rectangle2D</code>. */ public double getWidth() { return (double) width; } /** * Returns the height of this <code>Rectangle2D</code> * in double precision. * @return the height of this <code>Rectangle2D</code>. */ public double getHeight() { return (double) height; } /** * Determines whether or not this <code>Rectangle2D</code> * is empty. * @return <code>true</code> if this <code>Rectangle2D</code> * is empty; <code>false</code> otherwise. */ public boolean isEmpty() { return (width <= 0.0f) || (height <= 0.0f); } /** * Sets the location and size of this <code>Rectangle2D</code> * to the specified float values. * @param x, y the coordinates to which to set the * location of the upper left corner of this * <code>Rectangle2D</code> * @param w the value to use to set the width of this * <code>Rectangle2D</code> * @param h the value to use to set the height of this * <code>Rectangle2D</code> */ public void setRect(float x, float y, float w, float h) { this.x = x; this.y = y; this.width = w; this.height = h; } /** * Sets the location and size of this <code>Rectangle2D</code> * to the specified double values. * @param x, y the coordinates to which to set the * location of the upper left corner of this * <code>Rectangle2D</code> * @param w the value to use to set the width of this * <code>Rectangle2D</code> * @param h the value to use to set the height of this * <code>Rectangle2D</code> */ public void setRect(double x, double y, double w, double h) { this.x = (float) x; this.y = (float) y; this.width = (float) w; this.height = (float) h; } /** * Sets this <code>Rectangle2D</code> to be the same as the * specified <code>Rectangle2D</code>. * @param r the specified <code>Rectangle2D</code> */ public void setRect(Rectangle2D r) { this.x = (float) r.getX(); this.y = (float) r.getY(); this.width = (float) r.getWidth(); this.height = (float) r.getHeight(); } /** * Determines where the specified float coordinates lie with respect * to this <code>Rectangle2D</code>. * This method computes a binary OR of the appropriate mask values * indicating, for each side of this <code>Rectangle2D</code>, * whether or not the specified coordinates are on the same side * of the edge as the rest of this <code>Rectangle2D</code>. * @param x, y the specified coordinates * @return the logical OR of all appropriate out codes. * @see Rectangle2D#OUT_LEFT * @see Rectangle2D#OUT_TOP * @see Rectangle2D#OUT_RIGHT * @see Rectangle2D#OUT_BOTTOM */ public int outcode(double x, double y) { /* * use double to avoid rounding errors... */ int out = 0; if (this.width <= 0) { out |= OUT_LEFT | OUT_RIGHT; } else if (x < this.x) { out |= OUT_LEFT; } else if (x > this.x + (double) this.width) { out |= OUT_RIGHT; } if (this.height <= 0) { out |= OUT_TOP | OUT_BOTTOM; } else if (y < this.y) { out |= OUT_TOP; } else if (y > this.y + (double) this.height) { out |= OUT_BOTTOM; } return out; } /** * Returns the high precision bounding box of this * <code>Rectangle2D</code>. * @return the bounding box of this <code>Rectangle2D</code>. */ public Rectangle2D getBounds2D() { return new Float(x, y, width, height); } /** * 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> that is * 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 Rectangle2D createIntersection(Rectangle2D r) { Rectangle2D dest; if (r instanceof Float) { dest = new Rectangle2D.Float(); } else { dest = new Rectangle2D.Double(); } Rectangle2D.intersect(this, r, dest); return dest; } /** * 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 Rectangle2D createUnion(Rectangle2D r) { Rectangle2D dest; if (r instanceof Float) { dest = new Rectangle2D.Float(); } else { dest = new Rectangle2D.Double(); } Rectangle2D.union(this, r, dest); return dest; } /** * Returns the <code>String</code> representation of this * <code>Rectangle2D</code>. * @return a <code>String</code> representing this * <code>Rectangle2D</code>. */ public String toString() { return getClass().getName() + "[x=" + x + ",y=" + y + ",w=" + width + ",h=" + height + "]"; } } /** * The <code>Double</code> class defines a rectangle specified in * double coordinates. */ public static class Double extends Rectangle2D { /** * The x coordinate of this <code>Rectangle2D</code>. */ public double x; /** * The y coordinate of this <code>Rectangle2D</code>. */ public double y; /** * The width of this <code>Rectangle2D</code>. */ public double width; /** * The height of this <code>Rectangle2D</code>. */ public double height; /** * Constructs a new <code>Rectangle2D</code>, initialized to * location (0, 0) and size (0, 0). */ public Double() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -