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());
}
}
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.