Dear All,
1. I am trying to add an rect to the SVGDocument and then display it in the
JFrame.
The JSVGCanvas can only display things which are on the main.svg, I cannot
get it to
display the new rect element which i have just added. I hope someone would
help me out.
Thank you so much. The source code appended in the end of the message.
2. I cannot find the org.w3c.dom.svg.SVGDocument java source and
documentation in batik 1.7.
Is it only available in batik 1.5? Does anyone know where I can find it?
Thank you very much.
Kind regards,
Qian
/**********************MainSVGJFrame.java*********************************/
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;
public class MainSVGJFrame extends javax.swing.JFrame {
JSVGCanvas jsvgcanvas;
SVGDocument svgDoc;
Element svgRoot;
/** Creates new form MainSVGJFrame */
public MainSVGJFrame() {
initComponents();
jsvgcanvas = new JSVGCanvas();
jsvgcanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
try{
svgDoc =
(SVGDocument)f.createDocument("file:d:/workspace/TestSVG/svgElement/main.svg");
//svgDoc =
(SVGDocument)f.createDocument("file:/home/qianli/workspace/TestSVG/svgElement/svgBox.svg");
}catch(Exception e){
e.printStackTrace();
}
// get the root element (the svg element)
svgRoot = svgDoc.getRootElement() ;
System.out.println("svgDoc:::"+svgDoc.toString());
System.out.println("svgRoot:::"+svgRoot.getTagName() );
// set the width and height attribute on the root svg element
svgRoot.setAttributeNS(null, "width", "400");
svgRoot.setAttributeNS(null, "height", "450");
// create the rectangle
Element rectangle = svgDoc.createElement("rect");
rectangle.setAttributeNS(null, "id", "box");
rectangle.setAttributeNS(null, "x", "20");
rectangle.setAttributeNS(null, "y", "20");
rectangle.setAttributeNS(null, "rx", "20");
rectangle.setAttributeNS(null, "ry", "20");
rectangle.setAttributeNS(null, "width", "150");
rectangle.setAttributeNS(null, "height", "100");
rectangle.setAttributeNS(null, "style",
"fill:red;stroke:black;stroke-width:5;opacity:0.5");
// attach the rectangle to the svg root element
svgRoot.appendChild(rectangle);
Element box = svgDoc.getElementById("box");
System.out.println("box:::"+box.getTagName() );
jsvgcanvas.setDocument(svgDoc);
jsvgcanvas.updateUI();
// jsvgcanvas.setURI
("file:/home/qianli/workspace/TestSVG/svgElement/svgBox.svg");
jPanelMain.add(jsvgcanvas);
this.pack();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code
">
private void initComponents() {
jPanelMain = new javax.swing.JPanel();
jPanelControl = new javax.swing.JPanel();
jButtonCreateBox = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanelMain.setLayout(new java.awt.BorderLayout());
jPanelMain.setBorder(javax.swing.BorderFactory.createEtchedBorder
());
jPanelMain.setPreferredSize(new java.awt.Dimension(500, 500));
getContentPane().add(jPanelMain, java.awt.BorderLayout.CENTER);
jPanelControl.setLayout(new java.awt.GridBagLayout());
jPanelControl.setBorder(javax.swing.BorderFactory.createEtchedBorder
());
jButtonCreateBox.setText("Create Box");
jButtonCreateBox.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonCreateBoxActionPerformed(evt);
}
});
jPanelControl.add(jButtonCreateBox, new java.awt.GridBagConstraints
());
getContentPane().add(jPanelControl, java.awt.BorderLayout.SOUTH);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainSVGJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonCreateBox;
private javax.swing.JPanel jPanelControl;
private javax.swing.JPanel jPanelMain;
// End of variables declaration
}
/**********************
main.svg************************************************/
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg id="rootSVG" width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<text id="mainText" x="100" y="50" > Main Frame
</text>
</svg>