If you're using a different build system, you'll have to change the dependencies accordingly. From there, here's a class that uses SVG Salamander to scale and rasterize a vector graphic resource file:
import com.kitfox.svg.SVGDiagram;
import com.kitfox.svg.SVGException;
import com.kitfox.svg.SVGUniverse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Map;
import static java.awt.RenderingHints.*;
import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
/**
* Responsible for converting SVG images into rasterized PNG images.
*/
public class SvgRasterizer {
public final static Map
Use the SvgRasterizer as follows:
final var rasterizer = new SvgRasterizer();
final var image = rasterizer.rasterize( "/images/icon.svg", new Dimension( 200, 200 ) );
That image can be added to a Swing component without much effort. For example, here's a JComponent that can be treated much like any other:
import javax.swing.*;
import java.awt.*;
/**
* Responsible for drawing an image, which can be changed at any time.
*/
public class ImageComponent extends JComponent {
/**
* Mutable image.
*/
private Image mImage;
ImageComponent( final Image image ) {
mImage = image;
}
@Override
public Dimension getPreferredSize() {
// Race-condition guard.
final var image = mImage;
return new Dimension(
image.getWidth( null ), image.getHeight( null )
);
}
@Override
protected void paintComponent( final Graphics graphics ) {
super.paintComponent( graphics );
final var g = (Graphics2D) graphics.create();
g.drawImage( mImage, 0, 0, this );
}
/**
* Repaints this component using the given image. This is a mutable
* operation that changes the internal {@link Image} instance.
*
* @param image The new image to use for painting.
*/
public void redraw( final Image image ) {
mImage = image;
repaint();
}
}