简体   繁体   English

绑定到形状fillProperty JavaFX

[英]Binding to a shape fillProperty JavaFX

How can I bind a three doubleProperty Red, Green and Blue to a 'circle.fillProperty()' in JavaFX? 如何在JavaFX中将三个doubleProperty红色,绿色和蓝色绑定到“ circle.fillProperty()”?

I can easily bind for example the radiusProperty of a circle to a doubleProperty like this: 我可以轻松地将一个圆的radiusProperty绑定到doubleProperty,如下所示:

Circle circle = new Circle();
circle.radiusProperty().bind(boid.getRadiusProperty());

You can use Bindings.createObjectBinding . 您可以使用Bindings.createObjectBinding

The fillProperty of the Circle has the type of ObjectProperty<Paint> so you have to create a Paint object in the binding: CirclefillProperty具有ObjectProperty<Paint>的类型,因此您必须在绑定中创建一个Paint对象:

private IntegerProperty r = new SimpleIntegerProperty(0);
private IntegerProperty g = new SimpleIntegerProperty(0);
private IntegerProperty b = new SimpleIntegerProperty(0);

circle.fillProperty().bind(Bindings.createObjectBinding(() -> Color.rgb(r.get(), g.get(), b.get()), r, g, b));

Here is a complete example: 这是一个完整的示例:

This example uses Spinner s as input control, note that the valueProperty of these controls could be directly used as dependency of the binding. 本示例使用Spinner作为输入控件,请注意,这些控件的valueProperty可以直接用作绑定的依赖项。

public class Main extends Application {

    private IntegerProperty r = new SimpleIntegerProperty(0);
    private IntegerProperty g = new SimpleIntegerProperty(0);
    private IntegerProperty b = new SimpleIntegerProperty(0);

    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root, 400, 400);

            Group group = new Group();

            Circle circle = new Circle(60);
            circle.setCenterX(70);
            circle.setCenterY(70);

            circle.fillProperty()
                    .bind(Bindings.createObjectBinding(() -> Color.rgb(r.get(), g.get(), b.get()), r, g, b));

            group.getChildren().add(circle);

            root.setCenter(group);

            Spinner<Integer> spinnerR = new Spinner<>(0, 255, 100);
            Spinner<Integer> spinnerG = new Spinner<>(0, 255, 100);
            Spinner<Integer> spinnerB = new Spinner<>(0, 255, 100);

            r.bind(spinnerR.valueProperty());
            g.bind(spinnerG.valueProperty());
            b.bind(spinnerB.valueProperty());

            root.setBottom(new HBox(spinnerR, spinnerG, spinnerB));

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Note: It is the same with DoubleProperty . 注意: DoubleProperty

private DoubleProperty r = new SimpleDoubleProperty(0);
private DoubleProperty g = new SimpleDoubleProperty(0);
private DoubleProperty b = new SimpleDoubleProperty(0);

circle.fillProperty().bind(Bindings.createObjectBinding(() -> Color.rgb(r.getValue().intValue(), g.getValue().intValue(), b.getValue().intValue()), r, g, b));

You can do 你可以做

DoubleProperty red = new SimpleDoubleProperty();
red.bind(Bindings.createDoubleBinding( () ->
    ((Color)circle.getFill()).getRed(),
    circle.fillProperty()));

and similarly for green and blue. 同样适用于绿色和蓝色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM