尝试在 JavaFX 中制作一个 "Ball" class,它由一个可通过按钮控制的圆圈组成,可以在屏幕上左右移动、上下移动
Trying to make a "Ball" class in JavaFX that consists of a circle controllable by buttons to move left, right, up and down across the screen
我的大纲 Ball
class:
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() {
}
}
我不确定我会在我的方法中加入什么来向左移动、向右移动等
到目前为止我的可执行文件有什么 class:
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();
}
}
我的大纲 Ball
class:
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() {
}
}
我不确定我会在我的方法中加入什么来向左移动、向右移动等
到目前为止我的可执行文件有什么 class:
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();
}
}