2016-12-06 46 views
0

如何将三个doubleProperty红色,绿色和蓝色绑定到JavaFX中的'circle.fillProperty()'?绑定到形状fillProperty JavaFX

我可以很容易地结合,例如圆形到doubleProperty这样的radiusProperty:

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

回答

0

您可以使用Bindings.createObjectBinding

的的CirclefillPropertyObjectProperty<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)); 

下面是一个完整的例子:

本例使用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); 
    } 
} 

注:它与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)); 
0

你可以做

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

,同样为绿色和蓝色。