1

So I am creating a Quiz game in java for a university project. In the first part it had to be command line only, but for the second part I have to create a GUI for it and so I have chosen JAVAFX with Scenebuilder(I am a total beginner with javafx). My problem is that I don't know how to use the values that i get from the TextFields that I have in my other classes. For example i have this Game Class

public class Game {

private int howManyPlayers;
private int numberOfRounds;
private int numberOfQuestions;
private ArrayList<Player> players;
private Categories categories;


public  Game(int howManyPlayers,int numberOfRounds,int numberOfQuestions){

    this.howManyPlayers=howManyPlayers;
    this.numberOfRounds=numberOfRounds;
    this.numberOfQuestions=numberOfQuestions;
    players=new ArrayList<>();
    Scanner scanner = new Scanner(System.in);
    categories = new Categories();

    for(int i=0;i<howManyPlayers;i++){
        Parent root;
        try {
            FXMLLoader loader= new FXMLLoader(getClass().getResource("resources/UsernameInputController.fxml"));
            root=loader.load();
            Stage stage = new Stage();
            stage.setTitle("New Game");
            stage.setScene(new Scene(root,600,400));
            stage.show();
        }catch (IOException e){
            e.printStackTrace();
        }
        players.add(new Player(username));
    }

}

/****
 * starts the game which will be played for how many rounds the player chose and for each round
 * a different game mode is chosen randomly .
 */
public void start(){
    int choice;


    for(int i=0;i<numberOfRounds;i++){
        System.out.println("Press ENTER to start the round!!!");
        try {
            System.in.read();
        }catch (Exception e){}
        choice=gamemodePicker();
        switch (choice){
            case 0:
                System.out.println("POINTBUILDER : Choose the correct answer and win 1000 points!");
                Gamemode gamemode1 = new PointBuilder();
                gamemode1.gamemodeSetUp(players,numberOfQuestions, categories);
                break;
            case 1:
                System.out.println("BETTING : Win or lose the amount points you bet!");
                Gamemode gamemode2=new Betting();
                gamemode2.gamemodeSetUp(players,numberOfQuestions, categories);
                break;
        }
    }
}

/***
 * displays the number of rounds played and the final score of the player, while also
 * giving the option to play again if the player wants
 * @return false if the player wants to play again and true if the player wants to end the game
 */
public boolean end(){

    String playAgain="";
    Scanner scanner= new Scanner(System.in);
    System.out.format("GAME ENDED AFTER %d ROUNDS!\n",numberOfRounds);
    for(Player player:players){
        System.out.format("Player %s has a final score of: %d points\n",player.getUsername(),player.getScore());
    }
    System.out.println("Would you like to play again?(Y/N)");
    do {
        try{
            playAgain=scanner.nextLine();
            if(!(playAgain.equals("Y")||playAgain.equals("N"))){
                System.out.println("Wrong Input!!!");
            }
        }catch (InputMismatchException e){
            System.out.println("Wrong Input!!!");
        }
    }while(!(playAgain.equals("Y")||playAgain.equals("N")));
    if(playAgain.equals("Y"))return false;
    else return true;
}

/***
 *
 * @return a random number which corresponds to the game mode which will be played in this round
 */
private int gamemodePicker(){

    Random rand = new Random();
    int choice = rand.nextInt(2);
    return(choice);

}

in which I create load the fxml file which has a textfield so that the player can type his username, submit it and the i can take it and save it in players Arraylist to use it later in the project.

This is the fxml file which gets loaded

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" 
    xmlns:fx="http://javafx.com/fxml/1" 
    fx:controller="MainPackage.Controllers.UsernameInputController">
    <children>
       <TextField fx:id="username" layoutX="220.0" layoutY="187.0" />
         <Label layoutX="248.0" layoutY="143.0" text="Username">
           <font>
             <Font size="24.0" />
           </font>
         </Label>
         <Button layoutX="261.0" layoutY="238.0" mnemonicParsing="false" 
          onMouseClicked="#submitUsername" text="Submit">
            <font>
              <Font size="18.0" />
            </font>
         </Button>
    </children>
</AnchorPane>

and this is the Controller.

    public class UsernameInputController {

public TextField username;

  public void submitUsername(MouseEvent mouseEvent) {
      //System.out.println(username.getText());
  }
}

PHOTO OF THE GUI

In conclusion what I am trying to accomplish is to take the username that the player types and use it in my Game Class so that I can fill my players Arraylist and start the game.

janblake
  • 11
  • 1
  • 1
    Your code is a bit of a mess. You don't need a Scanner if you are getting values from your GUI. You have a username variable that isn't defined anywhere. You are constructing UI in a loop, which is odd. Your username TextField should be annotated with @FXML so the FXML loader can initialize it. Rather than a loop, your submitUsername method should be adding the new players and when enough users are registered you should move to the next scene of your UI. – swpalmer Nov 15 '20 at 18:14
  • First of all thanks a lot for your answer , it's very useful information. As I said ,I am trying to implement the GUI in the already existing game that I made so the code that I posted is more of a combination of my already existing code and my ideas on how to do the thing I wanted , rather than a working piece of code. I would also like to ask you something else based on your comment. If I add the players in the arraylist using the submitUsername function of the controller , then will I be able to use the arraylist in the game class or should I do everything from the Controller? – janblake Nov 15 '20 at 21:19
  • If you have an ArrayList with players, you are free to pass it to any method... so when the array has size()==howManyPlayers, you can create the next part of the UI and pass the array to the controller for that part. – swpalmer Nov 17 '20 at 03:04

0 Answers0