在JavaFX中检测窗口边界?

时间:2015-11-07 22:13:17

标签: object events javafx boundary

我一直在尝试创建一个在窗口中央显示一个球的程序,底部有4个按钮,分别为Up,Down,Left和Right。当您按下按钮时,球会沿相应的方向移动。我已经找到了那个部分,但是我还需要弄清楚如何制作它,如果让球向某个方向移动遮挡视线,它会阻止它向那个方向前进。我需要找到一种方法来检测窗口的边界,并阻止球离开边界。这是我到目前为止所做的:

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);

1 个答案:

答案 0 :(得分: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);
    }
}