Java Reference
In-Depth Information
Compared to the previous two constructors the only change is the last bit where we use the radius
attribute value to define the Ellipse2D.Double object representing the circle.
Creating a Curve Element
Before you nod off, this one's a little more challenging as there may be an arbitrary number of child nodes:
// Curve has angle attribute. Content is <color>, <position>, <point>+
public Curve(org.w3c.dom.Element xmlElement) {
super(xmlElement);
curve = new GeneralPath();
curve.moveTo(origin.x, origin.y);
org.w3c.dom.NodeList nodes = xmlElement.getElementsByTagName("point");
for(int i = 0 ; i<nodes.getLength() ; i++)
curve.lineTo(
(float)(Double.parseDouble(
((org.w3c.dom.Element)nodes.item(i)).getAttribute("x")) - position.x),
(float)(Double.parseDouble(
((org.w3c.dom.Element)nodes.item(i)).getAttribute("y")) - position.y));
}
Having said that, the first part calls the base class constructor the same as ever. It's more interesting
when we get the list of Element nodes with the name "point" by calling
getElementsByTagName() for the xmlElement object. These are the nodes holding the
coordinates of the points that define the curve. It is important to us here that the method returns the
nodes in the NodeList object in the sequence in which they were originally added to the XML
document. If it didn't, we would have no way to reconstruct the curve. With the data encapsulated in
the nodes from the NodeList object that is returned, we can reconstruct the GeneralPath object that
describes the curve. The first point on the curve is always the origin, so the first definition in the path is
defined by calling its moveTo() method to move to the origin.
Each of the <point> nodes contains a point on the path in absolute coordinates. Since we want the
curve to be defined relative to the origin, we subtract the coordinates of the start point, position ,
from the corresponding coordinates stored in each node. We use the resulting coordinates to define the
end on each line segment by passing them to the lineTo() method for the path object.
Creating a Text Element
Recreating an Element.Text object from a <text> element is the messiest of all. It certainly involves
the most code. It's not difficult though. There are just a lot of bits and pieces to take care of.
// Text has angle attribute. Content is <color>, <position>, <font>, <string>
// <font> has attributes fontname, fontstyle, pointsize
// fontstyle is "plain", "bold", "italic", or "bold-italic"
// <string> content is text plus <bounds>
public Text(org.w3c.dom.Element xmlElement) {
super(xmlElement);
// Get the font details
org.w3c.dom.NodeList list = xmlElement.getElementsByTagName("font");
Search WWH ::




Custom Search