1

Topic: Stream on canvas in application JavaFX

Simple application. Frame JavaFX in work space Canvas and Button. Application is started, there is a frame, the flow in which in a cycle with a time delay 10  is launched. On canvas  the text. Sooner or later the text rises. Sometimes  1000 times, sometimes 100 and sometimes 300000 times. Who knows what is the matter? the Program small... More low source codes. File Main.java package drawtext; import java.util.concurrent. ExecutorService; import java.util.concurrent. Executors; import javafx.application. Application; import javafx.event. ActionEvent; import javafx.event. EventHandler; import javafx.geometry. Pos; import javafx.stage. Stage; import javafx.scene. Scene; import javafx.scene.canvas. Canvas; import javafx.scene.canvas. GraphicsContext; import javafx.scene.control. Button; import javafx.scene.layout. StackPane; public class Main extends Application {static int isShutdown = 0; static int isStopDraw = 0; final Canvas canvas = new Canvas (400, 400); final GraphicsContext gc = canvas.getGraphicsContext2D (); Button btn = new Button (); Stage primaryStageMy; StackPane root = new StackPane (); Scene scene = new Scene (root, 400,400); @Override public void start (Stage primaryStage) {try {btn.setText ("isStopDraw" + isStopDraw); btn.setOnAction (new EventHandler <ActionEvent> () {@Override public void handle (ActionEvent event) {if (0 == isStopDraw) {isStopDraw=1;} else {isStopDraw=0;} btn.setText ("isStopDraw" + isStopDraw);}}); root.getChildren ().add (canvas); root.getChildren ().add (btn); StackPane.setAlignment (canvas, Pos. CENTER); primaryStage.setScene (scene); primaryStage.show (); ExecutorService exec = Executors.newSingleThreadExecutor (); exec.execute (new Draw (this)); exec.shutdown ();} catch (Exception e) {e.printStackTrace ();} } public static void main (String [] args) {launch (args); isShutdown = 1;}} file Draw.java package drawtext; import javafx.geometry. Bounds; import javafx.scene.canvas. GraphicsContext; import javafx.scene.paint. Color; import javafx.scene.text. Font; public class Draw implements Runnable {private Main main; private GraphicsContext gc; public Draw (Main main) {this.main = main; this.gc = main.gc;} public void run () {Bounds ViewRect; synchronized (gc) {gc.setFont (new Font ("Times New Roman", 20)); int i = 0; while (true) {try {Thread.sleep (10);} catch (InterruptedException ignory) {System.out.println ("the Emergency exit 1!" (I);//return; continue;} if (1 == main.isShutdown) {break;} if (1 == main.isStopDraw) {System.out.println ("the Pushed button of Feet!" + i); Thread.yield (); continue;} try {ViewRect=main.canvas.getBoundsInParent (); gc.setFill (Color. WHITE); gc.fillRect (ViewRect.getMinX (), ViewRect.getMinY (), ViewRect.getWidth (), ViewRect.getHeight ()); gc.setFill (Color. BLACK); gc.clearRect (0, 0, 400, 400); gc.fillText ("Test record on a canvas. No" + i, 20, 50);} catch (Exception ignory) {System.out.println ("the Emergency exit 2!" + i);//return; continue;} System.out.println ("the Test output to the console No" + i); i ++;}}}}

2

Re: Stream on canvas in application JavaFX

At first, at you is not present ** intercontinuous synchronization. Secondly, I with JavaFh did not work, but in the Swing and generally in the majority  if not in everything, to update  it is necessary from specially intended flow, instead of from the.

3

Re: Stream on canvas in application JavaFX

Hello, Kristy, you wrote: K> Simple application. Frame JavaFX in work space Canvas and Button. K> application is started, there is a frame, the flow in which in cycle K> with a time delay 10  is launched. On canvas  the text. Sooner or later the text rises. K> sometimes  1000 times, sometimes 100 and sometimes 300000 times. Who knows what is the matter? K> the Program small... More low source codes. Get function: public static void gui_call (Runnable code) throws Exception {if (Platform.isFxApplicationThread ()) {code.run (); return;} int timeout=100; Runnable r=new Runnable () {public synchronized void run () {code.run (); notify ();}}; synchronized (r) {Platform.runLater (r); r.wait (timeout);} } And then cause... final int ii=i; gui_call (new Runnable () {public void run () {Bounds ViewRect=main.canvas.getBoundsInParent (); gc.setFill (Color. WHITE); gc.fillRect (ViewRect.getMinX (), ViewRect.getMinY (), ViewRect.getWidth (), ViewRect.getHeight ()); gc.setFill (Color. BLACK); gc.clearRect (0, 0, 400, 400); gc.fillText ("Test record on a canvas. No" + ii, 20, 50);}});... So will not rise