1 package jimm.datavision; 2 import jimm.util.XMLWriter; 3 import java.awt.Color ; 4 5 14 public class Line extends Element { 15 16 protected static final Color DEFAULT_COLOR = Color.black; 17 18 protected double thickness; 19 protected Point[] points; 20 protected Color color; 21 22 31 public Line(Report report, Section section, double thickness, Color color, 32 boolean visible) 33 { 34 this(report, section, thickness, color, visible, null, null); 35 } 36 37 48 public Line(Report report, Section section, double thickness, Color color, 49 boolean visible, Point p0, Point p1) 50 { 51 super(report, section, visible); 52 this.thickness = thickness; 53 points = new Point[2]; 54 points[0] = p0; 55 points[1] = p1; 56 this.color = color == null ? DEFAULT_COLOR : color; 57 } 58 59 66 public void addEndPoint(double x, double y) { 67 Point p = new Point(x, y); 68 if (points[0] == null) 69 points[0] = p; 70 else 71 points[1] = p; 72 p.addObserver(this); 73 } 74 75 80 public double getThickness() { return thickness; } 81 82 87 public void setThickness(double newThickness) { 88 if (thickness != newThickness) { 89 thickness = newThickness; 90 setChanged(); 91 notifyObservers(); 92 } 93 } 94 95 101 public Point getPoint(int index) { return points[index]; } 102 103 109 public void setPoint(Point newPoint, int index) { 110 if (points[index] != newPoint) { 111 if (points[index] != null) points[index].deleteObserver(this); 112 points[index] = newPoint; 113 if (points[index] != null) points[index].addObserver(this); 114 setChanged(); 115 notifyObservers(); 116 } 117 } 118 119 124 public double length() { return points[0].distanceTo(points[1]); } 125 126 131 public String toString() { return "(" + points[0] + ", " + points[1] + ")"; } 132 133 139 public Color getColor() { return color == null ? DEFAULT_COLOR : color; } 140 141 148 public void setColor(Color c) { color = c == null ? DEFAULT_COLOR : c; } 149 150 155 public void writeXML(XMLWriter out) { 156 out.startElement("line"); 157 out.attr("thickness", thickness); 158 if (color != null && !color.equals(DEFAULT_COLOR)) 159 out.attr("color", color); 160 if (!visible) 161 out.attr("visible", visible); 162 points[0].writeXML(out); 163 points[1].writeXML(out); 164 out.endElement(); 165 } 166 167 } 168 | Popular Tags |