3

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?

Shreyas Dave
  • 3,815
  • 3
  • 28
  • 57
navige
  • 2,447
  • 3
  • 27
  • 53
  • doing some more tests it seems that the `fill` parameter from the `SnapshotParameters` is ignored when an image is passed into the `snapshot` function - is that possible? – navige May 19 '13 at 12:42
  • Can you post a runnable example of what you are trying to do ? – Salah Eddine Taouririt May 19 '13 at 12:56
  • There's really not much more code that is relevant to this, to be honest. So there are some `EventHandler` that create `Path` objects that are inserted either into `strokes1` or `strokes2`(depending on certain conditions). Both `strokes1` and `strokes2` are added to a `Pane` with `drawingPane.getChildren().add(strokes1)` and `drawingPane.getChildren().add(strokes2)`. – navige May 19 '13 at 13:14
  • Thing is, if I comment out ´im = strokes1.snapshot(params, im);´, I see the what comes from the line below it, and vice-versa. So the single lines work correctly, but if I try to combine both snapshots it fails. – navige May 19 '13 at 13:15

1 Answers1

2

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

Community
  • 1
  • 1
Salah Eddine Taouririt
  • 24,925
  • 20
  • 60
  • 96