W JavaFX 13 migawki możemy tworzyc ze sceny (Scene), z węzła (Node) oraz z ekranu (Robot).
W tym wpisie pokazuję jak utworzyć i zapisac na dysku migawki z węzła Node.

Klasa Listing4
package snapshot2;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Rectangle2D;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Label;
import javafx.scene.image.WritableImage;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Listing4 extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        try {
            Label label = new Label("Zrzut ekranu po naciśnięciu 'Ctrl+C'");
            Circle circle = new Circle(50, 50, 30);
            circle.setFill(Color.BLUE);
            HBox root = new HBox();
            root.setSpacing(20);
            root.getChildren().addAll(label, circle);
            Scene scene = new Scene(root, 410, 200);
            scene.setCursor(Cursor.DEFAULT);
            KeyCombination kk = new KeyCodeCombination(KeyCode.C,
                    KeyCombination.CONTROL_DOWN);
            SnapshotParameters params = new SnapshotParameters();
            params.setViewport(new Rectangle2D(0, 0, 270, 100));
            scene.getAccelerators().put(kk, () -> {
                WritableImage wi = root.snapshot(params, null);
                File file = new File("src/snapshot2/ryc4.png");
                BufferedImage bi = SwingFXUtils.fromFXImage(wi, null);
                try {
                    ImageIO.write(bi, "png", file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            stage.setScene(scene);
            stage.setTitle("Scena");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Po uruchomieniu i kliknięciu CTRL^C zrobimy zrzut ze sceny. Zrzut zostanie zapisany do pliku.

Klasa Listing5
package snapshot2;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.SnapshotResult;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Callback;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Listing5 extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        try {
            Label label = new Label("Zrzut ekranu po naciśnięciu 'Ctrl+C'");
            Circle circle = new Circle(50, 50, 30);
            circle.setFill(Color.BLUE);
            HBox root = new HBox();
            root.setSpacing(20);
            root.getChildren().addAll(label, circle);
            Scene scene = new Scene(root, 410, 200);
            scene.setCursor(Cursor.DEFAULT);
            KeyCombination kk = new KeyCodeCombination(KeyCode.C,
                    KeyCombination.CONTROL_DOWN);
            SnapshotParameters params = new SnapshotParameters();
            scene.getAccelerators().put(kk, () -> {
                Callback c = (SnapshotResult sr) -> {
                    File file = new File("src/snapshot2/ryc5.png");
                    BufferedImage bi = SwingFXUtils
                            .fromFXImage(sr.getImage(), null);
                    try {
                        ImageIO.write(bi, "png", file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                };
                root.snapshot(c, params, null);
            });
            stage.setScene(scene);
            stage.setTitle("Scena");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Po uruchomieniu i kliknięciu CTRL^C zrobimy zrzut ze sceny. Zrzut zostanie zapisany do pliku.

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *