JavaFX-如何在场景上方添加“图像”?

时间:2019-02-12 22:06:47

标签: java javafx graphics

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

我如何添加一个图像,该图像将用作玩家可以移动和拍摄的“坦克”,并将其放置在场景的顶部(带有背景)?

值得注意的是,战车将具有x,y和旋转参数。然后,将对坦克施加碰撞,该坦克还可以向其中发射子弹。

这在Eclipse IDE和JavaFX上运行。我尝试向ViewManager中添加另一个方法,并从ViewManager构造函数中调用该方法。我还在Main.java中添加了一个场景,以便可以显示两个场景。

这来自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 个答案:

没有答案