Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Draw SVG icons using java.awt.Graphics2D - solution

843807Sep 21 2010 — edited Sep 21 2010
Folks,

I have been searching this and other forums for info on how to draw SVG icons in my Java application. You may want to use SVG if you are doing pan/zoom where the size/orientation of icons (as displayed on screen) change. Using SVG, the icons look fantastic once scaled to a large size.

I am using the Batik SVG Toolkit (http://xmlgraphics.apache.org/batik/) for this purpose. It uses Apache license.

Assuming that you have already created SVG icons (you can use Inkscape http://inkscape.org/ to create SVG icons), the following class provides utility methods to

1. Load the SVG icon and do all the preprocessing.
2. Paint the SVG icon using the Graphics2D object
public class SvgRenderer {


	/**
	 * Method to fetch the SVG icon from a url
	 *
	 * @param url the url from which to fetch the SVG icon
	 *
	 * @return a graphics node object that can be used for painting
	 */
	public static org.apache.batik.gvt.GraphicsNode getSvgIcon(java.net.URL url) {
		org.apache.batik.gvt.GraphicsNode svgIcon = null;
		try {
			String xmlParser = org.apache.batik.util.XMLResourceDescriptor.getXMLParserClassName();
			org.apache.batik.dom.svg.SAXSVGDocumentFactory df = new org.apache.batik.dom.svg.SAXSVGDocumentFactory(xmlParser);
			org.w3c.dom.svg.SVGDocument doc = df.createSVGDocument(url.toString());
			org.apache.batik.bridge.UserAgent userAgent = new org.apache.batik.bridge.UserAgentAdapter();
			org.apache.batik.bridge.DocumentLoader loader = new org.apache.batik.bridge.DocumentLoader(userAgent);
			org.apache.batik.bridge.BridgeContext ctx = new org.apache.batik.bridge.BridgeContext(userAgent, loader);
			ctx.setDynamicState(org.apache.batik.bridge.BridgeContext.DYNAMIC);
			org.apache.batik.bridge.GVTBuilder builder = new org.apache.batik.bridge.GVTBuilder();
			svgIcon = builder.build(ctx, doc);
		} catch (Exception excp) {
			svgIcon = null;
			excp.printStackTrace();
		}
		return svgIcon;
	}


	/**
	 * Method to paint the icon using Graphics2D. Note that the scaling factors have nothing to do with the zoom
	 * operation, the scaling factors set the size your icon relative to the other objects on your canvas.
	 *
	 * @param g the graphics context used for drawing
	 *
	 * @param svgIcon the graphics node object that contains the SVG icon information
	 *
	 * @param x the X coordinate of the top left corner of the icon
	 *
	 * @param y the Y coordinate of the top left corner of the icon
	 *
	 * @param scaleX the X scaling to be applied to the icon before drawing
	 * 
	 * @param scaleY the Y scaling to be applied to the icon before drawing
	 */
	public static void paintSvgIcon(java.awt.Graphics2D g, org.apache.batik.gvt.GraphicsNode svgIcon, int x, int y, double scaleX, double scaleY) {
		java.awt.geom.AffineTransform transform = new java.awt.geom.AffineTransform(scaleX, 0.0, 0.0, scaleY, x, y);
		svgIcon.setTransform(transform);
		svgIcon.paint(g);
	}


}
Comments and feedback are welcome.

Edited by: tiwaris on Sep 21, 2010 8:53 AM

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 19 2010
Added on Sep 21 2010
0 comments
1,907 views