/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ensemble.autotest; import java.lang.reflect.Method; import javafx.animation.AnimationTimer; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Stage; import javafx.util.Duration; /** * * @author epavlova */ public class Ensemble8AutoTest extends Application { static { System.setProperty("java.net.useSystemProxies", "true"); System.setProperty("http.proxyHost", "www-proxy.us.oracle.com"); System.setProperty("http.proxyPort", "80"); } Stage stage; Pane root; Class sampleClass; Object sampleInstance; int curSampleIdx; String[] samples = { "ensemble.samples.graphics.brickbreaker.BrickBreakerApp" }; public void createScene() { root = new Pane(); root.setPrefSize(800, 600); } public void buildSample(String appClassName) { try { sampleClass = Class.forName(appClassName); sampleInstance = sampleClass.newInstance(); Parent sampleRoot = (Parent) sampleClass.getMethod("createContent").invoke(sampleInstance); root.getChildren().add(sampleRoot); } catch (Exception ex) { ex.printStackTrace(); } } public void startSample() { try { Method m = sampleClass.getMethod("play"); if (m != null) m.invoke(sampleInstance); } catch (NoSuchMethodException ex) { // skip } catch (Exception ex) { ex.printStackTrace(); } } public void stoptSample() { try { Method m = sampleClass.getMethod("stop"); if (m != null) m.invoke(sampleInstance); } catch (NoSuchMethodException ex) { // skip } catch (Exception ex) { ex.printStackTrace(); } } int prevSampleIdx = 0; int iters; public void play() { curSampleIdx = 0; System.out.println("Iteration: " + iters); Timeline mainTimeline = new Timeline(); mainTimeline.setCycleCount(Timeline.INDEFINITE); KeyFrame mainFrame = new KeyFrame(Duration.millis(2000), new EventHandler() { @Override public void handle(ActionEvent e) { if (sampleInstance != null) { System.out.println("Stop and remove " + samples[prevSampleIdx]); stoptSample(); } System.out.println("\ncurSample: " + curSampleIdx); System.out.println("Create and start " + samples[curSampleIdx]); String sample = samples[curSampleIdx]; root.getChildren().clear(); buildSample(sample); startSample(); prevSampleIdx = curSampleIdx; curSampleIdx = ((curSampleIdx +1) % samples.length); if (curSampleIdx == 0) { System.out.println("Iteration: " + iters); iters ++; } } }); mainTimeline.getKeyFrames().add(mainFrame); mainTimeline.play(); } @Override public void start(Stage primaryStage) throws Exception { stage = primaryStage; createScene(); stage.setResizable(false); stage.setScene(new Scene(root, 800, 600)); stage.show(); play(); } /** * Java main for when running without JavaFX launcher */ public static void main(String[] args) { launch(args); } }