The Java JSVG library provides support for generation of SVG files using standard Java AWT Graphics “draw” and “fill” commands. The library provides a Graphics2D subclass, SVGGraphics, that renders draw commands into SVG syntax. A sequence of standard Java Graphics drawing method calls on an SVGGraphics instance will thus create an SVG document that, when opened with a vector graphics program such as Inkscape, will display the Java image as an SVG image that can be modified within the drawing program.
This library is released under the MIT License.
Basic Workflow
The class SVGGraphics implements the graphics operations defined by the standard Java Graphics2D class. To create an SVG drawing, you use the standard Java Graphics drawing calls (including drawing, transformations, etc.) on an instance of SVGGraphics. The SVGGraphics instance will encode these Java drawing commands as SVG XML entities in an associated SVGDocument, which can then be retrieved as a text string and saved in a flat SVG (.svg) file.
An SVGGraphics is associated with an SVGDocument. The basic workflow is:
/* Create an SVG document and get its associated SVGGraphics instance */ SVGDocument svgDocument = new SVGDocument(); SVGGraphics svgGraphics = svgDocument.getGraphics(); /* Do drawing commands as on any other Graphics. If you have a paint(Graphics) method, you can just use it with the SVGGraphics instance since it's a subclass of Graphics. */ paint(svgGraphics); /* Get the SVG output as a string - it's just text - and save in an SVG file (.svg) for use with a vector drawing program such as Inkscape */ String svgText = svgDocument.toSVGString(); String filePath = “path/to/file.svg”; FileWriter fileWriter = new FileWriter(filePath); fileWriter.write(svgText); fileWriter.flush(); fileWriter.close(); /* For drawing, just use standard Java drawing operations */ public void paint(Graphics graphics) { // set pen characteristics graphics.setColor(Color.RED); graphics.setStroke(new BasicStroke(3)); // draw stuff - line, rectangles, ovals, ... graphics.drawLine(0, 0, 1000, 500); graphics.drawRect(1000, 500, 150, 150); graphics.drawRoundRect(20, 200, 130, 100, 20, 10); graphics.drawOval(200, 800, 200, 400); graphics.drawArc(100, 1900, 400, 200, 60, 150); // can draw filled shapes graphics.setColor(Color.BLUE); graphics.fillRect(100, 100, 100, 50); int[] xPoints = {200, 300, 250}; int[] yPoints = {200, 250, 300}; graphics.fillPolygon(xPoints, yPoints, xPoints.length); // text too graphics.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 38)); graphics.drawString("Some 38-point monospaced blue text at position 480, 400", 480, 400); // and even transformations graphics.rotate(Math.PI/4); graphics.drawRect(100, 100, 200, 200); }
Features
The JSVG library provides most of the drawing operations available in the standard Java Graphics and Graphics2D classes. However, certain methods are unimplemented at present (primarily due to lack of support in SVG). These unimplemented operations will be ignored or throw an UnsupportedOperationException, as indicated below.
Supported operations
- Drawing operations, including
- drawLine, drawPolyline
- drawPolygon, fillPolygon
- drawArc, fillArc
- drawOval, fillOval
- drawRect, fillRect, clearRect
- drawRoundRect, fillRoundRect
- draw(Shape), fill(Shape), clear(Shape)
- Text operations
- drawString, drawChars, drawBytes
- Drawing properties
- Color
- Line width
- Font properties: serif/sans-serif, size, bold, italic
- Affine transformation operations
- scale
- shear
- translate
- rotate
Not supported
- Clipping, including clipRect, clipRegion, clipPath, etc. – Ignored
- Paint xfermodes – Ignored
- Image drawing – throws UnsupportedOperationException
- setStroke only accepts BasicStroke instances, otherwise throws UnsupportedOperationException
- copyArea, drawGlyphVector – throws UnsupportedOperationException
Example SVG Files
Some example files generated with JSVG are at the links below. The Java source code used to generate these through the JSVG Library is available in the Example Code section. Note that these have been named with file suffix .html to enable them to be automatically viewed in a web browser; renaming them as .svg files will enable them to be automatically opened in SVG editing programs such as Inkscape.
- Example of basic graphics operations
- Example of basic text operations
- Example of extended Java Graphics2D operations
- Example of fill operations
- Example of matrix transformation operations on graphics
- Example of matrix transformation operators on text
Example Code
A test project is available that illustrates how to use the library and that can also be used to test its functionality; it was used to generate the output files above. The class JSVGTestPanel illustrates the use of the JSVGGraphics class and its draw methods.
Documentation
Downloads
The downloads are supplied as gzipped tar archives; the library archive contains the compiled gnuapdf.jar file for inclusion in a project, while the source archive contains the Java source files that can be used to rebuild the .jar file or included as source files in a referencing project. The test app source contains source for a simple test app that uses the classes in the ASVG library.
Version |
Library (.jar) |
Source |
Test App Source | Javadoc |
1.0 | jsvg_lib_v1.0.zip | jsvg_src_v1.0.zip | jsvg_test_v1.0.zip | jsvg_doc_1.0.zip |
1.1.1 | jsvg_lib_1.1.1.zip | jsvg_src_1.1.1.zip | jsvg_test_1.1.1.zip | jsvg_doc_1.1.1.zip |
Revision History
Version |
Date | Notes |
1.0 | 2023-02-13 | Initial release |
1.1 | 2024-08-26 | Add document autosizing of viewport to fit graphical entities |
1.1.1 | 2024-08-27 | Correct svg namespace specification |