This discussion is archived
1 Reply Latest reply: Dec 19, 2012 10:55 PM by 980420 RSS

"Offscreen" JFXPanel could not be released

980420 Newbie
Currently Being Moderated
Hi,

I am working on a project that need to snapshot the content loaded in offscreen webviewer. Actually, we need to load a html and execute a piece of javascript code before taking the snapshot. So I made a POC based on JAVAFX 2.2.

Below is the sample code:

-----
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.SwingUtilities;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.SnapshotResult;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.util.Callback;



public class TestOffscreenSnapshot {

     private Scene scene_;
     private WebView webViewer_;
     private WebEngine webEngine_;
     /**
      * @param args
      */
     public static void main(String[] args) {
          
          final TestOffscreenSnapshot test = new TestOffscreenSnapshot();
          
          test.test();
          
          return;
          
     }
     
     
     public  void test()
     {
          SwingUtilities.invokeLater(new Runnable() {
                 public void run() {               
                         JFXPanel panel = new JFXPanel();
                          InitFX(panel);
                          try {
                                   Thread.sleep(4000);
                               } catch (InterruptedException e) {
                                   // TODO Auto-generated catch block
                                   e.printStackTrace();
                               }
                          Load();                          
                          try {
                                   Thread.sleep(4000);
                               } catch (InterruptedException e) {
                                   // TODO Auto-generated catch block
                                   e.printStackTrace();
                          }
                          
                          createImage();
                          
                          try {
                                   Thread.sleep(4000);
                               } catch (InterruptedException e) {
                                   // TODO Auto-generated catch block
                                   e.printStackTrace();
                          }
                          //System.exit(0);
                          
                 }
          });
          
     }
     
     private void InitFX(final JFXPanel panel)
     {
          Platform.runLater(new Runnable() {
                public void run() {
                 webViewer_ = new WebView();
                 webEngine_ = webViewer_.getEngine();
                 webViewer_.setPrefSize(1800, 600);
                 scene_ = new Scene(webViewer_);
                 panel.setScene(scene_);
                 
            }
       });
     }
     
     private void Load()
     {
          Platform.runLater(new Runnable() {
                public void run() {
                 webEngine_.load("www.oracle.com");
            }
          });
//          // load the web page
          
     }
     
     private void createImage()
     {
          Platform.runLater(new Runnable() {
                public void run() {
                 final SnapshotParameters params = new SnapshotParameters();
                params.setFill(Color.ALICEBLUE);
                webViewer_.snapshot(
                  new Callback<SnapshotResult, Void>() {
                    public Void call(SnapshotResult result) {
                      BufferedImage image = SwingFXUtils.fromFXImage(result.getImage(), null);
                      try {
                           String WORKING_DIR = System.getProperty("user.dir");
                           String path = new File(WORKING_DIR, "test.png").getPath();
                           System.out.println(path);
                          ImageIO.write(image, "png", new File(path));
                        }
                      catch (IOException ex) {
                        }
                       
                      return null;
                    }
                  },
                  params, 
                  null
                );
            }
          });
     }

}
-----

If run this test, the snapshot could be successfully generated. However, the application could not exit(you could see in Eclipse,a javaw.exe process still there). I find that it is because a JFXPanel is created(you could just create a JFXPanel in main and run the sample). But if I do not create the JFXPanel and relate it with the scene, the snapshot could be generated.

And I do a test. If I add it to a JFrame, the application will exit after close. I look into the source code of AWT and find it call System.exit(0) to close the current java runtime. This is awful for me since the project is a component that will be used by others. If calling System.exit(), the client will close as well. It does not make sense.

So is there any suggestion to keep the offscreen snapshot work in my component but auto release the "JFXPanel" when it is not used?

Edited by: 977417 on Dec 17, 2012 8:19 PM

Edited by: 977417 on Dec 17, 2012 8:19 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points