2016-05-30 5 views
0

Ich habe javafx gui Übung zu tun, und ich muss den Ball entweder nach oben oder unten oder rechts und links bewegen. Momentan geht der Ball zufällig irgendwo hin. Können Sie mir mit dem Code helfen? Wenn ich also die Plus-Taste drücke, wird ein Ball hinzugefügt und geht nach oben oder unten oder nach rechts und links. Und jeder andere hinzugefügte Ball muss sich entweder nach oben oder nach unten verschieben? Jede Hilfe wäre toll. Danke.Wie kann ich einen Ball in JavaFX nach oben oder unten bewegen?

import javafx.animation.KeyFrame; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.beans.property.DoubleProperty; 
import javafx.geometry.Pos; 
import javafx.scene.Node; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ScrollBar; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.util.Duration; 

public class MultipleBounceBall extends Application { 
    @Override // Override the start method in the Application class 
    public void start(Stage primaryStage) { 
    MultipleBallPane ballPane = new MultipleBallPane(); 
    ballPane.setStyle("-fx-border-color: black"); 

    Button btAdd = new Button("+"); 
    Button btSubtract = new Button("-"); 
    HBox hBox = new HBox(10); 
    hBox.getChildren().addAll(btAdd, btSubtract); 
    hBox.setAlignment(Pos.CENTER); 

    // Add or remove a ball 
    btAdd.setOnAction(e -> ballPane.add()); 
    btSubtract.setOnAction(e -> ballPane.subtract()); 

    // Pause and resume animation 
    ballPane.setOnMousePressed(e -> ballPane.pause()); 
    ballPane.setOnMouseReleased(e -> ballPane.play()); 

    // Use a scroll bar to control animation speed 
    ScrollBar sbSpeed = new ScrollBar(); 
    sbSpeed.setMax(20); 
    sbSpeed.setValue(10); 
    ballPane.rateProperty().bind(sbSpeed.valueProperty()); 

    BorderPane pane = new BorderPane(); 
    pane.setCenter(ballPane); 
    pane.setTop(sbSpeed); 
    pane.setBottom(hBox); 

    // Create a scene and place the pane in the stage 
    Scene scene = new Scene(pane, 350, 450); 
    primaryStage.setTitle("Multiple Bounce Ball"); // Set the stage title 
    primaryStage.setScene(scene); // Place the scene in the stage 
    primaryStage.show(); // Display the stage 
    } 

    private class MultipleBallPane extends Pane { 
    private Timeline animation; 

    public MultipleBallPane() { 
     // Create an animation for moving the ball 
     animation = new Timeline(
     new KeyFrame(Duration.millis(50), e -> moveBall())); 
     animation.setCycleCount(Timeline.INDEFINITE); 
     animation.play(); // Start animation 
    } 

    public void add() { 
     Color color = new Color(Math.random(), 
     Math.random(), Math.random(), 0.5); 
     getChildren().add(new Ball(30, 30, 20, color)); 
    } 

    public void subtract() { 
     if (getChildren().size() > 0) { 
     getChildren().remove(getChildren().size() - 1); 
     } 
    } 

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

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

    public void increaseSpeed() { 
     animation.setRate(animation.getRate() + 0.1); 
    } 

    public void decreaseSpeed() { 
     animation.setRate(
     animation.getRate() > 0 ? animation.getRate() - 0.1 : 0); 
    } 

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

    protected void moveBall() { 
     for (Node node: this.getChildren()) { 
     Ball ball = (Ball)node; 
     // Check boundaries 
     if (ball.getCenterX() < ball.getRadius() || 
      ball.getCenterX() > getWidth() - ball.getRadius()) { 
      ball.dx *= -1; // Change ball move direction 
     } 
     if (ball.getCenterY() < ball.getRadius() || 
      ball.getCenterY() > getHeight() - ball.getRadius()) { 
      ball.dy *= -1; // Change ball move direction 
     } 

     // Adjust ball position 
     ball.setCenterX(ball.dx + ball.getCenterX()); 
     ball.setCenterY(ball.dy + ball.getCenterY()); 
     } 
    } 
    } 

    class Ball extends Circle { 
    private double dx = 1, dy = 1; 

    Ball(double x, double y, double radius, Color color) { 
     super(x, y, radius); 
     setFill(color); // Set ball color 
    } 
    } 

    /** 
    * The main method is only needed for the IDE with limited 
    * JavaFX support. Not needed for running from the command line. 
    */ 
    public static void main(String[] args) { 
    launch(args); 
    } 
} 
+0

Was ist das Problem? Haben Sie Probleme, dem 'Ball'-Konstruktor zwei weitere Parameter hinzuzufügen und damit zwei Felder zu initialisieren? – fabian

+0

Ja, ich weiß nicht, wie ich das machen soll. –

Antwort

0

Das Problem ist, dass die Verdrängerelemente der Kugelbewegung (dx und dy) sind beide auf 1. Somit wird die Kugel bewegt sich zu den beiden Richtungen x und y d.h. diagonal. Ändern Sie die Deklaration dieser Variablen zu:

private double dx = 1, dy = 0; // The ball will move right 
private double dx = -1, dy = 0; // The ball will move left 
private double dx = 0, dy = 1; // The ball will move down 
private double dx = 0, dy = -1; // The ball will move up 

Wenn man die Bälle gerne in zufällige Richtungen bewegen, können Sie es zum Beispiel tun können, indem Sie Ihre Ball Klasse zu ändern:

class Ball extends Circle { 
    private double dx; 
    private double dy; 

    Ball(double x, double y, double radius, Color color) { 
     super(x, y, radius); 
     setFill(color); // Set ball color 
     switch (ThreadLocalRandom.current().nextInt(0, 4)) { 
      case 0: 
       dx = 0; 
       dy = 1; 
       break; 
      case 1: 
       dx = 0; 
       dy = -1; 
       break; 
      case 2: 
       dx = 1; 
       dy = 0; 
       break; 
      case 3: 
       dx = -1; 
       dy = 0; 
       break; 
     } 
    } 
} 

Und wenn Sie möchte, dass sie von zufälligen Standorten aus starten, Sie können es tun, indem Sie Ihre MultipleBallPane.add() Methode ändern:

+0

Wie würde ich es dann so machen, dass der nächste Ball aus verschiedenen Sektionen startet? –

+0

Bitte überprüfen Sie meine bearbeitete Antwort. – kornisb

Verwandte Themen