/* * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. */ package controls; import com.sun.javafx.perf.PerformanceTracker; import java.text.DecimalFormat; import java.text.NumberFormat; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.CheckBox; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.stage.Stage; import javafx.stage.WindowEvent; import javafx.util.Duration; import javafx.builders.TimelineBuilder; import javafx.builders.StageBuilder; import java.util.*; /** * Checkbox test. * * Measure FPS rate of changing the Checkbox UI component. */ public class CheckboxTest extends Application implements EventHandler , Runnable { private static final int C_WIDTH = 80; private static final int C_HEIGHT = 20; private static double viewWidth = 800.0; private static double viewHeight = 600.0; // parameters below can be configured via command line, see output of printHelp() method for details. private static int warmupTime = 10; // warm-up phase duration private static int runTime = 20; // measurement phase duration private static boolean debugStatus = false; private static boolean registerListener = false; private static int toggleStep = 15; private final List checkboxes = new ArrayList(1000); private Stage stage; private Timeline fpsTimeline; protected PerformanceTracker tracker; private boolean isActive = false; private int idx = 0; private int listenersCalls = 0; ///////////////////////////////////////////////////////////////// public void stopTest() { if(isActive) { isActive = false; tracker.setOnPulse(null); if (fpsTimeline != null) { fpsTimeline.stop(); } PerformanceTracker.releaseSceneTracker(stage.getScene()); stage = null; } } public void parseArgs(String[] args) { parseCommandLine(args); } public Stage createTestGUI() { final Group root = new Group(); final NumberFormat twoDp = new DecimalFormat("000"); int i = 0; for (double x = 0; x <= (viewWidth - C_WIDTH); x += C_WIDTH) { for (double y = 0; y <= (viewHeight - C_HEIGHT); y += C_HEIGHT, i++) { final CheckBox cb = new CheckBox(" " + twoDp.format(i)); cb.setLayoutX(x); cb.setLayoutY(y); cb.setSelected(true); if (registerListener) { cb.selectedProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observableValue, Boolean aBoolean, Boolean aBoolean1) { ++listenersCalls; } }); } root.getChildren().add(cb); checkboxes.add(cb); } } final Scene scene = new Scene(root, viewWidth, viewHeight); stage = StageBuilder.create() .title("Checkbox") // workaround: terminate active test when window is closed .onHidden(new EventHandler() { @Override public void handle(WindowEvent event) { if (isActive) { stopTest(); System.out.println("Application window has been suddenly closed. Program terminated."); System.exit(-1); } } }) .scene(scene) .build(); stage.sizeToScene(); tracker = PerformanceTracker.getSceneTracker(stage.getScene()); if(debugStatus) { // init measurement timeline if(fpsTimeline == null) { fpsTimeline = TimelineBuilder.create() .cycleCount(Timeline.INDEFINITE) .keyFrames(new KeyFrame(Duration.valueOf(1000), new EventHandler() { @Override public void handle(ActionEvent e) { debug(" instant FPS: " + tracker.getInstantFPS() + ", average FPS: " + tracker.getAverageFPS()); debug(" instant pulses: " + tracker.getInstantPulses() + ", average pulses: " + tracker.getAveragePulses()); if (registerListener) { debug(" " + listenersCalls + " listeners calls"); } } }) ).build(); } fpsTimeline.playFromStart(); } return stage; } private void toggleCheckboxes() { if (idx == checkboxes.size()) { idx = 0; } final int max = Math.min(checkboxes.size(), idx + toggleStep); for (; idx < max; ++idx) { final CheckBox cb = checkboxes.get(idx); cb.setSelected(!cb.isSelected()); } } /* * Setup and run the Timeline for standalone mode */ private void doAutoTimeline() { // prepare timeline for standalone notifications TimelineBuilder.create().keyFrames( new KeyFrame(Duration.valueOf(0), new EventHandler() { @Override public void handle(ActionEvent e) { debug("Warm-up duration: " + warmupTime + " sec"); debug("Measurement duration: " + runTime + " sec"); System.out.println("Starting warmup for " + warmupTime + " sec..."); } }), new KeyFrame(new Duration(warmupTime * 1000), new EventHandler() { @Override public void handle(ActionEvent e) { debug("Resetting average fps counter"); tracker.resetAverageFPS(); tracker.resetAveragePulses(); System.out.println("Measurement phase for " + runTime + " sec... "); } }), new KeyFrame(new Duration((runTime + warmupTime) * 1000), new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("\n Score: " + tracker.getAverageFPS() + " Average FPS over " + runTime + "s "); System.out.println("\tinstant pulses: " + tracker.getInstantPulses() + ", average pulses: " + tracker.getAveragePulses()); System.exit(0); } }) ).build().playFromStart(); } public void runTest(){ idx = 0; for(CheckBox cb : checkboxes) { cb.setSelected(false); } tracker.setOnPulse(this); isActive = true; } @Override public void run() { if(isActive && stage != null) { toggleCheckboxes(); } } ///////////////////////////////////////////////////////////////// /** * Debug output. * @param msg message for output */ static void debug(String msg) { if (debugStatus) { System.out.println("DEBUG: " + msg); } } @Override public void handle(ActionEvent e) { Platform.runLater( this ); } private static void parseCommandLine(String[] args) { for(int i=0;i : width&height of visible area (default: " + viewWidth + "x" + viewHeight + ")\n" +"\t-warmup : warm-up time, seconds (default: "+warmupTime+" sec)\n" +"\t-duration : measurement time, seconds (default: "+runTime+" sec)\n" +"\t-toggleStep : amount of toggled checkboxes per single event, >=1, (default "+toggleStep+"\n" +"\t-register-listener : register listeners on checkboxes (default: " + registerListener + "\n" +"\t-debug true : enable debug messages (default " + debugStatus + ")\n" +"\n" +"\tExample:\n" +"\t\tjava -jar CheckboxTest.jar -debug true"); System.exit(0); } /* * Application class for running standalone. */ @Override public void start(Stage primaryStage) { final Stage stage = createTestGUI(); // Escape key will terminate application. stage.getScene().setOnKeyReleased(new EventHandler() { @Override public void handle(KeyEvent t) { if (t.getCode() == KeyCode.ESCAPE) { System.out.println("Escape key pressed. Application terminated."); System.exit(0); } } }); stage.setVisible(true); doAutoTimeline(); runTest(); } /** * Standalone entry point. * @param args Commandline arguments */ public static void main(String[] args) { parseCommandLine(args); Application.launch(CheckboxTest.class, args); } }