Moving a circle with keyevents in javafx (Solved)

I copied the code in the lambda expression from my text book, except I swapped text for circle. I'm not sure why it is having issues with getX and getY. Any help would be greatly appreciated.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.layout.StackPane;

public class MoveUsingKeys extends Application {
@Override
public void start(Stage primaryStage)
{
StackPane pane = new StackPane();
Circle circle = new Circle(150, 150, 25);

    pane.getChildren().add(circle);
    circle.setOnKeyPressed(e -> {
        switch(e.getCode()){
            case DOWN: circle.setY(circle.getY() - 10); break;
            case UP: circle.setY(circle.getY() + 10); break;
            case LEFT: circle.setX(circle.getX() - 10); break;
            case RIGHT: circle.setX(circle.getX() + 10); break;
        }
    });

    Scene scene = new Scene(pane, 300, 300);
    primaryStage.setTitle("Exercise15_11");
    primaryStage.setScene(scene);
    primaryStage.show();
    
    circle.requestFocus();
}

public static void main(String[] args)
{
    Application.launch(args);
}

}

Hi,

getX and getY don't exist:

http://docs.oracle.com/javafx/2/api/javafx/scene/shape/Circle.html
http://docs.oracle.com/javafx/2/api/index.html

If using Java (you shouldn't do that - it's a horrible language) always see JavaDoc first.

1 Like

Thank you!