package com.almasb.ios;
import javafx.animation.FillTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class IOSApp extends Application {
private Parent createContent() {
Pane root = new Pane();
root.setPrefSize(300, 300);
Rectangle bg = new Rectangle(300, 300);
ToggleSwitch toggle = new ToggleSwitch();
toggle.setTranslateX(100);
toggle.setTranslateY(100);
Text text = new Text();
text.setFont(Font.font(18));
text.setFill(Color.WHITE);
text.setTranslateX(100);
text.setTranslateY(200);
text.textProperty().bind(Bindings.when(toggle.switchedOnProperty()).then("ON").otherwise("OFF"));
root.getChildren().addAll(toggle, text);
return root;
}
private static class ToggleSwitch extends Parent {
private BooleanProperty switchedOn = new SimpleBooleanProperty(false);
private TranslateTransition translateAnimation = new TranslateTransition(Duration.seconds(0.25));
private FillTransition fillAnimation = new FillTransition(Duration.seconds(0.25));
private ParallelTransition animation = new ParallelTransition(translateAnimation, fillAnimation);
public BooleanProperty switchedOnProperty() {
return switchedOn;
}
public ToggleSwitch() {
Rectangle background = new Rectangle(100, 50);
background.setArcWidth(50);
background.setArcHeight(50);
background.setFill(Color.WHITE);
background.setStroke(Color.LIGHTGRAY);
Circle trigger = new Circle(25);
trigger.setCenterX(25);
trigger.setCenterY(25);
trigger.setFill(Color.WHITE);
trigger.setStroke(Color.LIGHTGRAY);
DropShadow shadow = new DropShadow();
shadow.setRadius(2);
trigger.setEffect(shadow);
translateAnimation.setNode(trigger);
fillAnimation.setShape(background);
getChildren().addAll(background, trigger);
switchedOn.addListener((obs, oldState, newState) -> {
boolean isOn = newState.booleanValue();
translateAnimation.setToX(isOn ? 100 - 50 : 0);
fillAnimation.setFromValue(isOn ? Color.WHITE : Color.LIGHTGREEN);
fillAnimation.setToValue(isOn ? Color.LIGHTGREEN : Color.WHITE);
animation.play();
});
setOnMouseClicked(event -> {
switchedOn.set(!switchedOn.get());
});
}
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(createContent()));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}