我试图将某些形状绑定在一起,所以当我拖动圆时,线条也会跟随它。当我拖动圆时,圆会移动,但直线不会跟随。我正在尝试使用.bind,但它似乎没有做任何事情。这是一些相关方法。
private Line connect(Line line, Circle startNode, Circle endNode) {
line.startXProperty().bind(startNode.centerXProperty());
line.startYProperty().bind(startNode.centerYProperty());
line.endXProperty().bind(endNode.centerXProperty());
line.endYProperty().bind(endNode.centerYProperty());
return line;
}
private void pointsToShapes() {
connect(leftArm, neckNode, leftHandNode);
leftHandNode.setCenterX(leftHand.x);
leftHandNode.setCenterY(leftHand.y);
neckNode.setCenterX(shoulder.x);
neckNode.setCenterY(shoulder.y);
}
leftHandNode.setOnMousePressed(mp);
leftHandNode.setOnMouseDragged(md);
//mouse pressed
EventHandler<MouseEvent> mp = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
System.out.println("pressed");
sceneX = t.getSceneX();
sceneY = t.getSceneY();
translateX = ((Circle)(t.getSource())).getTranslateX();
translateY = ((Circle)(t.getSource())).getTranslateY();
}
};
//mouse dragged
EventHandler<MouseEvent> md = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
System.out.println("dragged");
double offsetX = t.getSceneX() - sceneX;
double offsetY = t.getSceneY() - sceneY;
double newTranslateX = translateX + offsetX;
double newTranslateY = translateY + offsetY;
((Circle)(t.getSource())).setTranslateX(newTranslateX);
((Circle)(t.getSource())).setTranslateY(newTranslateY);
}
};
答案 0 :(得分:0)
如@fabian所述,在绑定到线属性时,您需要考虑翻译属性。
private Line connect(Line line, Circle startNode, Circle endNode) {
line.startXProperty().bind(startNode.centerXProperty().add(startNode.translateXProperty()));
line.startYProperty().bind(startNode.centerYProperty().add(startNode.translateYProperty()));
line.endXProperty().bind(endNode.centerXProperty().add(endNode.translateXProperty()));
line.endYProperty().bind(endNode.centerYProperty().add(endNode.translateYProperty()));
return line;
}