navige
navige

Reputation: 2517

JavaFX: get BufferedImage from path

Coming from AWT/Swing, I've started experimenting a bit with JavaFX the last few days. I realized that what I used to do in thousands lines of codes can now be done in a few hundred.

One problem I came across is, however, the following: I'm trying to develop a small painting app where the user can choose brush size and color for its strokes. For all the strokes the user makes, I use the JavaFX class Path and add these paths to a Group (which is added to a Pane) where they are - automagically - painted. Now I want to store the resulting image as a jpg and try to raster all the paths in a BufferedImage. However, I found no functions in the API that help me do that.

I tried to use Canvas and its GraphicsContext, but that did not help. How could I raster all the JavaFX Paths from a list on an image?

Upvotes: 1

Views: 775

Answers (1)

jewelsea
jewelsea

Reputation: 159290

  1. Take a snapshot of your Group to get a JavaFX Image.
  2. Use SwingFXUtils to convert your JavaFX image snapshot to a buffered image.
  3. Use ImageIO to convert your buffered image to a jpeg, png, etc

Upvotes: 2

Related Questions