如何在JavaFX中移动形状?

时间:2015-10-17 19:44:24

标签: java javafx shapes direction

我一直在尝试制作一个显示圆圈的程序,并允许您使用按钮移动它但我还没有能够弄清楚当您按下时如何告诉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);
    }
}

2 个答案:

答案 0 :(得分:2)

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

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

BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
String s = x.readLine();
System.out.println();
int firstNumber = Integer.parseInt(s);
s = x.readLine();
System.out.println();
int secondNumber = Integer.parseInt(s);

至于移动圆圈,您需要跟踪变化的圆圈位置,因此请将其与实例变量相加:

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

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

private double newY, newX = 0;

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

btnDown.setOnAction(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); } };

答案 1 :(得分:0)

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

int newY, newX = 0;

现在使用场景中的 setOnKeyPressed 按下箭头键移动圆圈,使用IF Logic。

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

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