1 /*
   2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package hello;
  27 
  28 import javafx.animation.KeyFrame;
  29 import javafx.animation.KeyValue;
  30 import javafx.animation.Timeline;
  31 import javafx.application.Application;
  32 import javafx.scene.Group;
  33 import javafx.scene.Scene;
  34 import javafx.scene.paint.Color;
  35 import javafx.scene.shape.Rectangle;
  36 import javafx.scene.text.Font;
  37 import javafx.scene.text.Text;
  38 import javafx.stage.Stage;
  39 import javafx.util.Duration;
  40 
  41 import com.sun.javafx.perf.PerformanceTracker;
  42 
  43 // FIXME: Needs public performance tracker API else move to tests/manual
  44 public class HelloFPS extends Application {
  45 
  46     private static final Color colors[] = {
  47         Color.RED,
  48         Color.ORANGE,
  49         Color.YELLOW,
  50         Color.GREEN,
  51         Color.BLUE,
  52         Color.INDIGO,
  53         Color.VIOLET
  54     };
  55 
  56     private int curColorIdx = 0;
  57 
  58     @Override public void start(Stage stage) {
  59         stage.setTitle("Hello FPS");
  60         final Scene scene = new Scene(new Group(), 600, 450);
  61         scene.setFill(Color.color(0.8, 0.8, 0.7));
  62 
  63         final Rectangle rect = new Rectangle(5, 5, Color.GRAY);
  64         rect.setLayoutX(5);
  65         rect.setLayoutY(5);
  66 
  67         final Text text = new Text("??? fps");
  68         text.setFont(new Font(30));
  69         text.setFill(colors[curColorIdx]);
  70         text.setLayoutX(5);
  71         text.setLayoutY(45);
  72 
  73         ((Group)scene.getRoot()).getChildren().addAll(rect, text);
  74         stage.setScene(scene);
  75         stage.show();
  76 
  77         final Timeline timeline = new Timeline();
  78         timeline.setCycleCount(Timeline.INDEFINITE);
  79         timeline.setAutoReverse(true);
  80         final KeyValue kv = new KeyValue (rect.layoutXProperty(), 25);
  81         final KeyFrame kf = new KeyFrame(Duration.millis(5000), kv);
  82         timeline.getKeyFrames().add(kf);
  83         timeline.play();
  84 
  85         final PerformanceTracker tracker = PerformanceTracker.getSceneTracker(scene);
  86 
  87         final Timeline tlTracker = new Timeline();
  88         tlTracker.setCycleCount(Timeline.INDEFINITE);
  89         final KeyFrame kfTracker = new KeyFrame(
  90                 Duration.millis(500),
  91                 event -> {
  92                     int fps = (int) Math.round(tracker.getInstantFPS());
  93                     text.setText("" + fps + " fps");
  94                     curColorIdx = (curColorIdx + 1) % colors.length;
  95                     text.setFill(colors[curColorIdx]);
  96                 });
  97         tlTracker.getKeyFrames().add(kfTracker);
  98         tlTracker.play();
  99     }
 100 
 101     /**
 102      * @param args the command line arguments
 103      */
 104     public static void main(String[] args) {
 105         Application.launch(args);
 106     }
 107 }