KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > Line


1 package jimm.datavision;
2 import jimm.util.XMLWriter;
3 import java.awt.Color JavaDoc;
4
5 /**
6  * A line is a visual report element. Lines are used in field {@link
7  * jimm.datavision.field.Border}s and independently.
8  * <p>
9  * Note that currently, line thickness is ignored in the Java GUI (but not
10  * in layout engines such as the LaTeXLE).
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  */

14 public class Line extends Element {
15
16 protected static final Color JavaDoc DEFAULT_COLOR = Color.black;
17
18 protected double thickness;
19 protected Point[] points;
20 protected Color JavaDoc color;
21
22 /**
23  * Constructor.
24  *
25  * @param report the report containing this line
26  * @param section the section containing this line
27  * @param thickness the line thickness
28  * @param color may be <code>null</code>
29  * @param visible show/hide flag
30  */

31 public Line(Report report, Section section, double thickness, Color JavaDoc color,
32         boolean visible)
33 {
34     this(report, section, thickness, color, visible, null, null);
35 }
36
37 /**
38  * Constructor.
39  *
40  * @param report the report containing this line
41  * @param section the section containing this line
42  * @param thickness the line thickness
43  * @param color may be <code>null</code>
44  * @param visible show/hide flag
45  * @param p0 one end point of the line
46  * @param p1 the other end point of the line
47  */

48 public Line(Report report, Section section, double thickness, Color JavaDoc 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 /**
60  * Adds an end point to the line. Used when constructing a line from XML,
61  * where we don't see the point until after creating this line.
62  *
63  * @param x the x coordinate
64  * @param y the y coordinate
65  */

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 /**
76  * Returns the line thickness.
77  *
78  * @return the line thickness
79  */

80 public double getThickness() { return thickness; }
81
82 /**
83  * Sets the line thickness.
84  *
85  * @param newThickness the new line thickness
86  */

87 public void setThickness(double newThickness) {
88     if (thickness != newThickness) {
89     thickness = newThickness;
90     setChanged();
91     notifyObservers();
92     }
93 }
94
95 /**
96  * Returns one of the two end points of the line.
97  *
98  * @param index either 0 or 1
99  * @return a point
100  */

101 public Point getPoint(int index) { return points[index]; }
102
103 /**
104  * Sets one of the two end points.
105  *
106  * @param newPoint a point
107  * @param index either 0 or 1
108  */

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 /**
120  * Returns the length of the line.
121  *
122  * @return the distance between the two end points
123  */

124 public double length() { return points[0].distanceTo(points[1]); }
125
126 /**
127  * Returns a string representation of this line.
128  *
129  * @return a string representing this line
130  */

131 public String JavaDoc toString() { return "(" + points[0] + ", " + points[1] + ")"; }
132
133 /**
134  * Returns the line's color. The return value will never be
135  * <code>null</code>.
136  *
137  * @return the line's color
138  */

139 public Color JavaDoc getColor() { return color == null ? DEFAULT_COLOR : color; }
140
141 /**
142  * Sets the line's color. If <var>c</var> is <code>null</code>, then the
143  * color is set to <code>DEFAULT_COLOR</code>.
144  *
145  * @param c new line color; if <code>null</code>, color is set to
146  * <code>DEFAULT_COLOR</code>
147  */

148 public void setColor(Color JavaDoc c) { color = c == null ? DEFAULT_COLOR : c; }
149
150 /**
151  * Writes this line as an XML tag.
152  *
153  * @param out a writer that knows how to write XML
154  */

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