如何不让我的图像移出场景

How to not let my image move out of scene

这是我的以下代码:

    public void start(Stage primaryStage) throws Exception {
    Pane root = new Pane();
    Scene scene = new Scene(root, 500, 500, Color.RED);

    ImageView dice = new ImageView(new Image(getClass().getResourceAsStream("dice.jpeg")));
    dice.setX(0);
    dice.setY(300);
    root.getChildren().add(dice);

    scene.setOnKeyPressed(e -> {
        if (dice.getX() >= 0 &&  dice.getX() <= 500 ) {
            switch (e.getCode()) {

                case RIGHT:
                    dice.setX(dice.getX() + KEYBOARD_MOVEMENT_DELTA);
                    break;

                case LEFT:
                    dice.setX(dice.getX() - KEYBOARD_MOVEMENT_DELTA);
                    break;
             }
           }
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}

在我的代码中,我的图像dice可以左右移动,但我不希望它离开场景。我希望它在到达左右场景的尽头后都不会移动。我试着用 if 语句来做,但它不起作用。有什么方法可以让我的图像 dice 不离开场景?任何帮助表示赞赏!

你的回答有几个问题。首先,检查边界的方式。如果满足条件,您的键将不再控制 ImageView。其次,在您的测试条件中使用 dice.getX() 。最好使用 dice.getLayoutBounds().getMaxX()dice.getLayoutBounds().getMinX()。此外,我建议使用 scene.getWidth() 而不是硬编码 Scene 的宽度,因为 Scene 宽度可以更改。 <-(在您发布的代码中)。

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFxTestingGround extends Application {

    double KEYBOARD_MOVEMENT_DELTA = 5;

    @Override
    public void start(Stage primaryStage) throws IOException {
        Pane root = new Pane();
        Scene scene = new Scene(root, 500, 500, Color.RED);

        ImageView dice = new ImageView(createImage("https://cdn.discordapp.com/attachments/250163910454280192/296377451599364107/Untitled.png"));
        dice.setFitHeight(100);
        dice.setFitWidth(100);
        dice.setX(0);
        dice.setY(300);
        root.getChildren().add(dice);

        scene.setOnKeyPressed(e -> {       
            System.out.println(dice.getLayoutBounds().getMinX() + " : " + dice.getLayoutBounds().getMaxX() + " : " + scene.getWidth());
            switch (e.getCode()) {
                case RIGHT:
                    dice.setX(dice.getX() + KEYBOARD_MOVEMENT_DELTA);
                    break;

                case LEFT:
                    dice.setX(dice.getX() - KEYBOARD_MOVEMENT_DELTA);
                    break;
             }


            if (dice.getLayoutBounds().getMinX() < 0)   
            {
                dice.setX(0);
            }            
            else if(dice.getLayoutBounds().getMaxX() > scene.getWidth() )
            {
                dice.setX(dice.getX() - KEYBOARD_MOVEMENT_DELTA);
            }           
        });

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    Image createImage(String url)
    throws IOException {
        URLConnection conn = new URL(url).openConnection();
        conn.setRequestProperty("User-Agent", "Wget/1.13.4 (linux-gnu)");

        try (InputStream stream = conn.getInputStream()) {
            return new Image(stream);
        }
    }
}