001: package jimm.datavision;
002:
003: import jimm.util.XMLWriter;
004: import java.util.Observable;
005:
006: /**
007: * A point with double coordinates.
008: *
009: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
010: */
011: public class Point extends Observable implements Writeable {
012:
013: /**
014: * Warning: though public, treat as read-only. When writing, make sure to
015: * use setter method so observers are notified.
016: */
017: public double x;
018: /**
019: * Warning: though public, treat as read-only. When writing, make sure to
020: * use setter method so observers are notified.
021: */
022: public double y;
023:
024: /** Constructor. */
025: public Point() {
026: this (0, 0);
027: }
028:
029: /** Constructor. */
030: public Point(java.awt.Point p) {
031: this ((double) p.x, (double) p.y);
032: }
033:
034: /** Constructor. */
035: public Point(Point p) {
036: this (p.x, p.y);
037: }
038:
039: /**
040: * Constructor.
041: *
042: * @param x a double
043: * @param y a double
044: */
045: public Point(double x, double y) {
046: this .x = x;
047: this .y = y;
048: }
049:
050: /**
051: * Returns the x coordinate.
052: *
053: * @return the doubleing-point x coordinate
054: */
055: public double getX() {
056: return x;
057: }
058:
059: /**
060: * Sets the x coordinate.
061: *
062: * @param newX the new x coordinate
063: */
064: public void setX(double newX) {
065: if (x != newX) {
066: x = newX;
067: setChanged();
068: notifyObservers();
069: }
070: }
071:
072: /**
073: * Returns the y coordinate.
074: *
075: * @return the doubleing-point y coordinate
076: */
077: public double getY() {
078: return y;
079: }
080:
081: /**
082: * Sets the y coordinate.
083: *
084: * @param newY the new y coordinate
085: */
086: public void setY(double newY) {
087: if (y != newY) {
088: y = newY;
089: setChanged();
090: notifyObservers();
091: }
092: }
093:
094: /**
095: * Translates this point by the coordinates of another.
096: */
097: public void translate(java.awt.Point p) {
098: translate((double) p.x, (double) p.y);
099: }
100:
101: /**
102: * Translates this point by the coordinates of another.
103: */
104: public void translate(Point p) {
105: translate(p.x, p.y);
106: }
107:
108: /**
109: * Translates this point by the coordinates of another.
110: */
111: public void translate(double dx, double dy) {
112: if (dx != 0 && dy != 0) {
113: x += dx;
114: y += dy;
115: setChanged();
116: notifyObservers();
117: }
118: }
119:
120: /**
121: * Returns the distance from this point to another.
122: *
123: * @param p the other point
124: * @return the distance between the two points
125: */
126: public double distanceTo(Point p) {
127: double dx = p.x - x;
128: double dy = p.y - y;
129: if (dx == 0)
130: return Math.abs(dy);
131: if (dy == 0)
132: return Math.abs(dx);
133: return Math.sqrt(dx * dx + dy * dy);
134: }
135:
136: /**
137: * Returns a string representation of this point.
138: *
139: * @return a string representing this point
140: */
141: public String toString() {
142: return "(" + x + ", " + y + ")";
143: }
144:
145: /**
146: * Writes this point as an XML tag.
147: *
148: * @param out a writer that knows how to write XML
149: */
150: public void writeXML(XMLWriter out) {
151: out.startElement("point");
152: out.attr("x", x);
153: out.attr("y", y);
154: out.endElement();
155: }
156:
157: }
|