I have a group of shapes like this:
Group g = new Group();
Rectangle r = new Rectangle( 100, 100, 50, 50 );
Circle c = new Circle( 125, 125, 10 );
g.getChildren().addAll( r, c );
and a Line, which is not part of the group, that ends at the centre of the circle.
Line l = new Line( 0, 0, 125, 125 );
I have made it so that the group can be dragged around as per the Mouse Events demo in Ensemble (just a couple of mouse listeners that update the x and y translation property). What I would like though is for the end of the line to follow the centre of the circle. That means updating the endX and endY properties of the line.
The problem I’m having is that endX and endY are in scene local space (I think). When I drag the group around the screen the centerX and centerY properties of the circle don’t get updated. All I seem to get is changes to translationX and translationY on the group. I can’t figure out how I find the new centre of the circle after (and during) the drag operation.
Pointers very gratefully received as this has got me tearing my hair out!
The best solution I’ve come up with is to put a translation listener on the group, get the centre position of the circle and then add the translation of the group but the results aren’t exactly great the end of the line roughly follows the circle but jumps about and drifts away. Something like this:
translateXProperty().addListener( new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov, Number oldValue, Number newValue) {
Connector c = getInputConnectors().get( 0 );
double x = circle.getCenterX();
double y = circle.getCenterY();
line.setEndX( x + newValue.doubleValue());
}
});
Use binding:
P.S.: your way can work too, but note you should have listeners for both x and y property.