Java Reference
In-Depth Information
// Create an XML element for a curve
public void addElementNode(Document doc) {
org.w3c.dom.Element curveElement = doc.createElement("curve");
// Create the angle attribute and attach it to the <curve> node
Attr attr = doc.createAttribute("angle");
attr.setValue(String.valueOf(angle));
curveElement.setAttributeNode(attr);
// Append the <color> and <position> nodes as children
curveElement.appendChild(createColorElement(doc));
curveElement.appendChild(createPositionElement(doc));
curveElement.appendChild(createBoundsElement(doc));
// Get the defining points via a path iterator
PathIterator iterator = curve.getPathIterator(new AffineTransform());
int maxCoordCount = 6;
// Maximum coordinates for a
segment
float[] temp = new float[maxCoordCount];
// Stores segment data
int result = iterator.currentSegment(temp);
// Get first segment
assert result == PathIterator.SEG_MOVETO;
// ... should be move to
iterator.next();
// Next segment
while(!iterator.isDone())
{
// While you have segments
result = iterator.currentSegment(temp);
// Get the segment data
assert result == PathIterator.SEG_LINETO;
// Should all be lines
// Create a <point> node and add it to the list of children
curveElement.appendChild(createPointTypeElement(doc, "point",
String.valueOf(temp[0]),
String.valueOf(temp[1])));
iterator.next();
// Go to next segment
}
doc.getDocumentElement().appendChild(curveElement);
}
Directory "Sketcher reading and writing XML"
The angle attribute and the position, color, and bounds elements are added in the same way as for other
elements. You use a PathIterator object to go through all the points in the path that defines the curve. You
add one <point> node as a child of the Element node for a curve for each defining point after the first. The
assertion in the loop verifies that each segment is a line segment.
Adding a Node for a Text Element
A node for an Element.Text object is a little different and also involves quite a lot of code. As well as the
usual <color> , <position> , and <bounds> child nodes, you also have to append a <font> node to define
Search WWH ::




Custom Search