Can I use Apache Batik to paint an SVG to an AWT Graphics2D context?

236 Views Asked by At

I have successfully read an SVG file into an org.w3c.dom.Document to see its structure. Now I'd like to render it to the screen using Java's AWT's Graphics2D.

Note: I don't want to use Swing (e.g., with org.apache.batik.swing.JSVGCanvas). All the Googling I do tells me how to write to an SVGGraphics2D. (Nifty; I use it elsewhere!) But I don't want to do that; I want to draw to the screen.

Is there a way to do this?

Or are there lower-level Swing-independent drawing classes that JSVGCanvas uses internally to draw to an AWT graphics context?

I'd hoped to find simple, clear calls that took as parameters, e.g., a Graphics2D and a Document.

[Edit by OP: Unless I dreamt it--complete with cut-and-paste-able code--I got the answer in a comment within minutes... ; ) Here's its essence for others with the need, and so people don't waste time answering again:]

// Create a user agent and document loader
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);

// Create a bridge context and GVT builder
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamic(true);
GVTBuilder builder = new GVTBuilder();

// Build the graphics node
GraphicsNode graphicsNode = builder.build(ctx, [Document]);

// Paint the graphics node to the Graphics2D object
graphicsNode.paint([Graphics2D]);

And without that comment the image I pasted of the result made the question meaningless/confusing; now may make sense. Thank you again to that moderator-deleted poster! (I think because he asked for college money...)


Image of SVG module showing SVG in a tree and drawn as an image.

0

There are 0 best solutions below