如何将JavaFX 2中的场景图形的内容输出到图像

如何将 JavaFX 2中的场景图的内容输出到图像.实际上,我正在开发一款基本上设计卡片的应用程序.因此,用户只需单击各种选项即可自定义场景.最后,我想将场景内容导出到Image文件.我怎么做 ?

解决方法

在FX 2.2中出现了新的快照功能.你可以说
WritableImage snapshot = scene.snapshot(null);

使用旧款FX,您可以使用AWT Robot.这不是一个很好的方法,因为它需要整个AWT堆栈才能启动.

// 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()),h,true,true);

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...