Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.javafx.perf.PerformanceTracker;
- import com.sun.javafx.tk.Toolkit;
- import com.sun.scenario.animation.shared.TimerReceiver;
- import java.util.ArrayList;
- import java.util.List;
- import javafx.animation.KeyFrame;
- import javafx.animation.KeyValue;
- import javafx.animation.Timeline;
- import javafx.animation.Transition;
- import javafx.animation.TranslateTransition;
- import javafx.application.Application;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Rectangle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- /**
- *
- * @author jmart
- */
- public class JavaFXApplication1 extends Application {
- int height = 1000;
- int width = 1900;
- Scene scene;
- Group root = new Group();
- Group points = new Group();
- TestTimer ta;
- @Override
- public void start(Stage primaryStage) {
- scene = new Scene(root, width, height);
- primaryStage.setTitle("animation tester");
- primaryStage.setScene(scene);
- primaryStage.show();
- //
- ta = new TestTimer(scene);
- ta.setAutoReverse(true);
- ta.setCycleCount(Timeline.INDEFINITE);
- ta.setDuration(100);
- ta.playFromStart();
- Toolkit.getToolkit().getMasterTimer().addAnimationTimer(ta);
- //
- pathJitterBug();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- private void pathJitterBug() {
- Circle circle2 = new Circle(20, Color.BLUE);
- Timeline tl = new Timeline(
- new KeyFrame(Duration.ZERO, new KeyValue(circle2.centerXProperty(), 125), new KeyValue(circle2.centerYProperty(), 125)),
- new KeyFrame(Duration.seconds(5), new KeyValue(circle2.centerXProperty(), 425), new KeyValue(circle2.centerYProperty(), 425))
- );
- tl.setCycleCount(Timeline.INDEFINITE);
- tl.setAutoReverse(true);
- tl.playFromStart();
- //
- Circle circle3 = new Circle(20, Color.GREEN);
- TranslateTransition animation = new TranslateTransition(Duration.seconds(5), circle3);
- animation.setFromX(425);
- animation.setFromY(125);
- animation.setToX(125);
- animation.setToY(425);
- animation.setAutoReverse(true);
- animation.setCycleCount(Timeline.INDEFINITE);
- animation.playFromStart();
- //
- Rectangle bg = new Rectangle(width, height);
- bg.setOnMousePressed(me -> drawDots());
- //
- root.getChildren().addAll(bg, points, circle2, circle3);
- }
- void drawDots() {
- System.out.println("making graph");
- List<Float> fps = ta.getFps();
- List<Long> timerData = ta.getTimerData();
- int x = 1;
- points.getChildren().clear();
- for (Float d : fps) {
- Circle c = new Circle(1, Color.LIGHTGREEN);
- points.getChildren().add(c);
- c.setTranslateX(x++);
- c.setTranslateY(height - d);
- }
- x = 1;
- for (long d : timerData) {
- Circle c = new Circle(1, Color.RED);
- points.getChildren().add(c);
- c.setTranslateX(x++);
- d = d / 10000;
- if (d > height) {
- System.out.println("off the charts, d = " + d);
- d = height;
- c.setRadius(4);
- }
- c.setTranslateY(height - d);
- }
- timerData.clear();
- fps.clear();
- }
- public class TestTimer extends Transition implements TimerReceiver {
- List<Double> transitionFrac = new ArrayList<>();
- List<Long> timerData = new ArrayList<>();
- List<Float> fps = new ArrayList<>();
- volatile boolean stopCollecting = false;
- double prevFrac = 0;
- final Scene scene;
- PerformanceTracker perfTracker;
- public TestTimer(Scene scene) {
- this.scene = scene;
- perfTracker = PerformanceTracker.getSceneTracker(scene);
- }
- public void setDuration(long d) {
- this.setCycleDuration(Duration.seconds(d));
- }
- public List<Long> getTimerData() {
- return timerData;
- }
- public List<Float> getFps() {
- return fps;
- }
- public List<Double> getTransitionFrac() {
- return transitionFrac;
- }
- @Override
- protected void interpolate(double frac) {
- if (!stopCollecting) {
- transitionFrac.add(frac - prevFrac);
- }
- prevFrac = frac;
- }
- long prevTime = 0;
- @Override
- public void handle(long now) {
- if (!stopCollecting) {
- timerData.add(now - prevTime);
- fps.add(perfTracker.getInstantFPS());
- }
- prevTime = now;
- if (fps.size() >= width) {
- drawDots();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement