java 如何在 JavaFX 中移动形状?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33190849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:19:44  来源:igfitidea点击:

How to move shapes in JavaFX?

javajavafxshapesdirection

提问by dungo

I've been trying to make a program that displays a circle and lets you move it using buttons but I haven't been able to figure out how to tell Java what direction to move the Circle when you press a certain button. I've tried setX and setY but apparently they aren't native to Circle. Here's what I've got so far:

我一直在尝试制作一个程序来显示一个圆圈并让您使用按钮移动它,但是我一直无法弄清楚当您按下某个按钮时如何告诉 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);
    }
}

回答by Jay Lin

The first problem with your code is the circle (or lack thereof).

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

You shouldn't use the no-argument circle unless you plan to define it later. One solution is to define the radius and fill color in one line:

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

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

As for moving the circle, you'll need to keep track of the changing circle position so add this with your instance variables:

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

private double newY, newX = 0;

I'll show you how to setup the down button (the code for Left, Right, and Up are very similar to down). Change your setOnAction method to:

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

btnDown.setOnAction(btnDownActionEvent);

Then define btnDownActionEvent after the main method (or wherever you want):

然后在 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);
    }
};

Also, you can remove circle.requestFocus();

此外,您可以删除 circle.requestFocus();

回答by Maihan Nijat

Create two variables: One for X coordinates and one for Y coordinates.

创建两个变量:一个用于 X 坐标,一个用于 Y 坐标。

int newY, newX = 0;

int newY, newX = 0;

Now use setOnKeyPressedon Scene to move the circle when arrow keys are pressed, using IF Logic.

现在使用IF 逻辑在场景上使用setOnKeyPressed在按下箭头键时移动圆圈。

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);
    }
});

Make sure to create Scene before above code, else you will get undefined error.

确保在上面的代码之前创建场景,否则你会得到未定义的错误。