Reputation: 95
I have this programming working for the most part. The circle seems to only be able to move in a small area. Then it hits a border. Why is this? Do I need to make circlePane larger?
MoveTheCircle:
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.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class MoveTheCircle extends Application {
private CirclePane circlePane = new CirclePane();
public void start(Stage primaryStage) {
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().add(btLeft);
hBox.getChildren().add(btRight);
hBox.getChildren().add(btUp);
hBox.getChildren().add(btDown);
btLeft.setOnAction(e -> circlePane.left());
btRight.setOnAction(e -> circlePane.right());
btUp.setOnAction(e -> circlePane.up());
btDown.setOnAction(e -> circlePane.down());
circlePane.setOnMouseClicked(e -> {
if (e.getButton() == MouseButton.PRIMARY) {
circlePane.left();
} else if (e.getButton() == MouseButton.PRIMARY) {
circlePane.right();
} else if (e.getButton() == MouseButton.PRIMARY) {
circlePane.up();
} else if (e.getButton() == MouseButton.PRIMARY) {
circlePane.down();
}
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(circlePane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
Scene scene = new Scene(borderPane, 350, 250);
primaryStage.setTitle("Move The Ball");
primaryStage.setScene(scene);
primaryStage.show();
}
class CirclePane extends StackPane {
private Circle circle = new Circle(25);
public CirclePane() {
getChildren().add(circle);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
}
public void left() {
circlePane.getTranslateX();
circlePane.setTranslateX(-10);
}
public void right() {
circlePane.getTranslateX();
circlePane.setTranslateX(+10);
}
public void up() {
circlePane.getTranslateY();
circlePane.setTranslateY(-10);
}
public void down() {
circlePane.getTranslateY();
circlePane.setTranslateY(+10);
}
}
public static void main(String[] args) {
launch(args);
}
}
Any and all help is appreciated.
Upvotes: 0
Views: 490
Reputation: 36722
You need to have
public void left() {
circle.setTranslateX(circle.getTranslateX()-10);
}
instead of
public void left() {
circle.getTranslateX();
circle.setTranslateX(-10);
}
Any similarly for all the other methods as well.
Upvotes: 1