点击创建按钮时创建球的java fx

时间:2014-10-11 01:47:27

标签: java javafx-2

我试图在点击创建按钮时创建球。我可以创建一个单球,但由于某些原因,当创建'按钮被反复点击。

感谢任何帮助。

package week3;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.TilePane;
import javafx.geometry.Orientation;
import javafx.geometry.Insets;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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 BounceBallControl1 extends Application {
        public final double radius = 10;
        private double x = radius, y = radius;
        private double dx = 1, dy = 1;
        private Circle circle = new Circle(x, y, radius);
        private Timeline animation;
@SuppressWarnings("restriction")
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
//BallPane ballPane = new BallPane();

Button btnCreate = new Button("Create");
Button btnDelete = new Button("Delete");
btnCreate.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
btnDelete.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
TilePane tileButtons = new TilePane(Orientation.HORIZONTAL);
tileButtons.setPadding(new Insets(5, 5, 5, 70));
tileButtons.setHgap(20.0);
tileButtons.getChildren().addAll(btnCreate, btnDelete);

Slider slSpeed = new Slider();
slSpeed.setMax(20);
//rateProperty().bind(slSpeed.valueProperty());

BorderPane pane = new BorderPane();
//pane.setCenter(ballPane);
pane.setTop(slSpeed);
pane.setBottom(tileButtons);

btnCreate.setOnAction(new EventHandler<ActionEvent>() {

    public void handle(ActionEvent event) {
        for(int i=0;i<=100;i++)
        circle.setFill(Color.TURQUOISE); // Set ball color
        pane.getChildren().add(circle); // Place a ball into this pane

        // Create an animation for moving the ball
        animation = new Timeline(
        new KeyFrame(Duration.millis(50), (e -> {
            // Check boundaries
            if (x < radius || x > pane.getWidth() - radius) {
            dx *= -1; // Change ball move direction
            }
            if (y < radius || y > pane.getHeight() - radius) {
            dy *= -1; // Change ball move direction
            }

            // Adjust ball position
            x += dx;
            y += dy;
            circle.setCenterX(x);
            circle.setCenterY(y);
            })));
        animation.setCycleCount(Timeline.INDEFINITE);
        animation.play(); // Start animation
    }
});



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

/*public DoubleProperty rateProperty() {
return animation.rateProperty();
}*/
}

1 个答案:

答案 0 :(得分:0)

每次单击“添加”按钮时,都使用相同的圆圈实例。所以每次将相同的圆圈添加到场景中。每次尝试创建新的圈子,都可以。