2015-11-07 69 views
1

我一直在試圖創建一個程序,在窗口的中心顯示一個球,並在底部標題爲上,下,左和右4個按鈕。當您按下按鈕時,球沿相應的方向移動。我已經弄清楚了這一點,但我也需要弄清楚如何讓球從某個方向進入某個方向,從而阻止它朝着這個方向前進。我需要找到一種方法來檢測窗口的邊界,並阻止球離開邊界。這是我到目前爲止有:在JavaFX中檢測窗口邊界?

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
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.stage.Stage; 

public class Ball extends Application { 
    private BallControl circle = new BallControl(); 
    @Override 
    public void start(Stage primaryStage) { 
     HBox pane = new HBox(); 
     pane.setSpacing(10); 
     pane.setAlignment(Pos.CENTER); 
     Button btUp = new Button("UP"); 
     Button btDown = new Button("DOWN"); 
     Button btLeft = new Button("LEFT"); 
     Button btRight = new Button("RIGHT"); 

     pane.getChildren().add(btLeft); 
     pane.getChildren().add(btRight); 
     pane.getChildren().add(btUp); 
     pane.getChildren().add(btDown); 

     BorderPane borderPane = new BorderPane(); 
     borderPane.setCenter(circle); 
     borderPane.setBottom(pane); 
     BorderPane.setAlignment(pane, Pos.CENTER); 

     btUp.setOnAction(new UpHandler()); 
     btDown.setOnAction(new DownHandler()); 
     btLeft.setOnAction(new LeftHandler()); 
     btRight.setOnAction(new RightHandler()); 

     Scene scene = new Scene(borderPane, 250, 250);  
     primaryStage.setTitle("Ball"); // Set the stage title 
     primaryStage.setScene(scene); // Place the scene in the stage 
     primaryStage.show(); 

    } 
     class UpHandler implements EventHandler<ActionEvent> { 
      public void handle(ActionEvent e) { 
       circle.up(); 
       System.out.println("Up Button Pressed"); 
      } 
     } 
     class DownHandler implements EventHandler<ActionEvent> { 
      public void handle(ActionEvent e) { 
       circle.down(); 
       System.out.println("Down Button Pressed"); 
      } 
     } 
     class LeftHandler implements EventHandler<ActionEvent> { 
      public void handle(ActionEvent e) { 
       circle.left(); 
       System.out.println("Left Button Pressed"); 
      } 
     } 
     class RightHandler implements EventHandler<ActionEvent> { 
      public void handle(ActionEvent e) { 
       circle.right(); 
       System.out.println("Right Button Pressed"); 
      } 
     } 

     public static void main(String[] args) { 
      launch(args); 
     }  
    } 

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

     public BallControl() { 
     getChildren().add(circle); 
     circle.setCenterX(125.0f); 
     circle.setCenterY(115.0f); 
     circle.setRadius(25.0f); 
     circle.setStroke(Color.BLACK); 
     circle.setFill(Color.WHITE); 
     } 

     protected void moveBall() { 
     // Check boundaries 
     if (x < radius || x > getWidth() - radius) { 
      dx = 0; // Change ball move direction 
     } 
     if (y < radius || y > getHeight() - radius) { 
      dy = 0; // Change ball move direction 
     } 

     // Adjust ball position 
     x += dx; 
     y += dy; 
     circle.setCenterX(x); 
     circle.setCenterY(y); 
     } 
     public void up() { 
     circle.setCenterY(circle.getCenterY() - 10); 
     } 
     public void down() { 
     circle.setCenterY(circle.getCenterY() + 10); 
     } 
     public void left() { 
     circle.setCenterX(circle.getCenterX() - 10); 
     }  
     public void right() { 
     circle.setCenterX(circle.getCenterX() + 10); 
     } 
     } 

這裏是我希望會作出邊界程序檢查的一部分,但它似乎並沒有工作:

protected void moveBall() { 
    // Check boundaries 
    if (x < radius || x > getWidth() - radius) { 
     dx = 0; // Change ball move direction 
    } 
    if (y < radius || y > getHeight() - radius) { 
     dy = 0; // Change ball move direction 
    } 

    // Adjust ball position 
    x += dx; 
    y += dy; 
    circle.setCenterX(x); 
    circle.setCenterY(y); 
+0

感謝所有的幫助傢伙,真的很感激。 – dungo

+0

'moveBall()'應該檢查所有方法up(),down(),left()和right()的碰撞嗎? – ItachiUchiha

+0

是的,在實踐中。 – dungo

回答

1

別人已經問一個非常類似的問題How to make the ball bounce off the walls in JavaFX?

儘管如此,你所缺少的是一個檢查方法,然後再做下一步。爲了簡單起見,我添加了moveAcceptable()並刪除了BallControl。

public class Ball extends Application 
{ 
    Circle circle = new Circle(); 
    Pane bc = new Pane(); 
    public BorderPane borderPane; 

    @Override 
    public void start(Stage primaryStage) 
    { 
     bc.getChildren().add(circle); 

     circle.setCenterX(125.0f); 
     circle.setCenterY(115.0f); 
     circle.setRadius(25.0f); 
     circle.setStroke(Color.BLACK); 
     circle.setFill(Color.WHITE); 


     HBox pane = new HBox(); 
     pane.setSpacing(10); 
     pane.setAlignment(Pos.CENTER); 
     Button btUp = new Button("UP"); 
     Button btDown = new Button("DOWN"); 
     Button btLeft = new Button("LEFT"); 
     Button btRight = new Button("RIGHT"); 

     pane.getChildren().add(btLeft); 
     pane.getChildren().add(btRight); 
     pane.getChildren().add(btUp); 
     pane.getChildren().add(btDown); 

     borderPane = new BorderPane(); 
     borderPane.setCenter(bc); 
     borderPane.setBottom(pane); 
     BorderPane.setAlignment(pane, Pos.CENTER); 

     btUp.setOnAction(new UpHandler()); 
     btDown.setOnAction(new DownHandler()); 
     btLeft.setOnAction(new LeftHandler()); 
     btRight.setOnAction(new RightHandler()); 

     Scene scene = new Scene(borderPane, 250, 250); 
     primaryStage.setTitle("Ball"); // Set the stage title 
     primaryStage.setScene(scene); // Place the scene in the stage 
     primaryStage.show(); 
    } 

    class UpHandler implements EventHandler<ActionEvent> 
    { 
     public void handle(ActionEvent e) 
     { 
      if (moveAcceptable("up")) 
       circle.setCenterY(circle.getCenterY() - 10); 
      System.out.println("Up Button Pressed"); 
     } 
    } 

    class DownHandler implements EventHandler<ActionEvent> 
    { 
     public void handle(ActionEvent e) 
     { 
      if (moveAcceptable("Down")) 
       circle.setCenterY(circle.getCenterY() + 10); 
      System.out.println("Down Button Pressed"); 
     } 
    } 

    class LeftHandler implements EventHandler<ActionEvent> 
    { 
     public void handle(ActionEvent e) 
     { 
      if (moveAcceptable("Left")) 
       circle.setCenterX(circle.getCenterX() - 10); 
      System.out.println("Left Button Pressed"); 
     } 
    } 

    class RightHandler implements EventHandler<ActionEvent> 
    { 
     public void handle(ActionEvent e) 
     { 
      if (moveAcceptable("Right")) 
       circle.setCenterX(circle.getCenterX() + 10); 
      System.out.println("Right Button Pressed"); 
     } 
    } 

    public boolean moveAcceptable(String direction) 
    { 
     final Bounds bounds = borderPane.getLayoutBounds(); 

     if (direction.equalsIgnoreCase("up")) 
     { 
      return (circle.getCenterY() > (bounds.getMinY() + circle.getRadius())); 
     } else if (direction.equalsIgnoreCase("down")) 
     { 
      return circle.getCenterY() < (bounds.getMaxY() - circle.getRadius()); 
     } else if (direction.equalsIgnoreCase("right")) 
     { 
      return circle.getCenterX() < (bounds.getMaxX() - circle.getRadius()); 
     } else //left 
     { 
      return circle.getCenterX() > (bounds.getMinX() + circle.getRadius()); 
     } 
    } 

    public static void main(String[] args) 
    { 
     launch(args); 
    } 
}