2
\$\begingroup\$

I am working with javafx making a simple Kings game. I am currently in a Java course at school and this is just a side project I am looking to get some feedback on.

Main.java

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;

public class Main extends Application {
    private static Stage primaryStage;
    private static Stage secondaryStage;
    private static FirstController firstController;
    private static SecondController secondController;

    @Override
    public void start(Stage primaryStage) {
        Main.primaryStage = primaryStage;
        secondWindow();
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("MainWindow.fxml"));
            AnchorPane rootLayout = (AnchorPane) loader.load();

            firstController = loader.getController();

            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Kings");
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void startGame(){
        firstController.startGame();
    }

    public static Stage getPrimaryStage() {
        return primaryStage;
    }

    public static Stage getSecondaryStage() {
        return secondaryStage;
    }

    public void secondWindow(){

        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("GameOver.fxml"));
            AnchorPane rootLayout = (AnchorPane) loader.load();
            Stage secondaryStage = new Stage();

            Main.secondaryStage = secondaryStage;
            secondController = loader.getController();

            Scene scene = new Scene(rootLayout);
            secondaryStage.setScene(scene);
            secondaryStage.setTitle("Kings");
            //secondaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

FirstController.java

package application;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ResourceBundle;
import javax.imageio.ImageIO;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class FirstController implements Initializable {
    private ArrayList<String> players = new ArrayList<String>();
    private ArrayList<Integer> deck = new ArrayList<Integer>();
    private boolean turnEnd;
    private int currentPlayer;
    private int cardCount;

    @FXML
    private Button btnDraw;
    @FXML
    private Label lblPlayerName;
    @FXML
    private Label lblInstruction;
    @FXML
    private Label lblThumbKing;
    @FXML
    private Label lblQuestionQueen;
    @FXML
    private ImageView imgCard;


    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        startGame();

        // TODO Auto-generated method stub
        btnDraw.setOnAction(e -> {
            if (turnEnd == false) {
                playTurn();
                turnEnd = true;
                btnDraw.setText("Next Turn");

            } else {
                btnDraw.setText("Draw");
                turnEnd = false;
                lblPlayerName.setText("Current Player: " + players.get(currentPlayer));
                hideLabelandImage();
            }
        });
    }


    public void startGame(){
        System.out.println("Ran");
        turnEnd = false;
        currentPlayer = 0;
        cardCount = 0;
        players.add("Danny");
        players.add("Cameron");
        players.add("Nathan");
        lblPlayerName.setText("Current Player: " + players.get(0));
        lblThumbKing.setText("");
        lblQuestionQueen.setText("");
        btnDraw.setText("Draw");
        setDeck();
        shuffleDeck();
    }

    public void setDeck(){
        for(int i = 1; i < 53; i++){
            deck.add(i);
        }
    }

    public void playTurn(){
        if(cardCount < 52) {
            try {
                setImage("bin/application/images/card/image (" + deck.get(cardCount) +").png");
                gameInstructions();
                cardCount++;
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else {
            Main.getSecondaryStage().show();
        }

        if (currentPlayer < players.size() - 1){
            currentPlayer++;
        } else {
            currentPlayer = 0;
        }
    }

    public void setImage(String strFile) throws IOException{
        File file = new File(strFile);
        BufferedImage bufferedImage = ImageIO.read(file);
        Image image = SwingFXUtils.toFXImage(bufferedImage, null);
        this.imgCard.setImage(image);
    }

    public void shuffleDeck(){
        Collections.shuffle(deck);
    }

    public void gameInstructions(){
        int card = deck.get(cardCount);
        System.out.println(deck.get(cardCount));
        String strMessage = null;
        if (card < 5) {
            strMessage = "2 is for you";
        } else if (card > 4 && card < 9) {
            strMessage = "3 is for me";
        } else if (card > 8 && card < 13) {
            strMessage = "Floor";
        } else if (card > 12 && card < 17) {
            strMessage = "Guys drink";
        } else if (card > 16 && card < 21) {
            strMessage = "Girls drink";
        } else if (card > 20 && card < 25) {
            strMessage = "Point to heaven";
        } else if (card > 24 && card < 29) {
            strMessage = "Pick a mate";
        } else if (card > 28 && card < 33) {
            strMessage = "Bust a rhyme";
        } else if (card > 32 && card < 37) {
            strMessage = "Category";
        } else if (card > 36 && card < 41) {
            strMessage = "Waterfall";
        } else if (card > 40 && card < 45) {
            strMessage = "Rule";
        } else if (card > 44 && card < 49) {
            lblThumbKing.setText(players.get(currentPlayer));
            strMessage = "Thumb King";
        } else if (card > 48 && card < 53) {
            lblQuestionQueen.setText(players.get(currentPlayer));
            strMessage = "Question Queen";
        } else {
            strMessage = "Error reading card...";
        }

        lblInstruction.setText(strMessage);
    }

    public void hideLabelandImage(){
        lblInstruction.setText("");
        try {
            setImage("bin/application/images/card/placeholder.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

SecondController.java

package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

public class SecondController implements Initializable {
    @FXML
    private Button btnYes;
    @FXML
    private Button btnNo;

    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        btnYes.setOnAction(e -> {
            Main.startGame();
            Main.getSecondaryStage().hide();
        });

        btnNo.setOnAction(e -> {
            Main.getSecondaryStage().close();
            Main.getPrimaryStage().close();
        });
    }
}

MainWindow.fxml

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

<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>

<AnchorPane id="rootLayout" prefHeight="400.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.FirstController">
  <children>
    <GridPane layoutX="0.0" layoutY="0.0" prefHeight="400.0" prefWidth="500.0">
      <children>
        <Button id="btOK" fx:id="btnDraw" mnemonicParsing="false" text="Draw" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
        <Label id="lblPlayerName" fx:id="lblInstruction" text="Click Draw to start playing" textFill="#ff3d00" wrapText="false" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="1">
          <font>
            <Font size="16.0" fx:id="x1" />
          </font>
        </Label>
        <Label id="lbPlayerName" fx:id="lblPlayerName" font="$x1" text="Label" textFill="#14ff00" GridPane.columnIndex="2" GridPane.halignment="LEFT" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
        <ImageView fitHeight="69.69602142333991" fitWidth="48.00001525878906" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.valignment="TOP">
          <image>
            <Image url="@images/card/thumbking.png" />
          </image>
          <GridPane.margin>
            <Insets left="5.0" top="5.0" />
          </GridPane.margin>
        </ImageView>
        <ImageView fitHeight="69.69602142333991" fitWidth="48.00001525878906" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="0" GridPane.rowIndex="0">
          <image>
            <Image url="@images/card/questionqueen.png" />
          </image>
          <GridPane.margin>
            <Insets left="5.0" />
          </GridPane.margin>
        </ImageView>
        <Label fx:id="lblQuestionQueen" font="$x1" text="" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0" />
        <Label fx:id="lblThumbKing" font="$x1" text="" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0" GridPane.valignment="TOP">
          <GridPane.margin>
            <Insets top="30.0" />
          </GridPane.margin>
        </Label>
        <ImageView id="imCard" fx:id="imgCard" fitHeight="247.96799494848656" fitWidth="170.77686309814453" pickOnBounds="true" preserveRatio="true" styleClass="cardImage" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="0" GridPane.valignment="CENTER" />
      </children>
      <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="266.0" minWidth="10.0" prefWidth="264.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="166.0" minWidth="0.0" prefWidth="2.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="234.0" minWidth="10.0" prefWidth="234.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints maxHeight="297.0" minHeight="10.0" prefHeight="297.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="230.0" minHeight="10.0" prefHeight="33.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="163.0" minHeight="10.0" prefHeight="33.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="163.0" minHeight="10.0" prefHeight="33.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="98.0" minHeight="10.0" prefHeight="37.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>
    <Separator layoutX="5.0" layoutY="279.0" prefWidth="495.0" />
  </children>
</AnchorPane>

GameOver.fxml

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>

<AnchorPane prefWidth="358.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.SecondController">
  <!-- TODO Add Nodes -->
  <children>
    <Pane layoutX="0.0" layoutY="0.0" prefHeight="163.0" prefWidth="358.0">
      <children>
        <Label layoutX="141.0" layoutY="29.0" text="Game over">
          <font>
            <Font size="16.0" />
          </font>
        </Label>
        <Label layoutX="145.0" layoutY="66.0" text="Play again?">
          <font>
            <Font size="14.0" />
          </font>
        </Label>
        <Button fx:id="btnYes" layoutX="275.0" layoutY="129.0" mnemonicParsing="false" prefWidth="50.0" text="Yes" />
        <Button fx:id="btnNo" layoutX="218.0" layoutY="129.0" mnemonicParsing="false" prefWidth="50.0" text="No" />
      </children>
    </Pane>
  </children>
</AnchorPane>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.