| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | /** |
| 18 | * @author Denis M. Kishenko |
| 19 | * @version $Revision$ |
| 20 | */ |
| 21 | |
| 22 | package java.awt.geom; |
| 23 | |
| 24 | import java.awt.Rectangle; |
| 25 | import java.awt.Shape; |
| 26 | import java.awt.geom.PathIterator; |
| 27 | import java.awt.geom.Rectangle2D; |
| 28 | import java.util.NoSuchElementException; |
| 29 | |
| 30 | import org.apache.harmony.awt.internal.nls.Messages; |
| 31 | import org.apache.harmony.luni.util.NotImplementedException; |
| 32 | |
| 33 | /** |
| 34 | * The Class Area provides a minimal implementation for a generic shape. |
| 35 | * |
| 36 | * @since Android 1.0 |
| 37 | */ |
| 38 | public class Area implements Shape, Cloneable { |
| 39 | |
| 40 | /** |
| 41 | * The source Shape object. |
| 42 | */ |
| 43 | Shape s; |
| 44 | |
| 45 | /** |
| 46 | * The Class NullIterator. |
| 47 | */ |
| 48 | private static class NullIterator implements PathIterator { |
| 49 | |
| 50 | /** |
| 51 | * Instantiates a new null iterator. |
| 52 | */ |
| 53 | NullIterator() { |
| 54 | } |
| 55 | |
| 56 | public int getWindingRule() { |
| 57 | return WIND_NON_ZERO; |
| 58 | } |
| 59 | |
| 60 | public boolean isDone() { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | public void next() { |
| 65 | // nothing |
| 66 | } |
| 67 | |
| 68 | public int currentSegment(double[] coords) { |
| 69 | // awt.4B=Iterator out of bounds |
| 70 | throw new NoSuchElementException(Messages.getString("awt.4B")); //$NON-NLS-1$ |
| 71 | } |
| 72 | |
| 73 | public int currentSegment(float[] coords) { |
| 74 | // awt.4B=Iterator out of bounds |
| 75 | throw new NoSuchElementException(Messages.getString("awt.4B")); //$NON-NLS-1$ |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Instantiates a new area with no data. |
| 82 | */ |
| 83 | public Area() { |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Instantiates a new area with data given by the specified shape. |
| 88 | * |
| 89 | * @param s |
| 90 | * the shape that gives the data for this Area. |
| 91 | */ |
| 92 | public Area(Shape s) { |
| 93 | if (s == null) { |
| 94 | throw new NullPointerException(); |
| 95 | } |
| 96 | this.s = s; |
| 97 | } |
| 98 | |
| 99 | public boolean contains(double x, double y) { |
| 100 | return s == null ? false : s.contains(x, y); |
| 101 | } |
| 102 | |
| 103 | public boolean contains(double x, double y, double width, double height) { |
| 104 | return s == null ? false : s.contains(x, y, width, height); |
| 105 | } |
| 106 | |
| 107 | public boolean contains(Point2D p) { |
| 108 | if (p == null) { |
| 109 | throw new NullPointerException(); |
| 110 | } |
| 111 | return s == null ? false : s.contains(p); |
| 112 | } |
| 113 | |
| 114 | public boolean contains(Rectangle2D r) { |
| 115 | if (r == null) { |
| 116 | throw new NullPointerException(); |
| 117 | } |
| 118 | return s == null ? false : s.contains(r); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Tests whether the object is equal to this Area. |
| 123 | * |
| 124 | * @param obj |
| 125 | * the object to compare. |
| 126 | * @return true, if successful. |
| 127 | * @throws NotImplementedException |
| 128 | * if this method is not implemented. |
| 129 | */ |
| 130 | public boolean equals(Area obj) throws org.apache.harmony.luni.util.NotImplementedException { |
| 131 | throw new RuntimeException("Not implemented"); //$NON-NLS-1$ |
| 132 | } |
| 133 | |
| 134 | public boolean intersects(double x, double y, double width, double height) { |
| 135 | return s == null ? false : s.intersects(x, y, width, height); |
| 136 | } |
| 137 | |
| 138 | public boolean intersects(Rectangle2D r) { |
| 139 | if (r == null) { |
| 140 | throw new NullPointerException(); |
| 141 | } |
| 142 | return s == null ? false : s.intersects(r); |
| 143 | } |
| 144 | |
| 145 | public Rectangle getBounds() { |
| 146 | return s == null ? new Rectangle() : s.getBounds(); |
| 147 | } |
| 148 | |
| 149 | public Rectangle2D getBounds2D() { |
| 150 | return s == null ? new Rectangle2D.Double() : s.getBounds2D(); |
| 151 | } |
| 152 | |
| 153 | public PathIterator getPathIterator(AffineTransform t) { |
| 154 | return s == null ? new NullIterator() : s.getPathIterator(t); |
| 155 | } |
| 156 | |
| 157 | public PathIterator getPathIterator(AffineTransform t, double flatness) { |
| 158 | return s == null ? new NullIterator() : s.getPathIterator(t, flatness); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Adds the specified area to this area. |
| 163 | * |
| 164 | * @param area |
| 165 | * the area to add to this area. |
| 166 | * @throws NotImplementedException |
| 167 | * if this method is not implemented. |
| 168 | */ |
| 169 | public void add(Area area) throws org.apache.harmony.luni.util.NotImplementedException { |
| 170 | throw new RuntimeException("Not implemented"); //$NON-NLS-1$ |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Performs an exclusive or operation between this shape and the specified |
| 175 | * shape. |
| 176 | * |
| 177 | * @param area |
| 178 | * the area to XOR against this area. |
| 179 | * @throws NotImplementedException |
| 180 | * if this method is not implemented. |
| 181 | */ |
| 182 | public void exclusiveOr(Area area) throws org.apache.harmony.luni.util.NotImplementedException { |
| 183 | throw new RuntimeException("Not implemented"); //$NON-NLS-1$ |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Extracts a Rectangle2D from the source shape if the underlying shape data |
| 188 | * describes a rectangle. |
| 189 | * |
| 190 | * @return a Rectangle2D object if the source shape is rectangle, or null if |
| 191 | * shape is empty or not rectangle. |
| 192 | */ |
| 193 | Rectangle2D extractRectangle() { |
| 194 | if (s == null) { |
| 195 | return null; |
| 196 | } |
| 197 | float[] points = new float[12]; |
| 198 | int count = 0; |
| 199 | PathIterator p = s.getPathIterator(null); |
| 200 | float[] coords = new float[6]; |
| 201 | while (!p.isDone()) { |
| 202 | int type = p.currentSegment(coords); |
| 203 | if (count > 12 || type == PathIterator.SEG_QUADTO || type == PathIterator.SEG_CUBICTO) { |
| 204 | return null; |
| 205 | } |
| 206 | points[count++] = coords[0]; |
| 207 | points[count++] = coords[1]; |
| 208 | p.next(); |
| 209 | } |
| 210 | if (points[0] == points[6] && points[6] == points[8] && points[2] == points[4] |
| 211 | && points[1] == points[3] && points[3] == points[9] && points[5] == points[7]) { |
| 212 | return new Rectangle2D.Float(points[0], points[1], points[2] - points[0], points[7] |
| 213 | - points[1]); |
| 214 | } |
| 215 | return null; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Reduces the size of this Area by intersecting it with the specified Area |
| 220 | * if they are both rectangles. |
| 221 | * |
| 222 | * @see java.awt.geom.Rectangle2D#intersect(Rectangle2D, Rectangle2D, |
| 223 | * Rectangle2D) |
| 224 | * @param area |
| 225 | * the area. |
| 226 | */ |
| 227 | public void intersect(Area area) { |
| 228 | Rectangle2D src1 = extractRectangle(); |
| 229 | Rectangle2D src2 = area.extractRectangle(); |
| 230 | if (src1 != null && src2 != null) { |
| 231 | Rectangle2D.intersect(src1, src2, (Rectangle2D)s); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Subtract the specified area from this area. |
| 237 | * |
| 238 | * @param area |
| 239 | * the area to subtract. |
| 240 | * @throws NotImplementedException |
| 241 | * if this method is not implemented. |
| 242 | */ |
| 243 | public void subtract(Area area) throws org.apache.harmony.luni.util.NotImplementedException { |
| 244 | throw new RuntimeException("Not implemented"); //$NON-NLS-1$ |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Checks if this Area is empty. |
| 249 | * |
| 250 | * @return true, if this Area is empty. |
| 251 | * @throws NotImplementedException |
| 252 | * if this method is not implemented. |
| 253 | */ |
| 254 | public boolean isEmpty() throws org.apache.harmony.luni.util.NotImplementedException { |
| 255 | throw new RuntimeException("Not implemented"); //$NON-NLS-1$ |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Checks if this Area is polygonal. |
| 260 | * |
| 261 | * @return true, if this Area is polygonal. |
| 262 | * @throws NotImplementedException |
| 263 | * if this method is not implemented. |
| 264 | */ |
| 265 | public boolean isPolygonal() throws org.apache.harmony.luni.util.NotImplementedException { |
| 266 | throw new RuntimeException("Not implemented"); //$NON-NLS-1$ |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Checks if this Area is rectangular. |
| 271 | * |
| 272 | * @return true, if this Area is rectangular. |
| 273 | * @throws NotImplementedException |
| 274 | * if this method is not implemented. |
| 275 | */ |
| 276 | public boolean isRectangular() throws org.apache.harmony.luni.util.NotImplementedException { |
| 277 | throw new RuntimeException("Not implemented"); //$NON-NLS-1$ |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Checks if this Area is singular. |
| 282 | * |
| 283 | * @return true, if this Area is singular. |
| 284 | * @throws NotImplementedException |
| 285 | * if this method is not implemented. |
| 286 | */ |
| 287 | public boolean isSingular() throws org.apache.harmony.luni.util.NotImplementedException { |
| 288 | throw new RuntimeException("Not implemented"); //$NON-NLS-1$ |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Resets the data of this Area. |
| 293 | * |
| 294 | * @throws NotImplementedException |
| 295 | * if this method is not implemented. |
| 296 | */ |
| 297 | public void reset() throws org.apache.harmony.luni.util.NotImplementedException { |
| 298 | throw new RuntimeException("Not implemented"); //$NON-NLS-1$ |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Transforms the data of this Area according to the specified |
| 303 | * AffineTransform. |
| 304 | * |
| 305 | * @param t |
| 306 | * the transform to use to transform the data. |
| 307 | */ |
| 308 | public void transform(AffineTransform t) { |
| 309 | s = t.createTransformedShape(s); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Creates a new Area that is the result of transforming the data of this |
| 314 | * Area according to the specified AffineTransform. |
| 315 | * |
| 316 | * @param t |
| 317 | * the transform to use to transform the data. |
| 318 | * @return the new Area that is the result of transforming the data of this |
| 319 | * Area according to the specified AffineTransform. |
| 320 | */ |
| 321 | public Area createTransformedArea(AffineTransform t) { |
| 322 | return s == null ? new Area() : new Area(t.createTransformedShape(s)); |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public Object clone() { |
| 327 | return new Area(this); |
| 328 | } |
| 329 | |
| 330 | } |