Java Reference
In-Depth Information
doc.getDocumentElement().appendChild(rectElement);
}
Directory "Sketcher reading and writing XML"
After creating the node for the Rectangle object, you set the width, height, and angle as attributes. You
then append the child elements for the element color, position, and bounding rectangle. Finally you append
the <rectangle> element as a child for the document root node.
Adding a Circle Node
Creating the node for a <circle> element in the Element.Circle class is not very different:
// Create an XML element for a circle
public void addElementNode(Document doc) {
org.w3c.dom.Element circleElement = doc.createElement("circle");
// Create the diameter attribute and attach it to the <circle> node
Attr attr = doc.createAttribute("diameter");
attr.setValue(String.valueOf(circle.width));
circleElement.setAttributeNode(attr);
// Create the angle attribute and attach it to the <circle> node
attr = doc.createAttribute("angle");
attr.setValue(String.valueOf(angle));
circleElement.setAttributeNode(attr);
// Append the <color> and <position> nodes as children
circleElement.appendChild(createColorElement(doc));
circleElement.appendChild(createPositionElement(doc));
circleElement.appendChild(createBoundsElement(doc));
doc.getDocumentElement().appendChild(circleElement);
}
Directory "Sketcher reading and writing XML"
There's nothing new here. You can use either the width or the height member of the Ellipse2D.Double
class object as the diameter of the circle because they have the same value.
Adding a Curve Node
Creating an XML <curve> node for an Element.Curve object is a bit more long-winded. A curve is repres-
ented by a GeneralPath object, and you have to add all the defining points after the first as child elements to
the <curve> element. You can obtain a special iterator object of type java.awt.geom.PathIterator for a
GeneralPath object by calling its getPathIterator() method. The PathIterator object provides access
to all the information you need to re-create the GeneralPath object.
Search WWH ::




Custom Search