JavaFX print Screen Effect - Java JavaFX

Java examples for JavaFX:Scene

Description

JavaFX print Screen Effect

Demo Code


//package com.java2s;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import javafx.animation.ScaleTransition;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import javax.imageio.ImageIO;

public class Main {

    public static void printScreenEffect(final Stage printStage) {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();

        Rectangle rectangle = new Rectangle(0, 0, dimension.getWidth(),
                dimension.getHeight());//from   w w  w  .java  2  s  .  c  o  m
        rectangle.setFill(Color.GRAY);

        StackPane stackPane = new StackPane();
        stackPane.setStyle("-fx-background-color:white");

        stackPane.getChildren().add(rectangle);

        final Stage stage = new Stage();

        stage.setX(0.0);
        stage.setY(0.0);
        stage.setWidth(dimension.getWidth());
        stage.setHeight(dimension.getHeight());
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setScene(new Scene(stackPane));

        stage.show();

        final ScaleTransition scaleTransition = new ScaleTransition();
        scaleTransition.setNode(rectangle);
        scaleTransition.durationProperty().set(Duration.millis(200));
        scaleTransition.toXProperty().set(0.0);
        scaleTransition.toYProperty().set(0.0);

        scaleTransition.setOnFinished(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                stage.close();
                try {
                    saveImageFile(sceneSnapshot(printStage), printStage);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                actionEvent.consume();
            }
        });

        scaleTransition.play();
    }

    private static void saveImageFile(WritableImage writableImage,
            Stage stage) throws IOException {
        FileChooser fileChooser = new FileChooser();

        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
                "image files (*.png)", "*.png");
        fileChooser.getExtensionFilters().add(extFilter);

        File file = fileChooser.showSaveDialog(stage);

        if (file != null) {
            /**
             * ?.
             */
            String fileName = file.getName();

            if (!fileName.toUpperCase().endsWith(".PNG")) {
                file = new File(file.getAbsolutePath() + ".png");
            }

            // PixelReader pixelReader = image.getPixelReader();
            // int width = (int) image.getWidth();
            // int height = (int) image.getHeight();
            // WritableImage writableImage = new WritableImage(pixelReader, width, height);

            ImageIO.write(SwingFXUtils.fromFXImage(writableImage, null),
                    "png", file);
        }
    }

    private static WritableImage sceneSnapshot(Stage stage) {
        // Optimized by Albert: Use javaFX Scene API Complete Snapshot.
        if (null != stage && stage.sceneProperty() != null) {
            Scene currentScene = stage.sceneProperty().get();

            int width = (int) currentScene.widthProperty().get();
            int height = (int) currentScene.heightProperty().get();

            WritableImage image = currentScene.snapshot(new WritableImage(
                    width, height));

            return image;
        }

        // ?
        return new WritableImage(800, 600);
    }
}

Related Tutorials