summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/geom/Area.java
blob: e6619e349c7034e9e4e3017e0ba5d09437a7c941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Denis M. Kishenko
 * @version $Revision$
 */

package java.awt.geom;

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
import java.util.NoSuchElementException;

import org.apache.harmony.awt.internal.nls.Messages;
import org.apache.harmony.luni.util.NotImplementedException;

/**
 * The Class Area provides a minimal implementation for a generic shape.
 * 
 * @since Android 1.0
 */
public class Area implements Shape, Cloneable {

    /**
     * The source Shape object.
     */
    Shape s;

    /**
     * The Class NullIterator.
     */
    private static class NullIterator implements PathIterator {

        /**
         * Instantiates a new null iterator.
         */
        NullIterator() {
        }

        public int getWindingRule() {
            return WIND_NON_ZERO;
        }

        public boolean isDone() {
            return true;
        }

        public void next() {
            // nothing
        }

        public int currentSegment(double[] coords) {
            // awt.4B=Iterator out of bounds
            throw new NoSuchElementException(Messages.getString("awt.4B")); //$NON-NLS-1$
        }

        public int currentSegment(float[] coords) {
            // awt.4B=Iterator out of bounds
            throw new NoSuchElementException(Messages.getString("awt.4B")); //$NON-NLS-1$
        }

    }

    /**
     * Instantiates a new area with no data.
     */
    public Area() {
    }

    /**
     * Instantiates a new area with data given by the specified shape.
     * 
     * @param s
     *            the shape that gives the data for this Area.
     */
    public Area(Shape s) {
        if (s == null) {
            throw new NullPointerException();
        }
        this.s = s;
    }

    public boolean contains(double x, double y) {
        return s == null ? false : s.contains(x, y);
    }

    public boolean contains(double x, double y, double width, double height) {
        return s == null ? false : s.contains(x, y, width, height);
    }

    public boolean contains(Point2D p) {
        if (p == null) {
            throw new NullPointerException();
        }
        return s == null ? false : s.contains(p);
    }

    public boolean contains(Rectangle2D r) {
        if (r == null) {
            throw new NullPointerException();
        }
        return s == null ? false : s.contains(r);
    }

    /**
     * Tests whether the object is equal to this Area.
     * 
     * @param obj
     *            the object to compare.
     * @return true, if successful.
     * @throws NotImplementedException
     *             if this method is not implemented.
     */
    public boolean equals(Area obj) throws org.apache.harmony.luni.util.NotImplementedException {
        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    }

    public boolean intersects(double x, double y, double width, double height) {
        return s == null ? false : s.intersects(x, y, width, height);
    }

    public boolean intersects(Rectangle2D r) {
        if (r == null) {
            throw new NullPointerException();
        }
        return s == null ? false : s.intersects(r);
    }

    public Rectangle getBounds() {
        return s == null ? new Rectangle() : s.getBounds();
    }

    public Rectangle2D getBounds2D() {
        return s == null ? new Rectangle2D.Double() : s.getBounds2D();
    }

    public PathIterator getPathIterator(AffineTransform t) {
        return s == null ? new NullIterator() : s.getPathIterator(t);
    }

    public PathIterator getPathIterator(AffineTransform t, double flatness) {
        return s == null ? new NullIterator() : s.getPathIterator(t, flatness);
    }

    /**
     * Adds the specified area to this area.
     * 
     * @param area
     *            the area to add to this area.
     * @throws NotImplementedException
     *             if this method is not implemented.
     */
    public void add(Area area) throws org.apache.harmony.luni.util.NotImplementedException {
        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    }

    /**
     * Performs an exclusive or operation between this shape and the specified
     * shape.
     * 
     * @param area
     *            the area to XOR against this area.
     * @throws NotImplementedException
     *             if this method is not implemented.
     */
    public void exclusiveOr(Area area) throws org.apache.harmony.luni.util.NotImplementedException {
        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    }

    /**
     * Extracts a Rectangle2D from the source shape if the underlying shape data
     * describes a rectangle.
     * 
     * @return a Rectangle2D object if the source shape is rectangle, or null if
     *         shape is empty or not rectangle.
     */
    Rectangle2D extractRectangle() {
        if (s == null) {
            return null;
        }
        float[] points = new float[12];
        int count = 0;
        PathIterator p = s.getPathIterator(null);
        float[] coords = new float[6];
        while (!p.isDone()) {
            int type = p.currentSegment(coords);
            if (count > 12 || type == PathIterator.SEG_QUADTO || type == PathIterator.SEG_CUBICTO) {
                return null;
            }
            points[count++] = coords[0];
            points[count++] = coords[1];
            p.next();
        }
        if (points[0] == points[6] && points[6] == points[8] && points[2] == points[4]
                && points[1] == points[3] && points[3] == points[9] && points[5] == points[7]) {
            return new Rectangle2D.Float(points[0], points[1], points[2] - points[0], points[7]
                    - points[1]);
        }
        return null;
    }

    /**
     * Reduces the size of this Area by intersecting it with the specified Area
     * if they are both rectangles.
     * 
     * @see java.awt.geom.Rectangle2D#intersect(Rectangle2D, Rectangle2D,
     *      Rectangle2D)
     * @param area
     *            the area.
     */
    public void intersect(Area area) {
        Rectangle2D src1 = extractRectangle();
        Rectangle2D src2 = area.extractRectangle();
        if (src1 != null && src2 != null) {
            Rectangle2D.intersect(src1, src2, (Rectangle2D)s);
        }
    }

    /**
     * Subtract the specified area from this area.
     * 
     * @param area
     *            the area to subtract.
     * @throws NotImplementedException
     *             if this method is not implemented.
     */
    public void subtract(Area area) throws org.apache.harmony.luni.util.NotImplementedException {
        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    }

    /**
     * Checks if this Area is empty.
     * 
     * @return true, if this Area is empty.
     * @throws NotImplementedException
     *             if this method is not implemented.
     */
    public boolean isEmpty() throws org.apache.harmony.luni.util.NotImplementedException {
        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    }

    /**
     * Checks if this Area is polygonal.
     * 
     * @return true, if this Area is polygonal.
     * @throws NotImplementedException
     *             if this method is not implemented.
     */
    public boolean isPolygonal() throws org.apache.harmony.luni.util.NotImplementedException {
        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    }

    /**
     * Checks if this Area is rectangular.
     * 
     * @return true, if this Area is rectangular.
     * @throws NotImplementedException
     *             if this method is not implemented.
     */
    public boolean isRectangular() throws org.apache.harmony.luni.util.NotImplementedException {
        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    }

    /**
     * Checks if this Area is singular.
     * 
     * @return true, if this Area is singular.
     * @throws NotImplementedException
     *             if this method is not implemented.
     */
    public boolean isSingular() throws org.apache.harmony.luni.util.NotImplementedException {
        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    }

    /**
     * Resets the data of this Area.
     * 
     * @throws NotImplementedException
     *             if this method is not implemented.
     */
    public void reset() throws org.apache.harmony.luni.util.NotImplementedException {
        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    }

    /**
     * Transforms the data of this Area according to the specified
     * AffineTransform.
     * 
     * @param t
     *            the transform to use to transform the data.
     */
    public void transform(AffineTransform t) {
        s = t.createTransformedShape(s);
    }

    /**
     * Creates a new Area that is the result of transforming the data of this
     * Area according to the specified AffineTransform.
     * 
     * @param t
     *            the transform to use to transform the data.
     * @return the new Area that is the result of transforming the data of this
     *         Area according to the specified AffineTransform.
     */
    public Area createTransformedArea(AffineTransform t) {
        return s == null ? new Area() : new Area(t.createTransformedShape(s));
    }

    @Override
    public Object clone() {
        return new Area(this);
    }

}