1 package jimm.datavision; 2 import jimm.util.XMLWriter; 3 import java.util.Observable ; 4 5 10 public class Point extends Observable implements Writeable { 11 12 16 public double x; 17 21 public double y; 22 23 24 public Point() { this(0, 0); } 25 26 27 public Point(java.awt.Point p) { this((double)p.x, (double)p.y); } 28 29 30 public Point(Point p) { this(p.x, p.y); } 31 32 38 public Point(double x, double y) { 39 this.x = x; 40 this.y = y; 41 } 42 43 48 public double getX() { return x; } 49 50 55 public void setX(double newX) { 56 if (x != newX) { 57 x = newX; 58 setChanged(); 59 notifyObservers(); 60 } 61 } 62 63 68 public double getY() { return y; } 69 70 75 public void setY(double newY) { 76 if (y != newY) { 77 y = newY; 78 setChanged(); 79 notifyObservers(); 80 } 81 } 82 83 86 public void translate(java.awt.Point p) { 87 translate((double)p.x, (double)p.y); 88 } 89 90 93 public void translate(Point p) { 94 translate(p.x, p.y); 95 } 96 97 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 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 128 public String toString() { 129 return "(" + x + ", " + y + ")"; 130 } 131 132 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 |