Load SVG file in a Button on JavaFX

后端 未结 1 1714
说谎
说谎 2021-02-09 05:23

I have an SVG image created in Inkscape. I put it in the same directory as my class.

Is there a way to load that image and convert it to an SVG Path?

The idea be

相关标签:
1条回答
  • 2021-02-09 05:54

    With the SvgLoader provided by https://github.com/afester/FranzXaver, you can simply load an SVG file as a JavaFX node and set it on the button as its graphic:

    ...
        // load the svg file
        InputStream svgFile = 
              getClass().getResourceAsStream("/afester/javafx/examples/data/Ghostscript_Tiger.svg");
        SvgLoader loader = new SvgLoader();
        Group svgImage = loader.loadSvg(svgFile);
    
        // Scale the image and wrap it in a Group to make the button 
        // properly scale to the size of the image  
        svgImage.setScaleX(0.1);
        svgImage.setScaleY(0.1);
        Group graphic = new Group(svgImage);
    
        // create a button and set the graphics node
        Button button = new Button();
        button.setGraphic(graphic);
    
        // add the button to the scene and show the scene
        HBox layout = new HBox(button);
        HBox.setMargin(button, new Insets(10));
        Scene scene = new Scene(layout);
        primaryStage.setScene(scene);
        primaryStage.show();
    ...
    

    0 讨论(0)
提交回复
热议问题