Java Reference
In-Depth Information
There's nothing new here. We can use either the width or the height member of the
Ellipse2D.Double class object to get the diameter of the circle. We divide the width field for the
circle object by 2.0 to get the radius.
Adding a Curve Node
Creating a <curve> node is a bit more long-winded as a GeneralPath object represents a curve, and
we have to extract the arbitrary number of defining points from it. The code that does this is more or
less what we used in the writeObject() method for a curve so it is nothing new:
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));
// 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 == iterator.SEG _ MOVETO; // ... should be move to
iterator.next(); // Next segment
while(!iterator.isDone()) { // While we have segments
result = iterator.currentSegment(temp); // Get the segment data
assert result == iterator.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]+position.x),
String.valueOf(temp[1])+position.y));
iterator.next(); // Go to next segment
}
doc.getDocumentElement().appendChild(curveElement);
}
We add one <point> node as a child of the Element node for a curve for each defining point after the
first. Since the defining points for the GeneralPath object were created relative to the origin, we have
to add the corresponding coordinates of position to the coordinates of each defining point.
Search WWH ::




Custom Search