I want to use batik to generate svg and display it, and I have a xml
configuration file(meta.xml). I read configuration from the meta.xml
and use batik to generate the corresponding svg,but it can't display
in JSVGCanvas. But if I use SVGGraphics2D,it works fine,see comment
code.I don't where the problem is,and I hope some help,thanks.
Java Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.io.File;
import java.io.OutputStreamWriter;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.swing.JSVGCanvas;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.svg.SVGDocument;
/*
* Created on 2005-5-10
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ImageTest1 {
private Shape circle = null;
private DOMImplementation impl = null;
private final String svgNS =
SVGDOMImplementation.SVG_NAMESPACE_URI;
private static JPanel group = null;
public ImageTest1() {
impl = SVGDOMImplementation.getDOMImplementation();
loadDefaultMeta();
}
private void loadDefaultMeta() {
File file = new File("meta.xml");
try {
Document doc =
DocumentBuilderFactory.newInstance()
.newDocumentBuilder
().parse(file);
group = new JPanel();
NodeList list = doc.getElementsByTagName
("View");
for (int j = 0; j < list.getLength();
j++) {
JSVGCanvas label = new JSVGCanvas
();
group.add(label);
Node node = list.item(j);
Node view = node.getChildNodes
().item(1);
SVGDocument svg = (SVGDocument)
impl.createDocument(svgNS,
"svg", null);
Element g = svg.createElement
("g");
label.setDocumentState
(JSVGCanvas.ALWAYS_DYNAMIC);
view = svg.importNode(view,
true);
g.appendChild(view);
svg.getDocumentElement
().appendChild(g);
SVGGraphics2D g2d = new
SVGGraphics2D(svg);
/*
* circle = new Ellipse2D.Double
(0, 0, 200, 200);
* g2d.setPaint(Color.red);
g2d.fill(circle);
* g2d.setSVGCanvasSize(new
Dimension(200, 200)); Element root =
* svg.getDocumentElement();
g2d.getRoot(root);
*/
g2d.stream(svg.getDocumentElement
(), new OutputStreamWriter(
System.out, "UTF-8"));
label.setDocument(svg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ImageTest1();
JFrame frame = new JFrame();
frame.getContentPane().add(group);
frame.setSize(600, 600);
frame.setVisible(true);
}
}
meta.xml:
<?xml version="1.0" encoding="UTF-8"?>
<MetaList>
<Info>
<Author>WebMap Studio Group</Author>
<Title>metaGraph </Title>
</Info>
<List>
<Meta>
<Title>Line</Title>
<Description>Line</Description>
<ActionName>draw</ActionName>
<View>
<line Type="Line" style="fill:rgb
(255,255,255);fill-opacity:1;stroke:rgb(0,0,0);stroke-opacity:1;"
x1="0" y1="0" x2="24" y2="24" />
</View>
</Meta>
<Meta>
<Title>Circle</Title>
<Description>Circle</Description>
<ActionName>draw</ActionName>
<View>
<circle Type="Circle"
style="fill:rgb(255,255,255);fill-opacity:1;stroke:rgb(0,0,0);stroke-
opacity:1;" cx="190.5" cy="156.5" r="111.5" />
</View>
</Meta>
<Meta>
<Title>Rect</Title>
<Description>Rect</Description>
<ActionName>draw</ActionName>
<View>
<rect style="fill:rgb
(255,255,255);fill-opacity:1;stroke:rgb(0,0,0);stroke-opacity:1;"
x="102" y="51" width="231" height="174" />
</View>
</Meta>
<Meta>
<Title>Text</Title>
<Description>Text</Description>
<ActionName>draw</ActionName>
<View>
<text style="fill:rgb
(255,255,255);fill-opacity:1;stroke:rgb(0,0,0);stroke-opacity:1;"
font-family="Arial" font-size="12" font-weight="normal" font-
style="normal" text-decoration="none" x="101.7544" y="181.5789" >����
��ʾ�ı�</text>
</View>
</Meta>
<Meta>
<Title>polyline </Title>
<Description>polyline </Description>
<ActionName>draw</ActionName>
<View>
<polyline style="fill:rgb
(255,255,255);fill-opacity:1;stroke:rgb(0,0,0);stroke-opacity:1;"
points="3.508769 14.03508,11.40351 5.263155,11.40351
18.42105,21.92982 6.140348" type="polyline"/>
</View>
</Meta>
<Meta>
<Title>Image</Title>
<Description>Image</Description>
<ActionName>draw</ActionName>
<View>
<image x="0" y="0" width="24"
height="24" xlink:href="" xmlns:xlink="http://www.w3.org/1999/xlink"
type="image"/>
</View>
</Meta>
</List>
</MetaList>
-----
To unsubscribe send a message to: [EMAIL PROTECTED]
-or-
visit http://groups.yahoo.com/group/svg-developers and click "edit my
membership"
----
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/svg-developers/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/