代码之家  ›  专栏  ›  技术社区  ›  user6857832

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

  •  0
  • user6857832  · 技术社区  · 7 年前

        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 不离开现场?感谢您的帮助!

    1 回复  |  直到 7 年前
        1
  •  0
  •   SedJ601    7 年前

    你的答案中有几个问题。首先,你检查边界的方式。如果满足该条件,您的钥匙将不再控制 ImageView . 其次,使用 dice.getX() dice.getLayoutBounds().getMaxX() dice.getLayoutBounds().getMinX() scene.getWidth() 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);
            }
        }
    }