001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Denis M. Kishenko
019: * @version $Revision$
020: */package java.awt.geom;
021:
022: import java.awt.Rectangle;
023: import java.awt.Shape;
024:
025: public abstract class RectangularShape implements Shape, Cloneable {
026:
027: protected RectangularShape() {
028: }
029:
030: public abstract double getX();
031:
032: public abstract double getY();
033:
034: public abstract double getWidth();
035:
036: public abstract double getHeight();
037:
038: public abstract boolean isEmpty();
039:
040: public abstract void setFrame(double x, double y, double w, double h);
041:
042: public double getMinX() {
043: return getX();
044: }
045:
046: public double getMinY() {
047: return getY();
048: }
049:
050: public double getMaxX() {
051: return getX() + getWidth();
052: }
053:
054: public double getMaxY() {
055: return getY() + getHeight();
056: }
057:
058: public double getCenterX() {
059: return getX() + getWidth() / 2.0;
060: }
061:
062: public double getCenterY() {
063: return getY() + getHeight() / 2.0;
064: }
065:
066: public Rectangle2D getFrame() {
067: return new Rectangle2D.Double(getX(), getY(), getWidth(),
068: getHeight());
069: }
070:
071: public void setFrame(Point2D loc, Dimension2D size) {
072: setFrame(loc.getX(), loc.getY(), size.getWidth(), size
073: .getHeight());
074: }
075:
076: public void setFrame(Rectangle2D r) {
077: setFrame(r.getX(), r.getY(), r.getWidth(), r.getHeight());
078: }
079:
080: public void setFrameFromDiagonal(double x1, double y1, double x2,
081: double y2) {
082: double rx, ry, rw, rh;
083: if (x1 < x2) {
084: rx = x1;
085: rw = x2 - x1;
086: } else {
087: rx = x2;
088: rw = x1 - x2;
089: }
090: if (y1 < y2) {
091: ry = y1;
092: rh = y2 - y1;
093: } else {
094: ry = y2;
095: rh = y1 - y2;
096: }
097: setFrame(rx, ry, rw, rh);
098: }
099:
100: public void setFrameFromDiagonal(Point2D p1, Point2D p2) {
101: setFrameFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY());
102: }
103:
104: public void setFrameFromCenter(double centerX, double centerY,
105: double cornerX, double cornerY) {
106: double width = Math.abs(cornerX - centerX);
107: double height = Math.abs(cornerY - centerY);
108: setFrame(centerX - width, centerY - height, width * 2.0,
109: height * 2.0);
110: }
111:
112: public void setFrameFromCenter(Point2D center, Point2D corner) {
113: setFrameFromCenter(center.getX(), center.getY(), corner.getX(),
114: corner.getY());
115: }
116:
117: public boolean contains(Point2D point) {
118: return contains(point.getX(), point.getY());
119: }
120:
121: public boolean intersects(Rectangle2D rect) {
122: return intersects(rect.getX(), rect.getY(), rect.getWidth(),
123: rect.getHeight());
124: }
125:
126: public boolean contains(Rectangle2D rect) {
127: return contains(rect.getX(), rect.getY(), rect.getWidth(), rect
128: .getHeight());
129: }
130:
131: public Rectangle getBounds() {
132: int x1 = (int) Math.floor(getMinX());
133: int y1 = (int) Math.floor(getMinY());
134: int x2 = (int) Math.ceil(getMaxX());
135: int y2 = (int) Math.ceil(getMaxY());
136: return new Rectangle(x1, y1, x2 - x1, y2 - y1);
137: }
138:
139: public PathIterator getPathIterator(AffineTransform t,
140: double flatness) {
141: return new FlatteningPathIterator(getPathIterator(t), flatness);
142: }
143:
144: @Override
145: public Object clone() {
146: try {
147: return super .clone();
148: } catch (CloneNotSupportedException e) {
149: throw new InternalError();
150: }
151: }
152:
153: }
|