You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Toto Laricot <to...@gmail.com> on 2007/12/06 21:29:11 UTC
Swing component to SVG - Is it possible?
Hi all,
Sorry if this is a dumb question but I was under the impression that Batik
could let me convert a swing component into an svg document (that I could
later edit, resize et.) in a headless environment (server-side).
So I tried the following code:
JFrame f = new JFrame("test");
f.add(new JLabel("Some text"));
f.setPreferredSize(new Dimension(500,500));
f.pack();
f.setVisible(true);
DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
final String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document document = domImpl.createDocument(svgNS, "svg", null);
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
f.paint(svgGenerator);
boolean useCSS = true;
Writer out = new OutputStreamWriter(System.out, "UTF-8");
svgGenerator.stream(out, useCSS);
But much to my chagrin 1) it generates a png image inside a <svg> tag, and
2) it doesn't work if I don't call the setVisible(true) method.
If when I call:
svgGenerator.fill(new Rectangle(10, 10, 100, 100));
the library generates:
<rect x="10" width="100" height="100" y="10" style="stroke:none;"/>
Instead of a png representing the rectangle. How come it does not do the
same thing with my JFrame?
Thanks,
-Toto.
Re: Swing component to SVG - Is it possible?
Posted by th...@kodak.com.
Hi Toto,
"Toto Laricot" <to...@gmail.com> wrote on 12/06/2007 03:29:11 PM:
> Sorry if this is a dumb question but I was under the impression that
> Batik could let me convert a swing component into an svg document
> (that I could later edit, resize et.) in a headless environment
> (server-side).
This is correct.
> So I tried the following code:
[...]
> But much to my chagrin 1) it generates a png image inside a <svg>
> tag, and 2) it doesn't work if I don't call the setVisible(true) method.
#1 is because you didn't turn off Swing's offscreen buffer.
So when you call f.paint(svgGenerator) all Swing painted to
our svgGenerator was a bitmap.
As for #2, if the component isn't visible what do you expect
it to paint to our svgGenerator?
You might find batik.svggen.SwingPrettyPrint useful
here. I suspect you still need to do #2 before rendering
the swing component.