JavaFX – Save As Image

There was a question on forum related to saving of node as image. JavaFX sample – EffectsPlayground had this feature. It uses internal APIs, so its broken in JavaFX 1.2. Community has identified a solution. Thanks to Chris Jensen for pointing me to this.

If you are using internal APIs as mentioned in forum thread, you will have to update your code to use 1.3 internal APIs. Please refer to JavaFX [1.3] – Save As Image post for update related to JavaFX 1.3.

Since it uses internal APIs, it may again break in next release. May be most of users just want to save a set of nodes displayed on screen. In that case we may get the top level container and draw the contents on to an image. Yes, draw back is that it will draw all nodes, so we may have to selectively hide nodes before we save the image.

For Applet mode, click on above image

For standalone mode

First we get the instance of top level container which may be an Applet or Frame.

public function getContainer() : Container {

    var container : Container;

    if("{__PROFILE__}" == "browser") { // Applet
        container = FX.getArgument("javafx.applet"as Applet;
    else // Standalone
        var frames = Frame.getFrames();
        // We may improve this logic so as to find the
        // exact Stage (Frame) based on its title
        container = (frames[0as JFrame).getContentPane();

    return container;

Now we can save the contents of the Container as Image

function save(container : Container, bounds : Bounds, file : File) {

    var bufferedImage = new BufferedImage(
        bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
    var graphics = bufferedImage.getGraphics();
    graphics.translate(-bounds.minX, -bounds.minY);

    ImageIO.write(bufferedImage, "png", file);

Yes, it captures the entire scene. So user has to hide the nodes which are not required before saving as image. For this I have created two groups. One group will hold all nodes that needs to be captured. Other group contains nodes which are not required (such as Button). Before taking saving as image, hide the node which contains controls etc, so that they won’t appear in saved image.

Try it out and let me know feedback

var dzone_url = “”;
var dzone_style = ‘2’;


About Rakesh Menon

10 Responses to JavaFX – Save As Image

  1. Man!
    I used this your code to alter this Aplication:
    Now you can make Sketch and Save in your Computer!!!
    Soon I will post in my site the code and explain the details for the implementation. Thanks a lot!!!!

  2. Rakesh Menon says:

    @William Antônio Siqueira Thanks! 🙂 Looking forward to see more applications from you..

  3. Oleg O. Plotnikov says:

    Thank you very much!! You really helped me.

  4. Pingback: JavaFX – Printing « Rakesh Menon

  5. Pingback: JavaFX [1.3] – Save As Image « Java, JavaFX and beyond…

  6. Amit Vijaykumar says:


    i have a question about file handling in javafx. can we save javafx nodes in a file? and can we read that file and again assign them to respective nodes?

    i have tried to convert it to java object and save which is working fine. but i am clueless about how to convert it from java object to javafx node.

    if this thing is possible pls send me the links which describes the same.

    • Rakesh Menon says:

      I haven’t tried this. So not sure if its feasible or not… When you say save javafx nodes in file, you are trying to serialize node instance or convert it to some other format (like XML)?

      • Amit Vijaykumar says:

        yes i meant to serialize a javafx node so that i can save it and if i want to use it again i can get the node back in its original format.

  7. Rambabu says:

    I am also looking for some one who helps me to write the nodes in a file and restore the nodes back. any one got the code..?
    plz post me

    Thanks in advance

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: