gpt4 book ai didi

java - 尝试在 JavaFX 中创建一个 "Ball"类,该类由一个可通过按钮控制的圆圈组成,可在屏幕上向左、向右、向上和向下移动

转载 作者:行者123 更新时间:2023-12-02 10:30:30 25 4
gpt4 key购买 nike

我的 Ball 类的大纲:

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;

public class Ball extends Pane{
public final double radius = 20;
private double x = radius, y = radius;
private double dx = 1, dy = 1;
private Circle circle = new Circle(x, y, radius);
private Timeline animation;

public Ball() {
circle.setFill(Color.GREEN); // Set ball color
getChildren().add(circle); // Place a ball into this pane

// Create an animation for moving the ball

//Not sure if I need an animation


public void play() {
animation.play();
}

public void pause() {
animation.pause();
}


protected void moveLeft() {

}
protected void moveRight() {

}
protected void moveUp() {

}
protected void moveDown() {

}
}

我不太确定我会在我的方法中添加什么来向左、向右移动等。

到目前为止我的可执行类有什么:

public class BallMover extends Application{

Ball ballPane = new Ball(); // Create a ball pane



@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Hold four buttons in an HBox
HBox ballcontrols = new HBox();
Button btLeft = new Button("Left");
Button btRight = new Button("Right");
Button btUp = new Button("Up");
Button btDown = new Button("Down");
ballcontrols.getChildren().addAll(btLeft, btRight, btUp, btDown);


// Create and register the handlers
btLeft.setOnAction(e -> ballPane.moveLeft());
btRight.setOnAction(e -> ballPane.moveRight());
btRight.setOnAction(e -> ballPane.moveUp());
btRight.setOnAction(e -> ballPane.moveDown());

ballPane.setOnMouseClicked(e -> {

});

ballPane.setOnKeyPressed(e -> {

});

BorderPane borderPane = new BorderPane();
borderPane.setCenter(ballPane);
borderPane.setBottom(ballcontrols);
BorderPane.setAlignment(ballcontrols, Pos.CENTER);

// Create a scene and place it in the stage
Scene scene = new Scene(borderPane, 200, 150);
primaryStage.setTitle("ControlCircle"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}

如何使用事件处理程序来移动球?我必须在 moveLeft() moveRight() moveUp() moveDown() 方法中添加什么使这个工作正常吗?

最佳答案

这就是你想要做的吗?

class Ball extends Pane{
public double radius = 20;
private double x = 0;
private double y = 0;
private final double dx = 5, dy = 5;
private Circle circle = new Circle(radius, radius, radius);
private TranslateTransition animation;

public Ball() {
circle.setFill(Color.GREEN);
getChildren().add(circle);
animation = new TranslateTransition(Duration.millis(200), circle);
}

protected void moveLeft() {
animation.setFromX(x); animation.setFromY(y);
animation.setToX(x - dx);
x -= dx;
animation.play();
}
protected void moveRight() {
animation.setToX(x+dx);
x += dx;
animation.play();
}
protected void moveUp() {
animation.setToY(y-dy);
y -= dy;
animation.play();
}
protected void moveDown() {
animation.setToY(y+dy);
y += dy;
animation.play();
}
}

关于java - 尝试在 JavaFX 中创建一个 "Ball"类,该类由一个可通过按钮控制的圆圈组成,可在屏幕上向左、向右、向上和向下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53644141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com