Skip to content

Instantly share code, notes, and snippets.

@ComFreek
Last active April 9, 2024 16:59
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ComFreek/b0684ac324c815232556 to your computer and use it in GitHub Desktop.
Save ComFreek/b0684ac324c815232556 to your computer and use it in GitHub Desktop.
JavaFX & Batik SVG Renderer: Minimal example
/**
* This is a compilation of code snippets required to render SVG files in JavaFX using Batik.
* See my full post on StackOverflow: http://stackoverflow.com/a/23894292/603003
*/
package proofofconcept;
import java.awt.image.BufferedImage;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.ImageTranscoder;
/**
* Many thanks to bb-generation for sharing this code!
* @author bb-generation
* @link https://web.archive.org/web/20131215231214/http://bbgen.net/blog/2011/06/java-svg-to-bufferedimage/
* @license Unfortunately unknown, but using this code is probably categorized as "fair use" (because the code is in my opinion too simple to be licensed)
*/
public class BufferedImageTranscoder extends ImageTranscoder {
private BufferedImage img = null;
@Override
public BufferedImage createImage(int width, int height) {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
return bi;
}
@Override
public void writeImage(BufferedImage img, TranscoderOutput to) throws TranscoderException {
this.img = img;
}
public BufferedImage getBufferedImage() {
return img;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="proofofconcept.MainMenuController">
<children>
<HBox layoutX="60.0" prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<ImageView fx:id="imgView" fitHeight="150.0" fitWidth="200.0" layoutX="60.0" pickOnBounds="true" />
</children>
</HBox>
</children>
</AnchorPane>
package proofofconcept;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
public class MainController implements Initializable {
@FXML
private ImageView imgView;
@Override
public void initialize(URL url, ResourceBundle rb) {
BufferedImageTranscoder trans = new BufferedImageTranscoder();
// In my case I have added a file "svglogo.svg" in my project source folder within the default package.
// Use any SVG file you like! I had success with http://en.wikipedia.org/wiki/File:SVG_logo.svg
try (InputStream file = getClass().getResourceAsStream("/svglogo.svg")) {
TranscoderInput transIn = new TranscoderInput(file);
try {
trans.transcode(transIn, null);
// Use WritableImage if you want to further modify the image (by using a PixelWriter)
Image img = SwingFXUtils.toFXImage(trans.getBufferedImage(), null);
imgView.setImage(img);
} catch (TranscoderException ex) {
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}
}
catch (IOException io) {
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, io);
}
}
}
package proofofconcept;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ProofOfConcept extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment