如果我未在initialize()中调用方法,JavaFX将不会在游戏板上显示我的卡

时间:2018-11-15 15:58:59

标签: javafx fxml

我在游戏板上显示游戏卡时遇到问题。如果我不使用JavaFX的initialize()方法执行操作,就不会“渲染”图像。

主要:

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/menu/menu.fxml"));
    primaryStage.setTitle("This is not a chess inspired game!!");
    primaryStage.setScene(new Scene(root, 1920, 1080));
    primaryStage.show();
}

MenuController:

public class MenuController {


    @FXML
    private Pane menuPane;

    public void startGame() throws IOException {
        Pane tableViewPane = FXMLLoader.load(getClass().getResource("/tableview/tableview.fxml"));
        menuPane.getChildren().setAll(tableViewPane);

    }

    public void viewHighscore() throws IOException {
        Pane highscorePane = FXMLLoader.load(getClass().getResource("/highscore/highscore.fxml"));
        menuPane.getChildren().setAll(highscorePane);
    }

    public void quitGame() {
        System.exit(0);
    }
}

TableViewController:

@FXML
private HBox playerOneHandBox;
@FXML
private HBox playerTwoHandBox;
@FXML
private AnchorPane cardPane;

private Server server;
private Deck deck; 

public TableViewController() throws IOException {
    deck = new Deck();
    server = Server.getInstance();
}

public void showPlayerHand(List<String> commands) throws IOException {
    try {
        deck.getCardsFromJSON(); // gets all cards (50)
    } catch (IOException e) {
        e.printStackTrace();
    }
    String player = commands.get(1);
    for (int i = 2; i < commands.size(); i++) {
        Card card = deck.getCards().get(Integer.parseInt(commands.get(i)));
        System.out.println(card.getId());
        String cardURL = "/card/card.fxml";
        switch (player) {
            case "1":
                // player1 gets 5 cards to her hand, from her deck
                cardPane = FXMLLoader.load(getClass().getResource(cardURL));
                playerOneHandBox.getChildren().add(cardPane);
                playerOneHandBox.setSpacing(50);
                playerOneHandBox.setAlignment(Pos.CENTER);
                cardPane.setId(String.valueOf(card.getId()));
                System.out.println(cardPane);
                //TODO: Code for showing card in FX for player 1
                break;
            case "2":
                //player 2 gets 5 cards on her hand from her deck.
                cardPane = FXMLLoader.load(getClass().getResource(cardURL));
                playerOneHandPane.getChildren().add(cardPane);
                playerTwoHandBox.setSpacing(50);
                playerTwoHandBox.setAlignment(Pos.CENTER);
                cardPane.setId(String.valueOf(card.getId()));
                System.out.println(cardPane);
                //TODO: Code for showing card in FX for player 2
                break;
            default:
                System.out.println("No player! Something wrong with string input from gameEngine");
                break;
        }
    }
}

tableView.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.effect.Glow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>

<Pane fx:id="tableViewPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.TableViewController">
    <children>
        <ImageView fx:id="tableImageView" fitHeight="1080.0" fitWidth="1920.0" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="@gameboard.jpg" />
            </image>
        </ImageView>
      <HBox fx:id="playerOneHandBox" layoutX="451.0" layoutY="767.0" prefHeight="224.0" prefWidth="973.0" />
      <HBox fx:id="playerTwoHandBox" layoutX="546.0" layoutY="91.0" prefHeight="207.0" prefWidth="830.0" />
        <Rectangle id="playerOneTableOne" fx:id="playerOneTableOne" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="899.0" layoutY="556.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerOneTableFour" fx:id="playerOneTableFour" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="1170.0" layoutY="556.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerOneTableTwo" fx:id="playerOneTableTwo" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="1034.0" layoutY="556.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerOneTableThree" fx:id="playerOneTableThree" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="763.0" layoutY="556.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerOneTableFive" fx:id="playerOneTableFive" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="626.0" layoutY="556.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerOneTableSix" fx:id="playerOneTableSix" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="1305.0" layoutY="556.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerOneTableSeven" fx:id="playerOneTableSeven" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="491.0" layoutY="556.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerTwoTableSix" fx:id="playerTwoTableSix" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="492.0" layoutY="341.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerTwoTableSeven" fx:id="playerTwoTableSeven" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="1306.0" layoutY="341.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerTwoTableFour" fx:id="playerTwoTableFour" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="627.0" layoutY="341.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerTwoTableTwo" fx:id="playerTwoTableTwo" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="764.0" layoutY="341.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerTwoTableThree" fx:id="playerTwoTableThree" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="1035.0" layoutY="341.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerTwoTableFive" fx:id="playerTwoTableFive" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="1171.0" layoutY="341.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerTwoTableOne" fx:id="playerTwoTableOne" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="900.0" layoutY="341.0" opacity="0.5" stroke="WHITE" strokeType="INSIDE" width="125.0" />
        <Rectangle id="playerTwoDeck" fx:id="playerTwoDeck" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="326.0" layoutY="52.0" rotate="45.0" stroke="WHITE" strokeType="INSIDE" width="108.0" />
        <Rectangle id="playerOneDeck" fx:id="playerOneDeck" arcHeight="50.0" arcWidth="500.0" fill="#1f93ff00" height="175.0" layoutX="326.0" layoutY="853.0" rotate="135.0" stroke="WHITE" strokeType="INSIDE" width="108.0" />
        <Button id="endTurn" fx:id="endTurn" layoutX="1560.0" layoutY="516.0" mnemonicParsing="false" opacity="0.5" prefHeight="47.0" prefWidth="109.0" text="End Turn" />
        <Label id="playerOneHp" fx:id="playerOneHp" layoutX="1527.0" layoutY="603.0" text="20" textFill="#ffffffcc">
            <font>
                <Font name="System Bold" size="64.0" />
            </font>
        </Label>
        <Label id="playerTwoHp" fx:id="playerTwoHp" layoutX="1527.0" layoutY="384.0" text="20" textFill="#ffffffcd">
            <font>
                <Font name="System Bold" size="64.0" />
            </font>
        </Label>
        <Circle fill="#00000080" layoutX="1605.0" layoutY="301.0" radius="37.0" stroke="WHITE" strokeType="INSIDE" />
        <Circle fill="#000000b8" layoutX="1606.0" layoutY="781.0" radius="37.0" stroke="WHITE" strokeType="INSIDE" />
        <Circle id="playerOneGraveyard" fx:id="playerOneGraveyard" fill="#ffffff8a" layoutX="1569.0" layoutY="960.0" radius="100.0" stroke="BLACK" strokeType="INSIDE" />
        <Circle id="playerTwoGraveyard" fx:id="playerTwoGraveyard" fill="#ffffff8a" layoutX="1569.0" layoutY="124.0" radius="100.0" stroke="BLACK" strokeType="INSIDE" />
        <ProgressBar id="playerOneMana" fx:id="playerOneMana" layoutX="695.0" layoutY="1026.0" opacity="0.8" prefHeight="30.0" prefWidth="530.0" progress="1.0">
            <effect>
                <Glow level="0.65" />
            </effect>
        </ProgressBar>
        <ProgressIndicator id="playerOneHpRound" fx:id="playerOneHpRound" blendMode="RED" layoutX="1507.0" layoutY="608.0" prefHeight="104.0" prefWidth="115.0" progress="0.28" />
        <ProgressIndicator id="playerTwoHpRound" fx:id="playerTwoHpRound" blendMode="RED" layoutX="1507.0" layoutY="391.0" prefHeight="104.0" prefWidth="115.0" progress="0.28" />
        <ProgressBar id="playerTwoMana" fx:id="playerTwoMana" layoutX="697.0" layoutY="24.0" opacity="0.8" prefHeight="30.0" prefWidth="530.0" progress="1.0">
            <effect>
                <Glow level="0.65" />
            </effect>
        </ProgressBar>
        <ImageView id="winner" fx:id="winner" fitHeight="539.0" fitWidth="1019.0" layoutX="451.0" layoutY="328.0" pickOnBounds="true" preserveRatio="true" visible="false">
            <image>
                <Image url="@WinnerScreen.png" />
            </image>
            <effect>
                <Glow level="0.65" />
            </effect>
        </ImageView>
        <ImageView id="PlayerTwoTurn" fx:id="PlayerTwoTurn" fitHeight="279.0" fitWidth="141.0" layoutX="1536.0" layoutY="114.0" pickOnBounds="true" preserveRatio="true" visible="false">
            <image>
                <Image url="@Fire_GIF.gif" />
            </image>
        </ImageView>
        <ImageView id="PlayerOneTurn" fx:id="PlayerOneTurn" fitHeight="279.0" fitWidth="141.0" layoutX="1536.0" layoutY="595.0" pickOnBounds="true" preserveRatio="true" visible="false">
            <image>
                <Image url="@Fire_GIF.gif" />
            </image>
        </ImageView>
    </children>
</Pane>

现在所有矩形都只是占位符,所以请忽略它们! 上面的代码不会将图片添加到HBox,但这将:

public void initialize() throws IOException {
    cardPane = FXMLLoader.load(getClass().getResource("/card/card.fxml"));
    playerOneHandBox.getChildren().add(cardPane);
}

Card.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane fx:id="cardPane" onMouseClicked="#getSelectedCard" prefHeight="175.0" prefWidth="125.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.CardController">
    <children>
        <Label layoutX="45.0" layoutY="29.0" text="KORT" />
        <Label layoutX="37.0" layoutY="100.0" text="ATTACK" />
        <Label layoutX="53.0" layoutY="68.0" text="HP" />
      <ImageView fx:id="cardImageView" fitHeight="175.0" fitWidth="125.0" pickOnBounds="true" preserveRatio="true" />
    </children>
</AnchorPane>

此方法来自我的“服务器”类,它为fx提供卡的ID。然后在TableViewController的showPlayerHand()方法中,比较从服务器获取的ID,并在showPlayerHand()中创建它们。

    public void initGame() throws IOException {
        deck.createFullDeck();
        initPlayer();

        String player1hand = getStringFromList(p2.getPlayerHand());
        String player2hand = getStringFromList(p1.getPlayerHand());
        server.msgToFX("showplayerhand,1,"+player1hand);
        server.msgToFX("showplayerhand,2,"+player2hand);
 }

showPlayerHand()方法的输出:

1. for FX
2. for Console
1
starting game
Player1: 0,4,31,18,1,
Player2: 30,37,46,41,34,
=== Call to showPlayerHand() in FX for Player1 ===
CardId 0/  imgURL: /cardpics/Card1.png AnchorPane[id=0]
CardId 4/  imgURL: /cardpics/Card1.png AnchorPane[id=4]
CardId 31/  imgURL: /cardpics/Card4.png AnchorPane[id=31]
CardId 18/  imgURL: /cardpics/Card2.png AnchorPane[id=18]
CardId 1/  imgURL: /cardpics/Card1.png AnchorPane[id=1]
=== Call to showPlayerHand() in FX for Player2 ===
CardId 30/  imgURL: /cardpics/Card4.png AnchorPane[id=30]
CardId 37/  imgURL: /cardpics/Card4.png AnchorPane[id=37]
CardId 46/  imgURL: /cardpics/Card5.png AnchorPane[id=46]
CardId 41/  imgURL: /cardpics/Card5.png AnchorPane[id=41]
CardId 34/  imgURL: /cardpics/Card4.png AnchorPane[id=34]

为什么?我无法自己弄清楚...我已经被困了两天了。

谢谢。

0 个答案:

没有答案