There are two sliders controlling value of x and y. The values are bound to label 1 (lab1) and 2 (lab2).
We bind the expression f(x,y) = 1/2 *x^2*y^2. The resulting value of f(x,y) is bound to label 3 (lab3).
package ex38;
import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Ex38 extends Application {
@Override
public void start(Stage primaryStage) {
Slider s1 = new Slider(0,10,5);
Slider s2 = new Slider(0,10,5);
Label lab1 = new Label();
Label lab2 = new Label();
Label lab3 = new Label();
lab1.textProperty().bind(
new SimpleStringProperty("x: ")
.concat(s1.valueProperty().asString()));
lab2.textProperty().bind(
new SimpleStringProperty("y: ")
.concat(s2.valueProperty().asString()));
DoubleProperty x = s1.valueProperty();
DoubleProperty y = s2.valueProperty();
DoubleBinding sum = x
.multiply(x)
.multiply(y)
.multiply(y)
.multiply(0.5);
lab3.textProperty().bind(new SimpleStringProperty("f(x,y): ")
.concat(sum.asString()));
VBox root = new VBox(50, s1, lab1, s2, lab2, lab3);
root.setPadding(new Insets(50));
root.setSpacing(50);
root.setAlignment(Pos.CENTER);
root.setStyle("-fx-font-size: 16pt;"
+ "-fx-background-color: darkorange;");
Scene scene = new Scene(root, 400, 400);
primaryStage.setTitle("Example 38. Fluent API 2");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is the output for x and y of 5:
No comments:
Post a Comment