Java Reference
In-Depth Information
the font and a <string> node containing the text. The <font> node has three attributes that define the font
name, the font style, and the point size. There is also an attribute to record the maxAscent value. Here's the
code:
// Create an XML element for a sketch Text element
public void addElementNode(Document doc) {
org.w3c.dom.Element textElement = doc.createElement("text");
// Create the angle attribute and attach it to the <text> node
Attr attr = doc.createAttribute("angle");
attr.setValue(String.valueOf(angle));
textElement.setAttributeNode(attr);
// Create the maxascent attribute and attach it to the <text> node
attr = doc.createAttribute("maxascent");
attr.setValue(String.valueOf(maxAscent));
textElement.setAttributeNode(attr);
// Append the <color> and <position> nodes as children
textElement.appendChild(createColorElement(doc));
textElement.appendChild(createPositionElement(doc));
textElement.appendChild(createBoundsElement(doc));
// Create and apppend the <font> node
org.w3c.dom.Element fontElement = doc.createElement("font");
attr = doc.createAttribute("fontname");
attr.setValue(font.getName());
fontElement.setAttributeNode(attr);
attr = doc.createAttribute("fontstyle");
String style = null;
int styleCode = font.getStyle();
if(styleCode == Font.PLAIN) {
style = "plain";
} else if(styleCode == Font.BOLD) {
style = "bold";
} else if(styleCode == Font.ITALIC) {
style = "italic";
} else if(styleCode == Font.ITALIC + Font.BOLD) {
style = "bold-italic";
}
assert style != null;
attr.setValue(style);
fontElement.setAttributeNode(attr);
attr = doc.createAttribute("pointsize");
attr.setValue(String.valueOf(font.getSize()));
fontElement.setAttributeNode(attr);
textElement.appendChild(fontElement);
// Create the <string> node
Search WWH ::




Custom Search