/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. */ package helloworld; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.beans.value.WritableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Point3D; import javafx.scene.Camera; import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.SubScene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.effect.BoxBlur; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.paint.CycleMethod; import javafx.scene.paint.LinearGradient; import javafx.scene.paint.Paint; import javafx.scene.paint.Stop; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.util.Duration; public class HelloSubScene extends Application { private static final boolean addTextField = false; private static final boolean addEffects = false; public static Timeline startAnimation(WritableValue target, T endValue, float millis) { Timeline timeline = new Timeline(); timeline.setCycleCount(Timeline.INDEFINITE); timeline.setAutoReverse(true); final KeyValue kv = new KeyValue(target, endValue); final KeyFrame kf = new KeyFrame(Duration.millis(millis), kv); timeline.getKeyFrames().add(kf); return timeline; } public static Paint getLinearGradPaint() { final Stop[] stops = new Stop[]{new Stop(0.0F, Color.TRANSPARENT), new Stop(0.5F, Color.LIGHTBLUE), new Stop(0.9F, Color.TRANSPARENT)}; return new LinearGradient(0.0f, 0.0f, 1.0f, 1.0f, true, CycleMethod.NO_CYCLE, stops); } private static SubScene createSubScene(String title, Paint fillPaint, Camera camera) { Group root = new Group(); root.getChildren().add(someNodes(title)); SubScene subScene = new SubScene(root, 150, 150); subScene.setFill(fillPaint); subScene.setCamera(camera); return subScene; } private static Parent someNodes(String str) { VBox vbox = new VBox(); final Text text = new Text(str); text.setFont(Font.font("Times New Roman", 16)); if (addEffects) { text.setEffect(new BoxBlur()); } Button button = new Button("Button"); button.setTranslateZ(50); button.setOnAction(new EventHandler() { @Override public void handle(ActionEvent t) { System.out.println("Button action from " + text.getText()); } }); vbox.getChildren().add(text); vbox.getChildren().add(button); if (addTextField) { TextField field = new TextField(str + " Textfield"); field.setFocusTraversable(true); field.focusedProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue ov, Object t, Object t1) { System.out.println("TextField focus " + t1); } }); vbox.getChildren().add(field); } return vbox; } @Override public void start(Stage stage) { stage.setTitle("HelloSubScene"); Group root = new Group(); HBox hbox = new HBox(); hbox.setTranslateY(50); root.getChildren().add(hbox); Scene scene = new Scene(root); SubScene plainSubScene = createSubScene("Plain SubScene", Color.GOLD, null); SubScene subSubScene = createSubScene("Embedded SubScene", getLinearGradPaint(), null); subSubScene.setHeight(subSubScene.getHeight()/2); subSubScene.setTranslateY(150/2); ((Group)plainSubScene.getRoot()).getChildren().add(subSubScene); hbox.getChildren().add(plainSubScene); SubScene animatedSubScene = createSubScene("SubScene with Gradient\nand\nPerspective Camera", getLinearGradPaint(), new PerspectiveCamera()); hbox.getChildren().add(animatedSubScene); startAnimation(animatedSubScene.rotateProperty(), 360.0, 40000.0f).play(); animatedSubScene.setRotationAxis(new Point3D(1.0, 1.0, 0.0)); SubScene overlaySubScene = createSubScene("Overlay Sub Scene", null, null); overlaySubScene.setWidth(plainSubScene.getWidth() + animatedSubScene.getWidth()); root.getChildren().add(overlaySubScene); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }