import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
/**
*
* @author Narayan
*/
publicclassFXImaging{
/**
* Some of the important private variables;
*/
privateJFXPanel fxPanel;
privateintTIME=200;
privateFile file;
privateJFrame frame;
privateBoundingBox boundbox;
privateTimer timer;
privateStage stage;
privateScene scene;
privateObservableList list;
privateNode node;
/**
*
* @param scene the scene which is to be imaged
* @param save place where the image to be saved
* @param width width of image
* @param height height of image
*/
publicvoidsceneToImage(finalScene scene,finalFile save,doublewidth,doubleheight){
stage=(Stage)scene.getWindow();
this.scene=scene;
BoundingBox bound=null;
if(width>0&&height>0){
bound=newBoundingBox(0,0,width,height);
}
initAndShowGUI(scene,save,bound);
}
/**
* Overload function of sceneToImage
*
* @param scene
* @param save
*/
publicvoidsceneToImage(finalScene scene,finalFile save){
sceneToImage(scene,save,0,0);
}
/**
* This function helps to save the Node to Image
* and it's the only function which is public
*
* @param node node to be saved
* @param list list of children where the node is kept
* @param save place where the image to be saved
*/
publicvoidnodeToImage(finalNode node,finalObservableList list,finalFile save){
nodeToImage(node,list,save,0,0);
}
/**
* This function helps to save the Node to Image
* and it's the only function which is public
*
* @param node the node to be saved
* @param list the ObservableList of children where the node is kept
* @param save place where the image to be saved
* @param width width of image to be saved
* @param height height of image to be saveed
*/
publicvoidnodeToImage(finalNode node,finalObservableList list,finalFile save,finaldoublewidth,finaldoubleheight){
stage=(Stage)node.getScene().getWindow();
scene=node.getScene();
this.node=node;
this.list=list;
BoundingBox bound=null;
if(width>0&&height>0){
bound=newBoundingBox(0,0,width,height);
}
initAndShowGUI(node,save,bound);
}
/**
* This is the main function to generate the graphics of the Node
* using the FXPanel inside the JFrame.
* @param node
* @param f
* @param bound
*/
privatevoidinitAndShowGUI(finalNode node,Filef,BoundingBox bound){
Group root=newGroup();
Scene sc=newScene(root);
root.getChildren().add(node);
initAndShowGUI(sc,f,bound);
}
/**
* This is the main function to generate the graphics of the scene
* using the FXPanel inside the JFrame.
*
* @param sc
* @param f
* @param bound
*/
privatevoidinitAndShowGUI(finalScene sc,Filef,BoundingBox bound){
file=f;
if(bound==null)
boundbox=newBoundingBox(0,0,stage.getWidth(),stage.getHeight());
else
boundbox=bound;
frame=newJFrame();
//Frame.setUndecorated(true);
fxPanel=newJFXPanel();
fxPanel.setScene(sc);
fxPanel.addComponentListener(newComponentListener(){
publicvoidcomponentResized(ComponentEvente){
ActionListener ac=newActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
save(fxPanel,boundbox,file);
timer.stop();
fxPanel.removeAll();
restore();
frame.dispose();
}
};
//I've set timer for capturing image of the Node
timer=newTimer(TIME,ac);
timer.start();
}
publicvoidcomponentMoved(ComponentEvente){
}
publicvoidcomponentShown(ComponentEvente){
}
publicvoidcomponentHidden(ComponentEvente){
}
});
frame.add(fxPanel);
frame.setSize((int)boundbox.getWidth(),(int)boundbox.getHeight());
if(stage!=null){
frame.setLocation((int)stage.getX(),(int)stage.getY());
Platform.runLater(newRunnable(){
publicvoidrun(){
stage.hide();
}
});
}
frame.setVisible(true);
}
/**
* This function saves the container as FXPanel
* to the Image using the Java API
* @param container
* @param bounds
* @param file
*/
privatevoidsave(Container container,Bounds bounds,File file){
try{
Stringextension="";
Stringname=file.getName();
if(name.contains(".")){
intstart=name.lastIndexOf(".");
extension=file.getName().substring(start+1);
}
else{
extension="jpg";
}
ImageIO.write(toBufferedImage(container,bounds),extension,file);
System.out.println("Node To Image saved");
}catch(java.lang.Exception exception){
exception.printStackTrace();
JOptionPane.showMessageDialog(null,"The image couldn't be saved","Error",JOptionPane.ERROR_MESSAGE);
restore();
}
}
/**
* Restoring the scene or Node to it's original state
*/
privatevoidrestore(){
if(node!=null){
restoreNode();
}else{
restoreScene();
}
}
/**
* Restores the Node
*/
privatevoidrestoreNode(){
Platform.runLater(newRunnable(){
publicvoidrun(){
list.add(node);
stage.show();
}
});
}
/**
* This function restores the main Scene to the original Stage
* from where the event has been triggered
*/
privatevoidrestoreScene(){
Platform.runLater(newRunnable(){
publicvoidrun(){
stage.setScene(scene);
stage.show();
}
});
}
/**
* This function is used to get the BufferedImage of the
* Container as JFXPanel
* @param container
* @param bounds
* @return
*/
privateBufferedImage toBufferedImage(Container container,Bounds bounds){
BufferedImage bufferedImage=newBufferedImage(
(int)bounds.getWidth(),
(int)bounds.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics graphics=bufferedImage.getGraphics();
graphics.translate((int)-bounds.getMinX(),(int)-bounds.getMinY());// translating to upper-left corner
container.paint(graphics);
graphics.dispose();
returnbufferedImage;
}
}
12 thoughts on “Save Node or Scene to Image in JavaFX 2.0”
Hello! Cool works!
But is (will) any possibility to NOT use AWT and Swing?
As I can imagine, this layers bring additional requirements of time and memory?
Actually it’s just the hack for saving the Node or scene to Image format. The JavaFX team is working on this facility I think this facility will be available on coming version soon
Thanks.
Pingback: Java desktop links of the week, November 7 | Jonathan Giles
I’m impressed!!! Really informative blog post on blog.ngopal.com.np my friend. I just wanted to comment & say keep up the quality work.
It works excellently. Thank you!
You are welcome!! π
Thank you friend. You are an excellent professional and very honest too.
Very nice one..!! Liked it a lot..!! Thank u very much use full to my project as well. Keep it up…, keep doing.
good
Great work!!!!
nice work.
can we send it to visitor’s browser for download without saving disk?
Yes we can send to browser for download as well, but I think you should make your own server and port for listening to the visitor’s computer and send file there by using HTTP headers rules.