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

JavaFX-如何在场景顶部添加分层的“图像”?

  •  0
  • WCKennedays  · 技术社区  · 6 年前

    我正在用JavaFX做一个2D游戏。有一个背景设置为游戏场景。

    我如何添加一个图像,将作为一个“坦克”,可以由玩家操作移动和拍摄,这将是一个场景(与背景)顶部分层?

    值得注意的是,坦克将有一个x,y和旋转参数。然后碰撞将应用到坦克上,坦克也可以发射子弹。

    ViewManager 并从 视图管理器 建造师。我还添加了一个场景 Main.java

    这是从 主类

    import javafx.application.Application; //import JavaFX libraries
    import javafx.scene.Scene;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.Group;
    import javafx.scene.Parent;
    import javafx.stage.Stage;
    
    
    public class Main extends Application {
        @Override
        public void start(Stage primaryStage)
        {
            try
            {
                ViewManager manager = new ViewManager();
                primaryStage = manager.getMainStage();
                primaryStage.setScene(manager.drawImageOnScreen(50, 50, 0, "image path"));
                primaryStage.show();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }//end catch
        }//end start
    
        public static void main(String[] args)
        {
            launch(args);
        }//end main
    }//end class
    

    ViewManager.java 尝试将图像添加到场景顶部的。

    /* I've discarded the imported libraries and packages*/
    
    public class ViewManager {
    
        private static final int HEIGHT = 1000;
        private static final int WIDTH = 1366;
        private AnchorPane mainPane;
        private Scene mainScene;
        private Stage mainStage;
    
        private final static int GAME_BUTTON_START_X = 100;
        private final static int GAME_BUTTON_START_Y = 150;
    
        List<WarstrikeButton> gameButtons;
    
        public ViewManager()
        {
            //Initialize the game stage
            initializeStage();
    
            //Initialize the game buttons
            gameButtons = new ArrayList<>();
            createButtons();
    
            //Create background
            createBackground();
    
        }//end viewManager
    
        private void initializeStage() {
            mainPane = new AnchorPane();
            mainScene = new Scene(mainPane, WIDTH, HEIGHT);
            mainStage = new Stage();
            mainStage.setScene(mainScene);
        }
    
        public Stage getMainStage()
        {
            return mainStage;
        }
    
        private void addGameButton(WarstrikeButton button)
        {
            button.setLayoutX(GAME_BUTTON_START_X);
            button.setLayoutY(GAME_BUTTON_START_Y + gameButtons.size() * 100);
            gameButtons.add(button);
            mainPane.getChildren().add(button);
        }
    
        private void createButtons()
        {
    
            createMoveButton();
            createShootButton();
    
        }//end createButtons method
    
        private void createMoveButton()
        {
            WarstrikeButton moveButton = new WarstrikeButton("Move");
            addGameButton(moveButton);
        }
    
        private void createShootButton()
        {
            WarstrikeButton shootButton = new WarstrikeButton("Shoot");
            addGameButton(shootButton);
        }
    
        private void createBackground()
        {
            Image backgroundImage = new Image("src/com/warstrike/graphics/resources/2d-game-background-6.jpg", 1366, 1000, false, true);
            BackgroundImage background =  new BackgroundImage(backgroundImage, BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.DEFAULT, null);
            mainPane.setBackground(new Background(background));
        }//end createBackground function
    
    //end class
    
        public Scene drawImageOnScreen(double x, double y, double rotation, String imagePath) throws FileNotFoundException
        {
            //Creating an image 
              Image image1 = new Image(new FileInputStream(imagePath));  
    
              //Setting the image view 
              ImageView imageView = new ImageView(image1); 
    
              //Setting the position of the image 
              imageView.setX(x); 
              imageView.setY(y); 
              imageView.setRotate(rotation);
    
              //setting the fit height and width of the image view 
              imageView.setFitHeight(x); 
              imageView.setFitWidth(y); 
    
              //Setting the preserve ratio of the image view 
              imageView.setPreserveRatio(true);
    
              Group root = new Group(imageView);  
    
              //Creating a scene object 
              Scene scene = new Scene(root, 600, 500);  
    
              return scene;
    
    
        }//end drawImageOnScreen function
    
    }
    

    我不知道该怎么办。谢谢你的帮助。

    0 回复  |  直到 6 年前