Download SVG file in javafx 2.0

I would like to display the svg image in javafx 2.0, but I did not find one in the API. I assume it is still in beta.

Until the final version, how can I download svg? Is there already a library that can handle this, or do I need to parse the file and then create the appropriate forms?

thanks

+4
source share
5 answers

Based on the answer to this question, I found a working solution.

1. Include links to Batik SVG Toolkit jars

2. Implement your own transcoder
(based on this answer by Devon_C_Miller )

class MyTranscoder extends ImageTranscoder { private BufferedImage image = null; @Override public BufferedImage createImage(int w, int h) { image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); return image; } @Override public void writeImage(BufferedImage img, TranscoderOutput out) { } public BufferedImage getImage() { return image; } 

}

3. Get BufferedImage from your svg
(based on a hint of this answer by John Doppelmann )

 String uri = "path_to_svg/some.svg"; MyTranscoder transcoder = new MyTranscoder(); TranscodingHints hints = new TranscodingHints(); hints.put(ImageTranscoder.KEY_WIDTH, 20f); //your image width hints.put(ImageTranscoder.KEY_HEIGHT, 20f); //your image height hints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, SVGDOMImplementation.getDOMImplementation()); hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, SVGConstants.SVG_NAMESPACE_URI); hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, SVGConstants.SVG_SVG_TAG); hints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, false); transcoder.setTranscodingHints(hints); TranscoderInput input = new TranscoderInput(url.toExternalForm()); transcoder.transcode(input, null); BufferedImage bufferedImage = transcoder.getImage(); 

4. Create an InputStream from BufferedImage

 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); JPEGImageEncoder imageEncoder = JPEGCodec.createJPEGEncoder(outputStream); imageEncoder.encode(bufferedImage); byte[] bytes = outputStream.toByteArray(); InputStream inputStream = new ByteArrayInputStream(bytes); 

5. Add an image to ImageView

 //javafx.scene.image.Image Image image = new Image(inputStream); //javafx.scene.image.ImageView ImageView imageView = new ImageView(); imageView.setImage(image); this.getChildren().add(imageView); 

Hope this helps!

+8
source

I used @pmoule's answer above, but he was lucky to replace his steps 3-5 with the following:

 MyTranscoder imageTranscoder = new MyTranscoder(); imageTranscoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) width); imageTranscoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float) height); TranscoderInput input = new TranscoderInput(new FileReader(file)); imageTranscoder.transcode(input, null); BufferedImage bimage = imageTranscoder.getImage(); WritableImage wimage = SwingFXUtils.toFXImage(bimage, null); ImageView imageView = new ImageView(wimage); <panel-vbox-whatever>.getChildren().clear(); <panel-vbox-whatever>.getChildren().add(imageView); 

Easier and it just worked better for me.

+2
source

you do not need batik, you can try WebView as follows:

 WebView view = new WebView(); view.setMinSize(500, 400); view.setPrefSize(500, 400); final WebEngine eng = view.getEngine(); eng.load("http://127.0.0.1/demo1.svg"); 
+1
source

Convert SVG to FXML and then it will become trivial. Conversion can be done using XSLT. You can get the stylesheet from here:

http://jayskills.com/blog/2012/06/03/svg-to-fxml-using-netbeans/

Then, after converting to FXML, you can load it as a Node with the built-in FXMLLoader:

 FXMLLoader.setDefaultClassLoader(this.getClass().getClassLoader()); URL location = KayakMain.class.getResource("path/to/my/resource.fxml"); Parent root = FXMLLoader.load(location); 
0
source

All Articles