
net.javainthebox.caraibe.svg.SVGContent Maven / Gradle / Ivy
package net.javainthebox.caraibe.svg;
import java.util.HashMap;
import java.util.Map;
import javafx.scene.Group;
import javafx.scene.Node;
/**
* SVGContent express SVG content.
*
* SVGContent has a root group. The root is a Group object, therefore is able to be added to scene
* graph:
*
*
*
*
URL url = ...;
SVGContent content = SVGLoader.load(url);
container.getChildren().add(content.getRoot());
*
*
* getNode() method returns Node object represented by ID. When loading following SVG file,
* Rectangle object is gotten by getNode() method.
*
*
* rectangle.svg
*
*
*
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="layer_1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" width="300px" height="200px"
viewBox="0 0 300 200"
style="enable-background:new 0 0 300 200;"
xml:space="preserve">
<rect id="rect"
x="100" y="50"
width="100" height="80"
style="fill:#FFFFFF; stroke:#000000;"/>
</svg>
*
*
* Java code is follows:
*
*
*
* SVGContent content = SVGLoader.load("rectangle.svg");
* Rectangle rect = (Rectangle) content.getNode("rect");
*
*
* getGroup() method returns Group object represented by ID. When loading following SVG file, Group
* object is gotten by getNode() method.
*
*
* group.svg
*
*
*
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" width="200px" height="200px" viewBox="0 0 200 200"
style="enable-background:new 0 0 200 200;" xml:space="preserve">
<g id="group">
<circle style="fill:#FF0000;stroke:#000000;" cx="100" cy="100" r="50"/>
</g>
</svg>
*
*
* Java code is follows:
*
*
*
* SVGContent content = SVGLoader.load("group.svg");
* Group group = content.getGroup("group");
*
*
* note: There are many unsupport SVG element.
*/
public class SVGContent extends Group
{
private Map nodes = new HashMap<>();
private Map groups = new HashMap<>();
void putNode(String id, Node node)
{
nodes.put(id, node);
}
/**
* Gets node object indicated by id. When there is no node indicated by id, return null.
*
* @param id the name of node
* @return node represented by id
*/
public Node getNode(String id)
{
return nodes.get(id);
}
void putGroup(String id, Group group)
{
groups.put(id, group);
}
/**
* Gets group object indicated by id. When there is no group indicated by id, return null.
*
* @param id the name of group
* @return group represented by id
*/
public Group getGroup(String id)
{
return groups.get(id);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy