2016-12-06 2 views
0

JavaFXの3つのdoubleProperty Red、Green、Blueを 'circle.fillProperty()'にバインドするにはどうすればよいですか?形状へのバインドfillProperty JavaFX

私は簡単に、たとえば、このようなdoublePropertyに円のradiusPropertyをバインドすることができます。

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

答えて

0

あなたはBindings.createObjectBindingを使用することができます。

CirclefillPropertyは、あなたが結合にPaintオブジェクトを作成する必要がありますので、ObjectProperty<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 Sを使用していますこれらのコントロールの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())); 

と同様の操作を行うことができます。

関連する問題