KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > Point


1 package jimm.datavision;
2 import jimm.util.XMLWriter;
3 import java.util.Observable JavaDoc;
4
5 /**
6  * A point with double coordinates.
7  *
8  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
9  */

10 public class Point extends Observable JavaDoc implements Writeable {
11
12 /**
13  * Warning: though public, treat as read-only. When writing, make sure to
14  * use setter method so observers are notified.
15  */

16 public double x;
17 /**
18  * Warning: though public, treat as read-only. When writing, make sure to
19  * use setter method so observers are notified.
20  */

21 public double y;
22
23 /** Constructor. */
24 public Point() { this(0, 0); }
25
26 /** Constructor. */
27 public Point(java.awt.Point JavaDoc p) { this((double)p.x, (double)p.y); }
28
29 /** Constructor. */
30 public Point(Point p) { this(p.x, p.y); }
31
32 /**
33  * Constructor.
34  *
35  * @param x a double
36  * @param y a double
37  */

38 public Point(double x, double y) {
39     this.x = x;
40     this.y = y;
41 }
42
43 /**
44  * Returns the x coordinate.
45  *
46  * @return the doubleing-point x coordinate
47  */

48 public double getX() { return x; }
49
50 /**
51  * Sets the x coordinate.
52  *
53  * @param newX the new x coordinate
54  */

55 public void setX(double newX) {
56     if (x != newX) {
57     x = newX;
58     setChanged();
59     notifyObservers();
60     }
61 }
62
63 /**
64  * Returns the y coordinate.
65  *
66  * @return the doubleing-point y coordinate
67  */

68 public double getY() { return y; }
69
70 /**
71  * Sets the y coordinate.
72  *
73  * @param newY the new y coordinate
74  */

75 public void setY(double newY) {
76     if (y != newY) {
77     y = newY;
78     setChanged();
79     notifyObservers();
80     }
81 }
82
83 /**
84  * Translates this point by the coordinates of another.
85  */

86 public void translate(java.awt.Point JavaDoc p) {
87     translate((double)p.x, (double)p.y);
88 }
89
90 /**
91  * Translates this point by the coordinates of another.
92  */

93 public void translate(Point p) {
94     translate(p.x, p.y);
95 }
96
97 /**
98  * Translates this point by the coordinates of another.
99  */

100 public void translate(double dx, double dy) {
101     if (dx != 0 && dy != 0) {
102     x += dx;
103     y += dy;
104     setChanged();
105     notifyObservers();
106     }
107 }
108
109 /**
110  * Returns the distance from this point to another.
111  *
112  * @param p the other point
113  * @return the distance between the two points
114  */

115 public double distanceTo(Point p) {
116     double dx = p.x - x;
117     double dy = p.y - y;
118     if (dx == 0) return Math.abs(dy);
119     if (dy == 0) return Math.abs(dx);
120     return Math.sqrt(dx * dx + dy * dy);
121 }
122
123 /**
124  * Returns a string representation of this point.
125  *
126  * @return a string representing this point
127  */

128 public String JavaDoc toString() {
129     return "(" + x + ", " + y + ")";
130 }
131
132 /**
133  * Writes this point as an XML tag.
134  *
135  * @param out a writer that knows how to write XML
136  */

137 public void writeXML(XMLWriter out) {
138     out.startElement("point");
139     out.attr("x", x);
140     out.attr("y", y);
141     out.endElement();
142 }
143
144 }
145
Popular Tags