01: /*
02: * MapArea.java
03: *
04: * Created on 25 January 2007, 09:32
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: package com.xoetrope.svg;
11:
12: import java.awt.Rectangle;
13: import java.awt.geom.GeneralPath;
14: import java.awt.geom.Line2D;
15: import java.awt.geom.Point2D;
16: import java.util.ArrayList;
17:
18: /**
19: *
20: *
21: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
22: * the GNU Public License (GPL), please see license.txt for more details. If
23: * you make commercial use of this software you must purchase a commercial
24: * license from Xoetrope.</p>
25: * <p> $Revision: 1.2 $</p>
26: */
27: public class XMapArea {
28: protected ArrayList points;
29:
30: /**
31: * Creates a new instance of MapArea.
32: * The areas bounds are defined by a set of points.
33: */
34: public XMapArea() {
35: points = new ArrayList();
36: }
37:
38: /**
39: * Add a point to the map area.
40: * @param x a <CODE>double</CODE> specifying the x-coordinate of the point.
41: * @param y a <CODE>double</CODE> specifying the y-coordinate of the point.
42: */
43: public void addPoint(double x, double y) {
44: points.add(new Point2D.Double(x, y));
45: }
46:
47: /**
48: * Add a point to the map area.
49: * @param point a <CODE>Point2D</CODE> object specifying the coordinates of the point.
50: */
51: public void addPoint(Point2D point) {
52: points.add(point);
53: }
54:
55: /**
56: * Return all the points associated with the area.
57: * @return the <CODE>ArrayList</CODE> containing the points.
58: */
59: public ArrayList getPoints() {
60: return points;
61: }
62:
63: /**
64: * Checks whether a specified point is contained within the area.
65: * @param x a <CODE>double</CODE> specifying the x-coordinate of the point.
66: * @param y a <CODE>double</CODE> specifying the y-coordinate of the point.
67: * @return <CODE>boolean</CODE> specifying whether the point is contained within the area or not.
68: */
69: public boolean contains(double x, double y) {
70: GeneralPath path = new GeneralPath();
71:
72: for (int i = 1; i < points.size(); i++) {
73: Point2D point1 = (Point2D) points.get(i - 1);
74: Point2D point2 = (Point2D) points.get(i);
75: Line2D.Double line = new Line2D.Double(point1, point2);
76:
77: path.append(line, true);
78: }
79:
80: path.closePath();
81:
82: return path.contains(x, y);
83: }
84:
85: /**
86: * Checks whether a specified point is contained within the area.
87: * @param point the <CODE>Point2D</CODE> being checked.
88: * @return <CODE>boolean</CODE> specifying whether the point is contained within the area or not.
89: */
90: public boolean contains(Point2D point) {
91: return contains(point.getX(), point.getY());
92: }
93: }
|