Skip to main content

How to output the content of a `Scene` graph in `JavaFx 2.0` to an Image



How to output the content of a Scene graph in JavaFx 2.0 to an Image. Actually, I am working on an app, which basically designs cards. So, the user just clicks to the various options to customize the scene. Finally I would like to export the scene content to an Image file. How do I do that ?




Comments

  1. For now you can try to use AWT Robot. This is not very good approach as it requires whole AWT mechanism to start. In upcoming releases FX Robot analogue may be available to be used for what purpose:

    // getting screen coordinates of a node (or whole scene)
    Bounds b = node.getBoundsInParent();
    int x = (int)Math.round(primaryStage.getX() + scene.getX() + b.getMinX());
    int y = (int)Math.round(primaryStage.getY() + scene.getY() + b.getMinY());
    int w = (int)Math.round(b.getWidth());
    int h = (int)Math.round(b.getHeight());
    // using ATW robot to get image
    java.awt.Robot robot = new java.awt.Robot();
    java.awt.image.BufferedImage bi = robot.createScreenCapture(new java.awt.Rectangle(x, y, w, h));
    // convert BufferedImage to javafx.scene.image.Image
    java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream();
    // or you can write directly to file instead
    ImageIO.write(bi, "png", stream);
    Image image = new Image(new java.io.ByteArrayInputStream(stream.toByteArray()), w, h, true, true);

    ReplyDelete
  2. JavaFX currently does not have a public function to convert a Node or Scene to an Image. There is an open feature request for this http://javafx-jira.kenai.com/browse/RT-13751 (anybody can sign up to view the current feature request status).

    As a workaround in the meantime, you could use Swing/AWT functions to convert the JavaFX scene to an image and write the resultant 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 sceneRect can be determined by:

    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 of threading - such that code accessing the live JavaFX scene only runs on the JavaFX Application Thread and the AWT code only runs on the AWT thread.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.