Re: rendering directly to java.awt.Graphics2D

Mike Jarmy <[email protected]>
Newsgroups gmane.text.xml.batik.user
Message-ID <CADjkO6iyzmC2ys2oK7Z8TRU5V6zaKT84bdo6d70wbEjuX+1ffg@mail.gmail.com>
On Wed, Aug 3, 2011 at 1:03 PM, Mike Jarmy <[email protected]> wrote:
> I'm working with a custom widget toolkit that is built directly on top
> of java.awt -- in other words I'm not using Swing, or SWT, or anything
> other than a framework built on plain old AWT.
>
> I'd like to be able to render (and animate) SVG drawings directly onto
> a java.awt.Graphics2D instance.  However, I'm having a bit of trouble
> figuring out how exactly to do that with batik.  I *think* that I need
> to decode my SVG drawing into a GVT tree, and then render the tree.
> However I can't figure out which part of batik I need in order to do
> the rendering.  Transcoder and SVGGraphics2D do not seem to be quite
> what I want. Actually, I haven't yet figured out the
> decoding-to-GVT-step either, but I'm assuming that's pretty
> straightforward.
>
> So can anyone give me a pointer on how to do this?  Perhaps just point
> me to an existing example?  My first goal is to just get static images
> rendering.
>
> After that, I'll need to get animation working.  I'm assuming that
> supporting animation will entail updating the drawing's current time
> every so oftern on a separate thread, via the AnimationEngine, and
> then re-rendering the updated GVT?
>
> I *could* do all this by rendering into a static java.awt.Image, and
> then bliting the Image (i.e. roll my own double-buffering), but I'd
> prefer to figure out how to just draw directly onto a Graphics2D
> instance first.  That's because the framework I'm using already has
> some double-buffering, so I most likely wont need to do that with the
> SVG rendering.
>
> Thanks,
> Mike Jarmy
>
> P.S. FYI, I'll probably never need to support handling interactive user input.
>

OK, I have discovered the GraphicsNode.paint(Graphics2D g2d)  method,
which may be all I need to call once I actually get a valid GVT build.
 However, I'm having trouble building a GVT from a
java.io.InputStream.  Here is the code I'm using:

            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
//            String uri = "http://www.example.org/document.svg";
            Document document = f.createDocument(null, inputStream);

            UserAgent userAgent = new UserAgentAdapter();
            DocumentLoader loader = new DocumentLoader(userAgent);
            BridgeContext bctx = new BridgeContext(userAgent, loader);
            bctx.setDynamicState( BridgeContext.DYNAMIC );
            GVTBuilder builder = new GVTBuilder();
            this.gvtRoot = builder.build(bctx, document);

This code fails at the call to GVTBuilder.build(), with the following error:

Invalid CSS document.
Unable to make sense of URL for connection
        at org.apache.batik.css.engine.CSSEngine.parseStyleSheet(CSSEngine.java:1149)
        at org.apache.batik.dom.svg.SVGDOMImplementation.createCSSEngine(SVGDOMImplementation.java:117)
        at org.apache.batik.dom.ExtensibleDOMImplementation.createCSSEngine(ExtensibleDOMImplementation.java:212)
        at org.apache.batik.bridge.BridgeContext.initializeDocument(BridgeContext.java:378)
        at org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:55)
        at com.tridium.svg.ui.BSvgDrawing.loadDrawing(BSvgDrawing.java:305)

Could that be because I'm passing a null value in for the URI when I
create the document from the InputStream?  I have noticed that I get
the same error no matter what I pass in for the URI.  Is there perhaps
a better way to construct a Document from an InputStream?

Thanks,
Mike Jarmy
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.