Issue
DoubleBinding angle = new SimpleDoubleProperty(myIndex*Math.PI*2).divide(nrMoons);
moon.centerXProperty().bind(earth.centerXProperty().add(Math.cos(angle.doubleValue()).multiply(RADIUS_ORBIT)));
that is the part of my code, where I get an error that double angle.doubleValue() cannot be dereferenced, and Math.cos() requires a double not a DoubleBinding, any idea on how to convert it?
Solution
Assuming the quantity you want to add is the product of the cosine of the angle, and RADIUS_ORBIT
, you just need to use the operator *
instead of the function multiply
:
moon.centerXProperty().bind(earth.centerXProperty().add(Math.cos(angle.doubleValue()) * RADIUS_ORBIT));
Note that this probably won't work the way you want: the value you pass to add(...)
is not observable (it's just a plain double
), so the value will only update if earth.centerXProperty()
changes, but won't update if angle
changes. This is not so much because of using *
instead of multiply()
, but because Math.cos()
returns a plain double
instead of some kind of ObservableDoubleValue
. There's no direct API in JavaFX properties/bindings for "taking the cosine of a value".
If you want this to depend dynamically on angle
, you should use a custom binding (which also has the advantage of making the computation easier to see in the code).
I think this is the value you want to compute:
moon.centerXProperty.bind(Bindings.createDoubleBinding(() -> {
double earthCenter = earth.getCenterX();
double offset = Math.cos(angle.get()) * RADIUS_ORBIT ;
return earthCenter + offset ;
}, earth.centerXProperty(), angle);
Answered By - James_D