Update
JavaFX 2.2 (jdk7u6) added a Node snapshot function to the image , which would be the preferred way to do this task.
Prior to 2.2, JavaFX does not currently have a public function for converting a Node or scene into an image. There is an open function request for this http://javafx-jira.kenai.com/browse/RT-13751 (anyone can register to view the current status of the function request).
As a workaround, you can use the Swing / AWT functions to convert the JavaFX scene to an image and write the resulting image to a file:
BufferedImage img = new Robot().createScreenCapture( new java.awt.Rectangle( (int)sceneRect.getX(), (int)sceneRect.getY(), (int)sceneRect.getWidth()-1, (int)sceneRect.getHeight()-1)); File file = File.createTempFile("card", ".jpg"); ImageIO.write(img, "jpg", file);
The above code is paraphrased from: JavaFXDev: Screen capture tool .
The scenario can be defined:
Stage stage = (Stage) scene.getWindow(); stage.toFront(); Rectangle sceneRect = new Rectangle( stage.getX() + scene.getX(), stage.getY() + scene.getY(), scene.getWidth(), scene.getHeight());
If you follow the above idiom, be careful with stream processing — so that code accessing the live JavaFX script runs only in the JavaFX application thread, and AWT code only works in the AWT stream.
source share