Java Reference
In-Depth Information
break;
}
}
NamedNodeMap attrs = node.getAttributes();
circle = new Ellipse2D.Double();
circle.width = circle.height =
Double.valueOf(((Attr)(attrs.getNamedItem("diameter"))).getValue());
}
Directory "Sketcher reading and writing XML"
There's not a lot to say about this that is new. You reconstruct the Ellipse2D.Double object using the
no-arg constructor and set its width and height to be the value you extract for the "diameter" attribute from
the <circle> element.
Creating a Curve Object from an XML Node
Creating an Element.Curve object is inevitably different from previous object types, but not substantially
different. The initial MOVE_TO segment for the general path is to the origin, so that requires no information
from the Node object. The remaining LINE_TO segments are specified by <point> child nodes, so you just
need to add a segment to the GeneralPath object corresponding to each <point> child node that is present.
Here's the code:
// Create Curve object from XML node
public Curve(Node node) {
curve = new GeneralPath();
curve.moveTo(origin.x, origin.y);
// Set current position as origin
setAngleFromXML(node);
NodeList childNodes = node.getChildNodes();
Node aNode = null;
for(int i = 0 ; i < childNodes.getLength() ; ++i) {
aNode = childNodes.item(i);
switch(aNode.getNodeName()) {
case "position":
setPositionFromXML(aNode);
break;
case "color":
setColorFromXML(aNode);
break;
case "bounds":
setBoundsFromXML(aNode);
break;
case "point":
NamedNodeMap attrs = aNode.getAttributes();
curve.lineTo(
Double.valueOf(((Attr)(attrs.getNamedItem("x"))).getValue()),
Double.valueOf(((Attr)(attrs.getNamedItem("y"))).getValue()));
break;
default:
Search WWH ::




Custom Search