如何在JavaFX中移动形状?

4

我一直在尝试编写一个程序,能够显示一个圆圈,并允许您使用按钮移动它,但我没能弄清楚如何告诉Java在按下某个按钮时移动圆圈的方向。我已经尝试过setX和setY,但显然它们不是Circle的本地功能。以下是我目前的代码:

import javafx.application.Application;
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 Ball extends Application {
    private Circle circle = new Circle();

    @Override
    public void start(Stage primaryStage) {
        HBox pane = new HBox();
        pane.setSpacing(10);
        pane.setAlignment(Pos.CENTER);  
        Button btUp = new Button("UP");
        Button btDown = new Button("DOWN");
        Button btLeft = new Button("LEFT");
        Button btRight = new Button("RIGHT");

        pane.getChildren().add(btUp);
        pane.getChildren().add(btDown);
        pane.getChildren().add(btRight);
        pane.getChildren().add(btLeft);

        btUp.setOnAction(e -> circle.setY(circle.getY() - 10));
        btDown.setOnAction(e -> circle.setY(circle.getY() + 10));
        btLeft.setOnAction(e -> circle.setX(circle.getX() - 10));
        btRight.setOnAction(e -> circle.setX(circle.getX() + 10));

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

        Scene scene = new Scene(borderPane, 200, 200);      
        primaryStage.setTitle("Ball"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show();

        circle.requestFocus();
    }

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

考虑将 [javafx] 标签添加到您的标签列表中,并更好地谈论 javafx 而不是 java,因为这个问题有多种可能被误解的方式。 - isaias-b
使用 setCenterX(),setCenterY()。 - WillShackleford
这里有一个关于在JavaFX画布上移动形状的解决方案。如果你使用场景图(就像你的问题描述中所提到的,这也是一种完全有效的方法)而不是画布,那么解决方案会稍微有所不同(目前我没有现成的例子)。 - jewelsea
2个回答

2

您的代码存在的第一个问题是圆(或者说缺乏圆形)。

如果您计划稍后定义圆,则不应使用无参圆。一种解决方案是在一行中定义半径和填充颜色:

private Circle circle = new Circle(50.0f, Color.RED);

关于移动圆形,您需要跟踪圆形位置的变化,因此请将其添加到您的实例变量中:

private double newY, newX = 0;

我将向您展示如何设置向下按钮(左、右和上的代码与向下非常相似)。将您的setOnAction方法更改为:

btnDown.setOnAction(btnDownActionEvent);

然后在主方法之后(或任何你想要的地方)定义btnDownActionEvent:

EventHandler<ActionEvent> btnDownActionEvent = new EventHandler<ActionEvent>()
{
    @Override
    public void handle(ActionEvent event)
    {
        System.out.println("Pressed Down");
        newY = circle.getCenterY() + 10 + newY;

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

此外,你可以移除circle.requestFocus();


0
创建两个变量:一个用于X坐标,另一个用于Y坐标。 int newY,newX = 0; 现在使用setOnKeyPressed在场景上,当按下箭头键时移动圆形,使用IF逻辑。
scene.setOnKeyPressed(e ->{
    if(e.getCode() == KeyCode.RIGHT){
        newX = newX + 10;
        circle.setTranslateX(newX);
    }
    else if(e.getCode() == KeyCode.LEFT){
        newX = newX - 10;
        circle.setTranslateX(newX);
    }
    else if(e.getCode() == KeyCode.UP){
        newY = newY - 10;
        circle.setTranslateY(newY);
    }
    else if(e.getCode() == KeyCode.DOWN){
        newY = newY + 10;
        circle.setTranslateY(newY);
    }
});

确保在上述代码之前创建场景,否则会出现未定义错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接