I have successfully read an SVG file into an org.w3c.dom.Document to see the structure. Now I would 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 tried to look through the JSVGCanvas source code and was confused by the complexity.
I had hoped to find simple, clear calls that took as parameters, e.g., a Graphics2D and a Document--or a Graphics2D and a drawable SVGElement (like an SVGCircleElement).
CodePudding user response:
Yes, you can use Apache Batik to paint an SVG to an AWT Graphics2D context. Apache Batik is a Java library that can be used to create, modify, and display Scalable Vector Graphics (SVG) images.
To paint an SVG to an AWT Graphics2D context using Apache Batik, you can use the GVTBuilder and GraphicsNode classes. Here is an example of how you can do this:
import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.svg.SVGDocument;
public class SVGToGraphics2D {
public static void main(String[] args) throws IOException {
// Load the SVG document
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
File svgFile = new File("path/to/svg/file.svg");
SVGDocument svgDoc = factory.createSVGDocument(svgFile.toURI().toString());
// 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, svgDoc);
// Get the width and height of the SVG document
int width = (int) ctx.getDocumentSize().getWidth();
int height = (int) ctx.getDocumentSize().getHeight();
// Create a Graphics2D object
Graphics2D g2d = ...;
// Paint the graphics node to the Graphics2D object
graphicsNode.paint(g2d);
}
}
This example loads an SVG file, creates a BridgeContext and GVTBuilder, builds a GraphicsNode from the SVG document, and then paints the GraphicsNode to the Graphics2D object. The width and height of the SVG document are used to set the size of the Graphics2D object.
This example loads an SVG file, creates a BridgeContext and GVTBuilder, builds a GraphicsNode from the SVG document, and then paints the GraphicsNode to the Graphics2D object. The width and height of the SVG document are used to set the size of the Graphics2D object.
You will need to include the Apache Batik library in your project to use these classes. You can download the Apache Batik library from the Apache Batik website (https://xmlgraphics.apache.org/batik/).
Is it clear? If not, just ask in the comment section.
CodePudding user response:
Image of SVG module showing SVG in a tree and drawn as an image.