Andreas
Andreas

Reputation: 347

JavaFX snapshot of the node

I can not figure out how to make a snapshot of group of nodes. Namely, I'd like to make an image of PNG icon overlayed with a number (e.g. unread messages).

My code:

int totalNumberOfUnreadMessages = 1;

ImageView applicationIconImageView = new ImageView(getApplication().getStage().getIcons().get(1));

Text text = new Text();            
            text.setX(10);
            text.setY(10);
            text.setFill(Color.RED);
            text.setText(Integer.toString(totalNumberOfUnreadMessages));
            text.setFont(Font.font(null, FontWeight.BOLD, 10));            

            Group group = new Group();
            group.getChildren().addAll(applicationIconImageView, text);            

            MainFrameWindow.runOnUIThread(() -> {
                getApplication().getStage().getIcons().clear();
                WritableImage applicationIconWritableImage = group.snapshot(new SnapshotParameters(), null);                
                getApplication().getStage().getIcons().add(0, applicationIconWritableImage);
            }, false);

Any help is much appreciated. Thank you all in advance.

Upvotes: 1

Views: 1293

Answers (1)

Andreas
Andreas

Reputation: 347

BufferedImage bufferedImage = new BufferedImage(dimension, dimension, BufferedImage.TYPE_INT_ARGB);
SwingFXUtils.fromFXImage(scene.snapshot(new WritableImage(dimension, dimension)), bufferedImage);
SwingFXUtils.toFXImage(bufferedImage, new WritableImage(dimension, dimension));

Upvotes: 2

Related Questions