Example usage for javafx.scene.canvas GraphicsContext fillText

List of usage examples for javafx.scene.canvas GraphicsContext fillText

Introduction

In this page you can find the example usage for javafx.scene.canvas GraphicsContext fillText.

Prototype

public void fillText(String text, double x, double y) 

Source Link

Document

Fills the given string of text at position x, y with the current fill paint attribute.

Usage

From source file:ijfx.ui.previewToolbar.DefaultWidget.java

@Override
public Image getImage(PreviewService previewService, int size) {

    if (image != null)
        return image;

    if (previewService == null)
        return null;

    if (previewService.getImageDisplayService().getActiveDataset() == null) {
        return null;
    } else if (this.getIcon().equals("preview")) {
        try {//from   w ww .  j a va  2  s  .c  o  m
            previewService.setParameters(-1, -1, size, size);
            return previewService.getImageDisplay(action, this.getParameters());

        } catch (Exception e) {
            e.printStackTrace();
            FontAwesomeIconView fontAwesomeIconView = new FontAwesomeIconView(FontAwesomeIcon.AMBULANCE);
            return FontAwesomeIconUtils.FAItoImage(fontAwesomeIconView, size);
        }
    }

    else if (getIcon().startsWith("char:")) {
        Canvas canvas = new Canvas(size, size);
        GraphicsContext graphicsContext2D = canvas.getGraphicsContext2D();

        graphicsContext2D.setFill(Color.WHITE);
        graphicsContext2D.setFont(javafx.scene.text.Font.font("Arial", size));
        graphicsContext2D.fillText(getIcon().substring(5), size / 3, size * 0.8);

        final SnapshotParameters params = new SnapshotParameters();
        params.setFill(Color.TRANSPARENT);
        //            final WritableImage snapshot = canvas.snapshot(params, null);

        Task<WritableImage> getIcon = new CallbackTask<Canvas, WritableImage>(canvas)
                .run(input -> input.snapshot(params, null));

        Platform.runLater(getIcon);

        try {
            // Image image = new Ima

            image = getIcon.get();
            return image;
        } catch (InterruptedException ex) {
            Logger.getLogger(DefaultWidget.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExecutionException ex) {
            Logger.getLogger(DefaultWidget.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    //Check if icon exist in Enumeration
    else if (Arrays.stream(FontAwesomeIcon.values()).filter(e -> e.name().equals(icon)).count() > 0) {

        FontAwesomeIconView fontAwesomeIconView = new FontAwesomeIconView(FontAwesomeIcon.valueOf(icon));
        return FontAwesomeIconUtils.FAItoImage(fontAwesomeIconView, size);
    } else {
        image = new Image(getClass().getResource(icon).toExternalForm(), size, size, true, true);
        return image;
    }
}