kordamp / ikonli

Icon packs for Java applications
http://kordamp.org/ikonli/
Apache License 2.0
500 stars 50 forks source link

Draw FontIcon (Ikonli) directly on "javafx.scene.Canvas" #162

Open tretonio opened 8 months ago

tretonio commented 8 months ago

How to draw FontIcon (Ikonli) directly inside "javafx.scene.Canvas" ? Version 12.3.1

Thank's

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;

import javafx.scene.image.Image;

import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;

import javafx.stage.Stage;

import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid;
import org.kordamp.ikonli.javafx.FontIcon;

public class IkonliCanvas extends Application {

    @Override
    public void start(Stage primaryStage) {
        // Criar um Canvas
        Canvas canvas = new Canvas(100, 400 );
        GraphicsContext gc = canvas.getGraphicsContext2D();        

        Pane pcanvas = new Pane();
        pcanvas.getChildren().add(canvas); 

        FontIcon fi = new FontIcon(FontAwesomeSolid.APPLE_ALT);      
        fi.setIconSize(30);

        Image snapshot = fi.snapshot(new SnapshotParameters(), null);       
        gc.drawImage(snapshot, 40,100);

        Pane p = new Pane();
        p.getChildren().add(fi);
        p.setPrefWidth(100);        
        fi.setLayoutX(40);
        fi.setLayoutY(100); 

        BorderPane bp = new BorderPane();
        bp.setLeft(p);
        bp.setCenter(pcanvas);

        Scene scene = new Scene(bp, 220,400);

        primaryStage.setScene(scene);
        primaryStage.setTitle("Canvas Ikonli");
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}