Hello everyone,
I was following the example on the SVG Generator page
(http://xmlgraphics.apache.org/batik/using/svg-generator.html), but I am
running into trouble with the MySVGGenerator class. My IDE (Netbeans 7.3) warns
me that I cannot call "myCanvas.paintComponent(svgGenerator)", because
"paintComponent(Graphics) has protected access in JComponent". Could anyone
help me understand why I'm getting this error? For the record, I'm using Java 6
and Batik 1.7, and I have made certain that the library has been added to my
project properly.
-CJB
Here is my code in full for "MySVGGenerator.java":
package testsvggen;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import javax.swing.JPanel;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
class MySVGGenerator {
static String svgNS = "http://www.w3.org/2000/svg";
public static void generateSVG(JPanel myCanvas, OutputStream outStream)
throws UnsupportedEncodingException, SVGGraphics2DIOException {
DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
Document myFactory = domImpl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(myFactory);
ctx.setComment("Testing 123");
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
// Create the SVG DOM tree.
myCanvas.paintComponent(svgGenerator);
Writer out = new OutputStreamWriter(outStream, "UTF-8");
svgGenerator.stream(out, true);
}
}