2014-10-11 2 views
0

버튼을 클릭 할 때마다 볼을 만들려고합니다. 하나의 공을 만들 수 있지만 '만들기'버튼을 반복해서 클릭하면 여러 개의 공을 만들 수없는 이유가 있습니다.java fx 버튼을 클릭하면 볼 생성

도움을 주시면 감사하겠습니다.

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(); 
}*/ 
} 

답변

0

추가 버튼을 클릭 할 때마다 같은 원의 서클을 사용합니다. 그래서 매번 동일한 원이 장면에 추가됩니다. 매번 새로운 서클을 만들면 좋을 것입니다.

관련 문제