0

I just started JavaFx this past week and having a problem trying to get a circle setup in a pane to respond to button event handler. I have buttons setup with names left, right, up, down which when pressed should move the circle inside inside the pane. My problem is I can't get the circle to respond to my event handlers at all. I saw another tutorial that incorporated key presses to move the circle and I'm trying something similar but with buttons instead. Any help in getting me in the right direction would be great thanks.

package movingball;

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

public class MovingBall extends Application{
  private CirclePane circlePane = new CirclePane();


@Override
public void start(Stage primaryStage) {
    StackPane pane = new StackPane();
    Circle circle = new Circle(50);
    circle.setStroke(Color.BLACK);
    circle.setFill(Color.WHITE);
    pane.getChildren().add(circle);

    HBox hBox = new HBox();
    hBox.setSpacing(10);
    hBox.setAlignment(Pos.CENTER);
    Button btLeft = new Button("Left");
    Button btRight = new Button("Right");
    Button btUp = new Button("Up");
    Button btDown = new Button("Down");
    hBox.getChildren().addAll(btLeft, btRight, btUp, btDown);

    btLeft.setOnAction(new LeftHandler());
    btRight.setOnAction(new RightHandler());
    btUp.setOnAction(new UpHandler());
    btDown.setOnAction(new DownHandler());


    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(pane);
    borderPane.setBottom(hBox);
    BorderPane.setAlignment(hBox, Pos.CENTER);

    // Create a scene and place it in the stage
    Scene scene = new Scene(borderPane, 500, 350);
    primaryStage.setTitle("Move the Ball"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
  }



class LeftHandler implements EventHandler<ActionEvent> {

    @Override
    public void handle(ActionEvent event) {
        circlePane.left();

    }

}

class RightHandler implements EventHandler<ActionEvent> {

    @Override
    public void handle(ActionEvent event) {
        circlePane.right();

    }

}

class UpHandler implements EventHandler<ActionEvent> {

    @Override
    public void handle(ActionEvent event) {
        circlePane.up();

    }

}

class DownHandler implements EventHandler<ActionEvent> {

    @Override
    public void handle(ActionEvent event) {
        circlePane.down();

    }

}

class CirclePane extends StackPane {
    private Circle circle = new Circle(50);

    public CirclePane() {
        getChildren().add(circle);
        circle.setStroke(Color.BLACK);
        circle.setFill(Color.WHITE);
    }

    public void left() {
        circle.setCenterX(circle.getCenterX() - 10);
    }

    public void right() {
        circle.setCenterX(circle.getCenterX() + 10);
    }

    public void up() {
        circle.setCenterY(circle.getCenterY() - 10);
    }

    public void down() {
        circle.setCenterY(circle.getCenterY() + 10);
    }
}


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

}

1 Answer 1

1

The first issue is that you're moving the CirclePane, but it isn't part of your scene. Remove the pane and circle you create in the start(...) method, and put the circlePane in the center of the BorderPane instead.

The second issue is that a StackPane will center the circle, adjusting its coordinate system to keep it centered as the circle moves. So make CirclePane a subclass of Pane, instead of StackPane. Alternatively, you can call circle.setManaged(false); to prevent the StackPane from positioning the circle.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much this one was giving me lots of issues and now starting to understand it a bit more thanks to your suggestion it actually moves now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.