From b3902d3fde2d318db728e94b69c526b8e57fc30c Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Wed, 19 Aug 2015 16:00:57 -0400 Subject: [PATCH] FX - main drawing loop --- core/src/processing/javafx/PSurfaceFX.java | 60 ++++++++++++---------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/core/src/processing/javafx/PSurfaceFX.java b/core/src/processing/javafx/PSurfaceFX.java index 7e5e1c6b9..da5a68693 100644 --- a/core/src/processing/javafx/PSurfaceFX.java +++ b/core/src/processing/javafx/PSurfaceFX.java @@ -33,7 +33,9 @@ package processing.javafx; import java.util.HashMap; import java.util.Map; -import javafx.animation.AnimationTimer; +import javafx.animation.Animation; +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; import javafx.application.Application; import javafx.application.Platform; import javafx.beans.value.ChangeListener; @@ -50,6 +52,7 @@ import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.stage.WindowEvent; +import javafx.util.Duration; import processing.core.*; @@ -60,13 +63,31 @@ public class PSurfaceFX implements PSurface { Stage stage; Canvas canvas; - AnimationTimer timer; - + final Animation animation; public PSurfaceFX(PGraphicsFX2D graphics) { fx = graphics; canvas = new ResizableCanvas(); fx.context = canvas.getGraphicsContext2D(); + + { // set up main drawing loop + KeyFrame keyFrame = new KeyFrame(Duration.millis(1000), e -> { + sketch.handleDraw(); + if (sketch.exitCalled()) { + //sketch.exitActual(); // just calls System.exit() + Platform.exit(); // version for safe JavaFX shutdown + } + }); + animation = new Timeline(keyFrame); + animation.setCycleCount(Timeline.INDEFINITE); + + // key frame has duration of 1 second, so the rate of the animation + // should be set to frames per second + + // setting rate to negative so that event fires at the start of + // the key frame and first frame is drawn immediately + animation.setRate(-60); + } } @@ -437,8 +458,9 @@ public class PSurfaceFX implements PSurface { public void setFrameRate(float fps) { - // TODO Auto-generated method stub - + // setting rate to negative so that event fires at the start of + // the key frame and first frame is drawn immediately + if (fps > 0) animation.setRate(-fps); } @@ -473,44 +495,28 @@ public class PSurfaceFX implements PSurface { public void startThread() { - if (timer == null) { - timer = new AnimationTimer() { - - @Override - public void handle(long now) { - //System.out.println("handle(" + now + ") calling handleDraw()"); - sketch.handleDraw(); - - if (sketch.exitCalled()) { - //sketch.exitActual(); // just calls System.exit() - Platform.exit(); // version for safe JavaFX shutdown - } - } - }; - timer.start(); - } + animation.play(); } public void pauseThread() { - // TODO Auto-generated method stub + animation.pause(); } public void resumeThread() { - // TODO Auto-generated method stub + animation.play(); } public boolean stopThread() { - // TODO Auto-generated method stub - return false; + animation.stop(); + return true; } public boolean isStopped() { - // TODO Auto-generated method stub - return false; + return animation.getStatus() == Animation.Status.STOPPED; }