java - JavaFX capture screen using Robot -


i using transparent stage in javafx contains canvas.

the user can draw rectangle in canvas dragging mouse.

when hits enter saving image using:

try {      imageio.write(bufferedimage,extension,outputfile); } catch (ioexception e) {     e.printstacktrace();                         } 

to capture image screen using:

gc.clearrect(0, 0, getwidth(), getheight()); // [gc] graphicscontext2d of canvas          // start thread         new thread(() -> {             try {                  // sleep time                 thread.sleep(100);                  // capture image                 bufferedimage image;                 int[] rect = calculaterect();                 try {                     image = new robot().createscreencapture(new rectangle(rect[0], rect[1], rect[2], rect[3]));                 } catch (awtexception e) {                     e.printstacktrace();                     return;                 }              } catch (exception ex) {                 ex.printstacktrace();             }         }).start(); 

the problem:

i don't know when javafx thread clear canvas using thread can see above waiting 100 milliseconds , capture screen.but not working times,what mean canvas has not been cleared , image(cause canvas has not been cleared yet):

enter image description here

instead of this:

enter image description here

the question:

how know when javafx has cleared canvas can capture screen after that?

mixing swing javafx not recommended although that's way know capture screen java...

i hope there's better way achieve want this, canvas cleared next time "pulse" rendered. 1 approach start animationtimer , wait until second frame rendered:

gc.clearrect(0, 0, getwidth(), getheight()); animationtimer waitforframerender = new animationtimer() {     private int framecount = 0 ;     @override     public void handle(long timestamp) {         framecount++ ;         if (framecount >= 2) {             stop();             // screen capture...         }     } }; waitforframerender.start(); 

obviously, if doing screen capture in background thread, need make sure don't draw onto canvas before capture occurs...


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -