rabidrabbit
rabidrabbit

Reputation: 11

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.

Upvotes: 0

Views: 195

Answers (1)

c0der
c0der

Reputation: 18792

You can let the event handler update the text:

 circle.setOnMouseDragged(e ->  {
     circle.setCenterX(e.getX());
     circle.setCenterY(e.getY());
     addCoordinates.setText(e.getX() +"-"+e.getY());
 });

Or use binding:

    Label addCoordinates = new Label();
    addCoordinates.layoutXProperty().bind(circle.centerXProperty());
    addCoordinates.layoutYProperty().bind(circle.centerYProperty());

    SimpleStringProperty centerX = new SimpleStringProperty();
    centerX.bind(Bindings.createStringBinding( ()->String.valueOf(circle.centerXProperty().get()), circle.centerXProperty()));
    
    SimpleStringProperty centerY = new SimpleStringProperty();
    centerY.bind(Bindings.createStringBinding( ()->String.valueOf(circle.centerYProperty().get()), circle.centerYProperty()));

    addCoordinates.textProperty().bind(Bindings.concat(centerX,"-",centerY));

Upvotes: 1

Related Questions