Java Reference
In-Depth Information
break;
case "color":
setColorFromXML(aNode);
break;
case "bounds":
setBoundsFromXML(aNode);
break;
default:
System.err.println("Invalid node in <rectangle>: " + aNode);
break;
}
}
NamedNodeMap attrs = node.getAttributes();
rectangle = new Rectangle2D.Double();
rectangle.width =
Double.valueOf(((Attr)(attrs.getNamedItem("width"))).getValue());
rectangle.height =
Double.valueOf(((Attr)(attrs.getNamedItem("height"))).getValue());
}
Directory "Sketcher reading and writing XML"
The no-arg constructor for a Rectangle2D.Double object creates an object at (0,0) with a width and
height of zero. The width and height of the rectangle are recorded as XML attributes, so you retrieve these
and set the width and height of the object that you have created.
Creating a Circle Object from an XML Node
This constructor is almost identical to the previous constructor:
// Create Circle object from XML node
public Circle(Node node) {
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;
default:
System.err.println("Invalid node in <circle>: " + aNode);
Search WWH ::




Custom Search