在JavaFX中检测窗口边界? [英] Detecting Window Boundary in JavaFX?

查看:140
本文介绍了在JavaFX中检测窗口边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建一个程序,该程序在窗口的中心显示一个球,底部有四个分别名为Up,Down,Left和Right的按钮.当您按下按钮时,球将向相应的方向移动.我已经弄清楚了那部分,但是我还需要弄清楚如何做到这一点,因此,如果使球朝某个方向移动会遮挡视线,则阻止它朝那个方向移动.我需要找到一种方法来检测窗口的边界,并阻止球超出边界.这是我到目前为止的内容:

I've been trying to create a program that displays a ball in the center of a window, with 4 buttons titled Up, Down, Left and Right at the bottom. When you press the buttons the ball moves in the corresponding direction. I've got that part figured out, but I also need to figure out how to make it so if making the ball go in a certain direction obscures it from view, it stops it from going in that direction. I need to find a way to detect the boundaries of the window, and stopping the ball from being able to go outside of the boundary. Here's what I have so far:

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

这是我希望对程序进行边界检查的部分,但它似乎不起作用:

Here is the part that I was hoping would make the program check for boundaries, but it doesn't seem to work:

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

推荐答案

其他人已经问了一个非常相似的问题

Someone else already asked a very similar question How to make the ball bounce off the walls in JavaFX?

尽管如此,您所缺少的只是在进行下一步之前的检查方法.为了简单起见,我添加了moveAcceptable()并删除了BallControl.

Nevertheless, all you were missing was a check method before making the next move. I added moveAcceptable() and deleted BallControl for simplicity.

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

这篇关于在JavaFX中检测窗口边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆