navige
navige

Reputation: 2517

JavaFX: multiple snapshots in single image

I learned to use snapshot to make an Image object out of a node. Having multiple Groups that hold various strokes, I'm now trying to create a single Image with strokes from both groups. For this purpose I'm using the following code:

Group strokes1;
Group strokes2;
WriteableImage im = null;

SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
params.setViewport(new Rectangle2D(0, 0, 400, 400)); 

im = strokes1.snapshot(params, im);
im = strokes2.snapshot(params, im);

The documentation for the snapshot function says that

"If the image is non-null, the node will be rendered into the existing image."

However, the resulting Image im only contains strokes from strokes2. What am I doing wrong?

Upvotes: 3

Views: 1079

Answers (1)

Salah Eddine Taouririt
Salah Eddine Taouririt

Reputation: 26415

One way to achieve your goal is to use setComposite() method on the Graphics2D of the BufferedImage to be converted, this solution is discussed briefly here and here.

this gist provide a runnable complete example of the approach.

App snapshot

Image snapshot

Upvotes: 2

Related Questions