Example usage for javafx.scene.canvas Canvas getGraphicsContext2D

List of usage examples for javafx.scene.canvas Canvas getGraphicsContext2D

Introduction

In this page you can find the example usage for javafx.scene.canvas Canvas getGraphicsContext2D.

Prototype

public GraphicsContext getGraphicsContext2D() 

Source Link

Document

returns the GraphicsContext associated with this Canvas .

Usage

From source file:Main.java

/**
 * Creates an image canvas with the given width and height.
 *///from   w w w. j  a  v a2  s.co m
public static Canvas createImageCanvas(String url, double width, double height) {
    ImageView img = new ImageView(new Image(url, width, height, false, true));
    final Canvas canvas = new Canvas(width, height);
    final GraphicsContext gc = canvas.getGraphicsContext2D();
    gc.drawImage(img.getImage(), 0, 0);

    return canvas;
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Canvas canvas = new Canvas(300, 100);

    GraphicsContext gc = canvas.getGraphicsContext2D();

    gc.setLineWidth(2.0);/*from  w  w  w . j  a  v a  2 s.  c  o m*/
    gc.setFill(Color.RED);

    gc.strokeRoundRect(10, 10, 50, 50, 10, 10);

    gc.fillOval(70, 10, 50, 20);

    gc.strokeText("Hello Canvas", 150, 20);

    Pane root = new Pane();
    root.getChildren().add(canvas);
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setTitle("");
    stage.show();
}

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 {/* w  w  w.ja v a  2s  .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;
    }
}

From source file:org.nmrfx.processor.gui.spectra.DrawSpectrum.java

public DrawSpectrum(NMRAxis[] axes, Canvas canvas) {
    this.axes = axes;
    this.canvas = canvas;
    if (canvas != null) {
        g2 = canvas.getGraphicsContext2D();
    }/* w  ww  .j  a  v  a 2 s . co  m*/
    makeContours = new DrawTask(this);
    drawContours = new DrawContours(this);
}