Advertisement
Guest User

Untitled

a guest
May 7th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.05 KB | None | 0 0
  1. import com.sun.javafx.perf.PerformanceTracker;
  2. import com.sun.javafx.tk.Toolkit;
  3. import com.sun.scenario.animation.shared.TimerReceiver;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import javafx.animation.KeyFrame;
  7. import javafx.animation.KeyValue;
  8. import javafx.animation.Timeline;
  9. import javafx.animation.Transition;
  10. import javafx.animation.TranslateTransition;
  11. import javafx.application.Application;
  12. import javafx.scene.Group;
  13. import javafx.scene.Scene;
  14. import javafx.scene.paint.Color;
  15. import javafx.scene.shape.Circle;
  16. import javafx.scene.shape.Rectangle;
  17. import javafx.stage.Stage;
  18. import javafx.util.Duration;
  19.  
  20. /**
  21.  *
  22.  * @author jmart
  23.  */
  24. public class JavaFXApplication1 extends Application {
  25.  
  26.     int height = 1000;
  27.     int width = 1900;
  28.     Scene scene;
  29.     Group root = new Group();
  30.     Group points = new Group();
  31.     TestTimer ta;
  32.  
  33.     @Override
  34.     public void start(Stage primaryStage) {
  35.         scene = new Scene(root, width, height);
  36.         primaryStage.setTitle("animation tester");
  37.         primaryStage.setScene(scene);
  38.         primaryStage.show();
  39.         //        
  40.         ta = new TestTimer(scene);
  41.         ta.setAutoReverse(true);
  42.         ta.setCycleCount(Timeline.INDEFINITE);
  43.         ta.setDuration(100);
  44.         ta.playFromStart();
  45.         Toolkit.getToolkit().getMasterTimer().addAnimationTimer(ta);
  46.         //
  47.         pathJitterBug();
  48.     }
  49.  
  50.     /**
  51.      * @param args the command line arguments
  52.      */
  53.     public static void main(String[] args) {
  54.         launch(args);
  55.     }
  56.  
  57.     private void pathJitterBug() {
  58.         Circle circle2 = new Circle(20, Color.BLUE);
  59.         Timeline tl = new Timeline(
  60.                 new KeyFrame(Duration.ZERO, new KeyValue(circle2.centerXProperty(), 125), new KeyValue(circle2.centerYProperty(), 125)),
  61.                 new KeyFrame(Duration.seconds(5), new KeyValue(circle2.centerXProperty(), 425), new KeyValue(circle2.centerYProperty(), 425))
  62.         );
  63.         tl.setCycleCount(Timeline.INDEFINITE);
  64.         tl.setAutoReverse(true);
  65.         tl.playFromStart();
  66.         //
  67.         Circle circle3 = new Circle(20, Color.GREEN);
  68.         TranslateTransition animation = new TranslateTransition(Duration.seconds(5), circle3);
  69.         animation.setFromX(425);
  70.         animation.setFromY(125);
  71.         animation.setToX(125);
  72.         animation.setToY(425);
  73.         animation.setAutoReverse(true);
  74.         animation.setCycleCount(Timeline.INDEFINITE);
  75.         animation.playFromStart();
  76.         //
  77.         Rectangle bg = new Rectangle(width, height);
  78.         bg.setOnMousePressed(me -> drawDots());
  79.         //
  80.         root.getChildren().addAll(bg, points, circle2, circle3);
  81.  
  82.     }
  83.  
  84.     void drawDots() {
  85.         System.out.println("making graph");
  86.         List<Float> fps = ta.getFps();
  87.         List<Long> timerData = ta.getTimerData();
  88.         int x = 1;
  89.         points.getChildren().clear();
  90.         for (Float d : fps) {
  91.             Circle c = new Circle(1, Color.LIGHTGREEN);
  92.             points.getChildren().add(c);
  93.             c.setTranslateX(x++);
  94.             c.setTranslateY(height - d);
  95.         }
  96.         x = 1;
  97.         for (long d : timerData) {
  98.             Circle c = new Circle(1, Color.RED);
  99.             points.getChildren().add(c);
  100.             c.setTranslateX(x++);
  101.             d = d / 10000;
  102.             if (d > height) {
  103.                 System.out.println("off the charts, d = " + d);
  104.                 d = height;
  105.                 c.setRadius(4);
  106.             }
  107.             c.setTranslateY(height - d);
  108.         }
  109.         timerData.clear();
  110.         fps.clear();
  111.     }
  112.  
  113.     public class TestTimer extends Transition implements TimerReceiver {
  114.  
  115.         List<Double> transitionFrac = new ArrayList<>();
  116.         List<Long> timerData = new ArrayList<>();
  117.         List<Float> fps = new ArrayList<>();
  118.         volatile boolean stopCollecting = false;
  119.         double prevFrac = 0;
  120.         final Scene scene;
  121.         PerformanceTracker perfTracker;
  122.  
  123.         public TestTimer(Scene scene) {
  124.             this.scene = scene;
  125.             perfTracker = PerformanceTracker.getSceneTracker(scene);
  126.         }
  127.  
  128.         public void setDuration(long d) {
  129.             this.setCycleDuration(Duration.seconds(d));
  130.         }
  131.  
  132.         public List<Long> getTimerData() {
  133.             return timerData;
  134.         }
  135.  
  136.         public List<Float> getFps() {
  137.             return fps;
  138.         }
  139.  
  140.         public List<Double> getTransitionFrac() {
  141.             return transitionFrac;
  142.         }
  143.  
  144.         @Override
  145.         protected void interpolate(double frac) {
  146.             if (!stopCollecting) {
  147.                 transitionFrac.add(frac - prevFrac);
  148.             }
  149.             prevFrac = frac;
  150.         }
  151.  
  152.         long prevTime = 0;
  153.  
  154.         @Override
  155.         public void handle(long now) {
  156.             if (!stopCollecting) {
  157.                 timerData.add(now - prevTime);
  158.                 fps.add(perfTracker.getInstantFPS());
  159.             }
  160.             prevTime = now;
  161.             if (fps.size() >= width) {
  162.                 drawDots();
  163.             }
  164.         }
  165.  
  166.     }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement