Dragony
Dragony

Reputation: 55

Circle does not move from new position, but from starting position

My code for making a circle and moving it with 4 buttons (left, right, up and down) works, but rather than moving it from that new position, it moves from its starting position (y = 0 and x = 0).

package movetheball;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.geometry.Pos;

public class MoveTheBall extends Application {

@Override
public void start(Stage primaryStage) throws Exception {

    Circle circle = new Circle();

    circle.setRadius(50);
    circle.setStroke(Color.BLACK);
    circle.setFill(Color.WHITE);

    Button btn1 = new Button();
    btn1.setText("Left");
    btn1.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            double newY = 0; 
            double newX = 0;
            System.out.println("Went to the left.");
            newX = circle.getCenterX() - 10;

            circle.setTranslateX(newX);
            circle.setTranslateY(newY);
        }
    });

    Button btn2 = new Button();
    btn2.setText("Right");
    btn2.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            double newY = 0; 
            double newX = 0;
            System.out.println("Went to the right.");
            newX = circle.getCenterX() + 10;

            circle.setTranslateX(newX);
            circle.setTranslateY(newY);
        }
    });

    Button btn3 = new Button();
    btn3.setText("Up");
    btn3.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            double newY = 0; 
            double newX = 0;
            System.out.println("Went up.");
            newY = circle.getCenterY() - 10;

            circle.setTranslateX(newX);
            circle.setTranslateY(newY);
        }
    });
    Button btn4 = new Button();
    btn4.setText("Down");
    btn4.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            double newY = 0; 
            double newX = 0;
            System.out.println("Went down.");
            System.out.println("Went up.");
            newY = circle.getCenterY() + 10;

            circle.setTranslateX(newX);
            circle.setTranslateY(newY);
        }
    });


    BorderPane rootPane = new BorderPane();
    rootPane.setCenter(circle);
    HBox hb = new HBox(btn1, btn2, btn3, btn4);
    hb.setAlignment(Pos.CENTER);
    rootPane.setBottom(hb);




    Scene scene = new Scene(rootPane, 400, 400);
    primaryStage.setTitle("Move the circle!");
    primaryStage.setScene(scene);
    primaryStage.show();
}
}

How do I change it to going the position the user wants and going to the new position from that older position?

Thanks!

Upvotes: 1

Views: 441

Answers (1)

fabian
fabian

Reputation: 82531

translateX and centerX are 2 independent properties that both influence the position where the circle is drawn. If you adjust one of those properties, you have to use the previous value of the same property not of the other property.

(Similar for the y properties.)

You should change the code of your event handlers to something like this:

// using only the translate properties
double newY = circle.getTranslateY();
System.out.println("Went to the left.");
double newX = circle.getTranslateX() - 10;

circle.setTranslateX(newX);
circle.setTranslateY(newY);

or

// using only the center properties
double newY = circle.getCenterY();
System.out.println("Went to the left.");
double newX = circle.getCenterX() - 10;

circle.setCenterX(newX);
circle.setCenterY(newY);

Note: Anything related to the y coordinates is not necessary in the code, since those properties are not modified...

Upvotes: 1

Related Questions