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

Klasa Listing1
package snapshot1;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Cursor;
import javafx.scene.Scene;
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 Listing1 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);
            scene.getAccelerators().put(kk, () -> {
                WritableImage w = new WritableImage(300, 100);
                WritableImage wi = scene.snapshot(w);
                File file = new File("src/snapshot1/ryc1.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 Listing2
package snapshot1;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Cursor;
import javafx.scene.Scene;
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 Listing2 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);
            scene.getAccelerators().put(kk, () -> {
                Callback c = (SnapshotResult sr) -> {
                    File file = new File("src/snapshot1/ryc2.png");
                    BufferedImage bi = SwingFXUtils
                            .fromFXImage(sr.getImage(), null);
                    try {
                        ImageIO.write(bi, "png", file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                };
                scene.snapshot(c, 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.

Klasa Listing3
package snapshot1;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.SnapshotResult;
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 javafx.util.Callback;

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

public class Listing3 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);
            scene.getAccelerators().put(kk, () -> {
                Callback c = (sr) -> {
                    File file = new File("src/snapshot1/ryc3.png");
                    BufferedImage bi = SwingFXUtils
                            .fromFXImage(sr.getImage(), null);
                    try {
                        ImageIO.write(bi, "png", file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                };
                WritableImage w = new WritableImage(300, 100);
                scene.snapshot(c, w);
            });
            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 *