2021年1月16日星期六

JavaFX: Updating doubles based on the center of a circle

I'm building a GUI interface where there's a circle that can be dragged. Atop the circle is text that shows the sum of the current x and y coordinates of the center of the circle.

The only problem is that the number shown remain the same as I move them. The values begin at the calculation made based on the circle's position at the beginning, but do not change as the circle moves. As you could probably guess, I would like it to show the number changing as the circle is dragged and the calculations are done.

Code:

  import javafx.event.ActionEvent;  import javafx.event.EventHandler;  import javafx.scene.Scene;  import javafx.scene.layout.Pane;  import javafx.stage.Stage;  import javafx.scene.shape.Circle;  import javafx.scene.paint.Color;  import javafx.scene.text.Text;    public class AddedCircle extends Application  {      public static void main(String[] args)      {          launch(args);       }      @Override      public void start(Stage stage) throws Exception      {                    //Circle node                    Circle circle = new Circle();          circle.setRadius(20);          circle.setStroke(Color.RED);          circle.setFill(Color.RED);          circle.setCenterX(50);          circle.setCenterY(50);            //Add drag function          circle1.setOnMouseDragged(e ->          {              circle.setCenterX(e.getX());              circle.setCenterY(e.getY());          });                     //Get x and y coordinates of the center of the circle                    double x = circle.getCenterX();          double y = circle.getCenterY();                    //Add the coordinates                    double A = x + y;                    //Have the result appear starting from the center of the circle                    Text addCoordinates = new Text(circle.getCenterX(), circle.getCenterY(), A);          addCoordinates.xProperty().bind(circle.centerXProperty());          addCoordinates.yProperty().bind(circle.centerYProperty());                    Pane pane = new Pane();             pane.getChildren().add(circle);          pane.getChildren().add(addCoordinates);                Scene scene = new Scene(pane, 150, 300);          stage.setScene(scene);          stage.show();      }  }    

I know I should find a way to bind the circle's current location to the double value. I need the double value to calculate the sum. All I need is for the x and y values to update as I drag the circle. I'm drawing a blank here.

https://stackoverflow.com/questions/65756951/javafx-updating-doubles-based-on-the-center-of-a-circle January 17, 2021 at 11:06AM

没有评论:

发表评论