Java Reference
In-Depth Information
protected org.w3c.dom.Element createPositionElement(Document doc) {
return createPointTypeElement(doc, "position",
String.valueOf(position.getX()),
String.valueOf(position.getY()));
}
We will be able to create <endpoint> , <bottomright> , or <point> nodes in the same way in
methods in the subclasses of Element .
Adding a Line Node
The method to add a <line> node to the Document object will create a <line> element with an angle
attribute, and then add three child elements: <color> , <position> , and <endpoint> . You can add the
following implementation of the addElementNode() method to the Element.Line class:
public void addElementNode(Document doc) {
org.w3c.dom.Element lineElement = doc.createElement("line");
// Create the angle attribute and attach it to the <line> node
Attr attr = doc.createAttribute("angle");
attr.setValue(String.valueOf(angle));
lineElement.setAttributeNode(attr);
// Append the <color>, <position>, and <endpoint> nodes as children
lineElement.appendChild(createColorElement(doc));
lineElement.appendChild(createPositionElement(doc));
lineElement.appendChild(createEndpointElement(doc));
// Append the <line> node to the document root node
doc.getDocumentElement().appendChild(lineElement);
}
When we have a <Line> element in a sketch, calling this method with a reference to a Document
object as an argument will add a child node corresponding to the <line> element. To complete this we
must add the createEndpointElement() to the Element.Line class:
private org.w3c.dom.Element createEndpointElement(Document doc) {
return createPointTypeElement(doc, "endpoint",
String.valueOf(line.x2+position.x),
String.valueOf(line.y2+position.y));
}
This calls the createPointTypeElement() method that is inherited from the base class. Since the
position of a line is recorded in the base class and the end point of the line is relative to that point, we
must add the coordinates of position in the base class to the coordinates of the end point of the line
to get the original end point coordinates back.
Adding a Rectangle Node
The code to add a <rectangle> node to the Document object will be almost the same as adding a
<line> node:
Search WWH ::




Custom Search