JavaFX distance between two circle and keep updating property

℡╲_俬逩灬. 提交于 2019-12-02 09:21:26

You could create a DoubleProperty

DoubleProperty distanceProperty = new SimpleDoubleProperty();

and a ChangeListener in which you calculate the distance

ChangeListener<Number> changeListener = (observable, oldValue, newValue) -> {

  Point2D p1 = new Point2D(circle1.getCenterX(), circle1.getCenterY());
  Point2D p2 = new Point2D(circle2.getCenterX(), circle2.getCenterY());
  distanceProperty.set(p1.distance(p2));

};

assign the listener

circle1.centerXProperty().addListener( changeListener);
circle1.centerYProperty().addListener( changeListener);
circle2.centerXProperty().addListener( changeListener);
circle2.centerYProperty().addListener( changeListener);

and bind the distanceProperty to the text

Text text = new Text();
text.textProperty().bind(distanceProperty.asString());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!