2016-10-17 17 views
1

私はInkscapeで作成したSVGイメージを持っています。私はクラスと同じディレクトリに置いています。JavaFXのボタンにSVGファイルをロード

イメージをロードしてSVGパスに変換する方法はありますか?

これは、画像をgetClass().getResource("image.svg").toExternalForm()で取得し、それをimageSVG.setContent()メソッドのSVGPathに変換することです。その後、そのSVGPathオブジェクトをbutton.setGraphic()メソッドのButtonに配置したいと考えています。

TranscodersクラスまたはBufferedImageクラスは使用しません。 https://github.com/afester/FranzXaverによって提供さSvgLoader

+1

可能な重複JavaFX 2.2](http://stackoverflow.com/questions/12436274/svg-image-in-javafx-2-2) – SSchuette

+0

http://stackoverflow.com/questions/12436274で__working解決策を見てください。/svg-image-in-javafx-2-2/23894292#23894292 – SSchuette

+1

私が実装したSVGローダーを使うことができます: .svg'ファイルを開き、対応するJavaFXノードの階層を返します。https://github.com/afester/FranzXaver https://github.com/afester/FranzXaver/tree/master/Exampleの例を参照してください。 s/src/main/java/afester/javafx/examples/svgにあります。私は特にInkScapeを使って '.svg'フォーマットを検証していました –

答えて

3

、あなたは単にJavaFXのノードとしてSVGファイルをロードし、そのグラフィックなどのボタンの上にそれを設定することができます:[SVG画像内の

... 
    // 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(); 
... 

Screenshot of image on button

関連する問題