【问题标题】:How to move shapes in JavaFX?如何在 JavaFX 中移动形状?
【发布时间】:2015-10-17 19:44:23
【问题描述】:

我一直在尝试制作一个显示圆圈并允许您使用按钮移动它的程序,但我无法弄清楚如何告诉 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,因为有多种方式可能会误解这个问题。
  • 使用 setCenterX(), setCenterY()
  • 这是Moving shapes on a JavaFX Canvas 的解决方案。如果您使用场景图而不是画布(如您的问题中所示,也是一种完全有效的方法),则解决方案将略有不同(目前我没有现成的示例)。

标签: java javafx shapes direction


【解决方案1】:

您的代码的第一个问题是圆圈(或缺少圆圈)。

除非您打算稍后定义,否则不应使用无参数圆圈。一种解决方案是在一行中定义半径和填充颜色:

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

至于移动圆圈,您需要跟踪不断变化的圆圈位置,因此将其添加到您的实例变量中:

private double newY, newX = 0;

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

btnDown.setOnAction(btnDownActionEvent);

然后在main方法之后定义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();

【讨论】:

    【解决方案2】:

    创建两个变量:一个用于 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);
        }
    });
    

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

    【讨论】:

      猜你喜欢
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 2015-09-06
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多