Forum Stats

  • 3,827,608 Users
  • 2,260,798 Discussions
  • 7,897,314 Comments

Discussions

Snapshot of any node throws IllegalArgumentException when screen is locked - how can we avoid this?

Andipa
Andipa Member Posts: 25
edited Sep 19, 2013 1:50PM in JavaFX 2.0 and Later

Here's some example code that won't work if the snapshot is taken while the screen is locked (and it was locked after application start) in both Java 7 and 8.


Is there a way to test whether the screen is locked or even better get notified when it is unlocked, so we don't accidentally run into this?

I also suspect there are more causes than just the screen being locked, so it would be great to know when this can happen in general.


import javafx.application.Application;

import javafx.application.Platform;

import javafx.scene.Scene;

import javafx.scene.layout.BorderPane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;

import javafx.stage.Stage;

public class SnapshotTester extends Application {

    @Override

    public void start(Stage primaryStage) throws Exception {

        final BorderPane borderPane = new BorderPane();

        final Rectangle rectangle = new Rectangle(20, 20, Color.BLUE);

        borderPane.setCenter(rectangle);

        Scene scene = new Scene(borderPane);

        primaryStage.setScene(scene);

        primaryStage.show();

        new Thread(new Runnable() {

            @Override

            public void run() {

                try {

                    Thread.sleep(2000);

                    Platform.runLater(new Runnable() {

                        @Override

                        public void run() {

                            Image snapshot = rectangle.snapshot(null, null);

                            System.out.println("Snapshot taken: " + snapshot);

                        }

                    });

                } catch (InterruptedException ignored) {

                }

            }

        }).start();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

To reproduce do the following:

1) Start application

2) Lock screen as soon as window is shown

3) Unlock after two seconds


You'll get the following exception which is unfortunately not very expressive:


java.lang.IllegalArgumentException: Unrecognized image loader: null
at javafx.scene.image.WritableImage.loadTkImage(WritableImage.java:230)
at javafx.scene.image.WritableImage.access$000(WritableImage.java:44)
at javafx.scene.image.WritableImage$1.loadTkImage(WritableImage.java:49)
at javafx.scene.Scene.doSnapshot(Scene.java:1144)
at javafx.scene.Node.doSnapshot(Node.java:1632)
at javafx.scene.Node.snapshot(Node.java:1710)



Tagged:

Answers

This discussion has been closed.