gpt4 book ai didi

java - 将线绑定(bind)到节点的中心

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:02 24 4
gpt4 key购买 nike

我正在创建一个动画,其中有两个圆圈由一条线连接起来。当节点(圆)移动时,我希望线绑定(bind)它们的中心。我已经尝试过什么?

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

public class BindIt extends Application {

@Override
public void start(Stage primaryStage) {
DoubleProperty startX = new SimpleDoubleProperty(100);
DoubleProperty startY = new SimpleDoubleProperty(100);
DoubleProperty endX = new SimpleDoubleProperty(300);
DoubleProperty endY = new SimpleDoubleProperty(300);
Line line = new Line(100, 100, 300, 300);
line.startXProperty().bind(startX);
line.startYProperty().bind(startY);
line.endXProperty().bind(endX);
line.endYProperty().bind(endY);
Circle c1 = new Circle(25);
c1.setCenterX(100);
c1.setCenterY(100);
c1.centerXProperty().bind(startX);
c1.centerYProperty().bind(startY);

Circle c2 = new Circle(25);
c2.setCenterX(300);
c2.setCenterY(300);
c2.centerXProperty().bind(endX);
c2.centerYProperty().bind(endY);


Group root = new Group();
root.getChildren().add(line);
root.getChildren().add(c1);
root.getChildren().add(c2);

Scene scene = new Scene(root, 500, 500);

primaryStage.setTitle("Bind the line!");
primaryStage.setScene(scene);
final Timeline timeline = new Timeline();
timeline.getKeyFrames().addAll(new KeyFrame(Duration.millis(0)), new KeyFrame(Duration.millis(2000), new KeyValue(c1.centerXProperty(), 200)));
timeline.play();

primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

如果我删除所有带有“bind”的行,我就会让圆圈移动。但是使用绑定(bind)时,我收到错误消息“无法绑定(bind)等”我的意思是这些行

line.startXProperty().bind(startX);
line.startYProperty().bind(startY);
line.endXProperty().bind(endX);
line.endYProperty().bind(endY);

c1.centerXProperty().bind(startX);
c1.centerYProperty().bind(startY);

c2.centerXProperty().bind(endX);
c2.centerYProperty().bind(endY);

没有这些行,代码就可以工作。但绑定(bind)没有发生。

有人可以告诉我我哪里做错了吗?

最佳答案

我找到了答案,如果你绑定(bind)一个属性,它就是因变量,所以它不能改变,只能改变自变量。所以我改变了

c1.centerXProperty().bind(startX) 

startX.bind(c1.centerXProperty())

它成功了。谢谢大家的评论。

关于java - 将线绑定(bind)到节点的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29071515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com