Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JavaFX program only presents the Stage correctly at the end

  1. #1
    Junior Member
    Join Date
    Apr 2024
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default JavaFX program only presents the Stage correctly at the end

    Hello Everybody.

    I'm solving a programming exercise about JavaFX.

    This is the text of the Exercise:


    (Tic-tac-toe board) Write a program that displays a tic-tac-toe board, as shown
    in Figure 14.43b. A cell may be X, O, or empty. What to display at each cell is
    randomly decided. The X and O are images in the files x.gif and o.gif.

    I've already created previously a program to show a Tic-tac-toe game using console print and println.

    That program uses a two dimensional char[][] to represent the Board.

    Initially all the Board positions are 'E', that is Empty.

    Then, as the player 1 and player 2 play the Board positions are changed to 'O' or 'X'.

    So, I decided to use the previous program and change it to show the Tic-tac-toe board with O and X in it.

    The program is still asking for user input in the console, using a Scanner.

    I've used Images for O, X and a white square image for Empty positions.

    The program checks if the game has ended, before asking for the player1 to enter its position for Row and then for Column.

    After the player 1 has entered its choice, the program checks if there is a Victory.

    The program checks if there is a Victory on the Rows, on the Columns or in the Diagonals.

    If the game is over but there is no Victory, then the game ended in a draw.

    If the game has not ended the program asks the player 2 to enter a row and a column.

    After the player 2 has entered its choice, the program checks if there is a Victory.

    The program checks if there is a Victory on the Rows, on the Columns or in the Diagonals.

    If the game is over but there is no Victory, then the game ended in a draw.

    If there is a Victory the game has ended.

    I'm using three Images, one with an X, another with a O, and another with a white square for the empty positions.

    I'm using ImageViews to show the images.

    I'm using a GridPane to place the ImageViews according to the positions in the Board.

    When I run the Program the console is working well and fluid.

    But the problem I get is with the JavaFX part.

    The program asks the player 1 to enter the row and column.

    After I insert the Row and Column (from 0 to 2), the JavaFX displays an empty Stage with the title I' ve used "Tic-tac-toe board".

    But that Stage is empty and the window has the message "Not responding" in it.

    The Console part asks the palyer 2 to enter the row and column.

    The Stage displayed is empty and the window has the message "Not responding" in it.

    I continue entering player 1 and player 2 choices.

    When the program ends, only then, the Stage is correctly presented displaying the positions with X and O images, according to the choices of player 1 and player 2 during the game.


    This is the console output of the program.




    Enter a row (0, 1, or 2) for player X: 1
    Enter a column (0, 1, or 2) for player X: 1
    Enter a row (0, 1, or 2) for player O: 1
    Enter a column (0, 1, or 2) for player O: 2
    Enter a row (0, 1, or 2) for player X: 0
    Enter a column (0, 1, or 2) for player X: 0
    Enter a row (0, 1, or 2) for player O: 0
    Enter a column (0, 1, or 2) for player O: 2
    Enter a row (0, 1, or 2) for player X: 2
    Enter a column (0, 1, or 2) for player X: 2
    X player won
    BUILD SUCCESSFUL (total time: 7 minutes 23 seconds)


    As you can see the program has done its purpose without errors.

    I crated two moments to display the Stage with the Images for X and O.

    First after the player 1 has entered its choice.

    And the second after the player 2 entered its choice.

    So the problem is only happening with the presentation of the Stage before the game ends.

    After the game ends, the Stage is presented correctly.

    I would like some help to understand this situation.

    Thank you,

    Rogério

  2. #2
    Junior Member
    Join Date
    Apr 2024
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: JavaFX program only presents the Stage correctly at the end

    Hello everybody.

    I've already solved my own problem.

    I have to go now, but I will come back to this.

    Rogério Biscaia

  3. #3
    Junior Member
    Join Date
    Apr 2024
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: JavaFX program only presents the Stage correctly at the end

    Hi Everybody.

    So, I came back here, as promised.

    I have solved my problem.

    But in solving this problem, that has created even more questions that I would like someone in this Forum to answer please.

    First of all the explanation of the solution.

    This is an Oversimplification.

    But it's a necessary Oversimplification.

    So, in my Tic-Tac-Toe Program, there is a While Loop.

    Iniside that while loop:


    proceedGame is a boolean


    public void start(Stage primaryStage) {


    while(proceedGame) {


    Player1 makes its move


    Player2 makes its move


    }


    }


    Instead of using the Stage primaryStage of the Application.start() method, I've created a second Stage.

    I've named this stage boardStage


    Stage boardStage = new Stage();

    boardStage.setTitle("Tic-tac-toe board");

    boardStage.setScene(scene);


    After the player1 makes its move, I update the ImageViewer using the char[][] board (X or O or E (Empty) ).

    I update the GridPane with that ImageViewer.

    Instead of using the method boardStage.show() I use the method showAndWait()


    boardStage.showAndWait();


    Next the player2 makes its move.

    But if I place boardStage.showAndWait() in there I get an Exception, because the Stage was already visible before.

    So I have to change the method to this;



    @Override
    public void start(Stage primaryStage) {


    while(proceedGame) {


    Player1 makes its move

    boardStage.showAndWait();

    boardStage.hide();


    Player2 makes its move

    boardStage.showAndWait();

    boardStage.hide();


    }


    }


    Now the Board is presented correctly during the game, but it does not Show after the game ends.


    There are two possible scenarios for the Tic-Tac-Toe game to End.

    And these two possible scenarios are also the exits for the program to end.


    Scenario 1:

    Victory (One Player Wins)


    Scenario 2:

    No Victory and the Game Ends

    There is no Victory.

    All the board positions have been filled with an X or an O

    In this case the game ends with a Draw


    So, in all of these two scenarios, I added


    boardStage.show();

    Now after the program ends the Stage of the Board is presented.

    So this was the explanation of how I solved this problem.

    Thank you,

    Rogério

    --- Update ---

    Hello, again.

    The first part of the explanation was getting quite long.

    By solving this problem, that has created even more questions that I would like someone in this Forum to answer please.


    Why is it that in the Stage primaryStage, I cannot use primaryStage.showAndWait() ?


    When I have the following code, which Stage is the primary Stage and which one is the secondary (because the Stage primaryStage is not being used) ?


    @Override
    public void start(Stage primaryStage) {



    Stage boardStage = new Stage();

    boardStage.setTitle("Tic-tac-toe board");

    boardStage.setScene(scene);


    while(proceedGame) {



    Player1 makes its move

    boardStage.showAndWait();

    boardStage.hide();


    Player2 makes its move

    boardStage.showAndWait();

    boardStage.hide();


    }

    }


    In the code above, when the program runs the Stage primaryStage is created with init and start and then the primaryStage creates the secondary stage ?

    Thank you,

    Rogério

Similar Threads

  1. JavaFX NullPointerException when creating a new stage.
    By seok96 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 29th, 2021, 08:07 AM
  2. JavaFX - Main Program Loop
    By Blick in forum JavaFX
    Replies: 6
    Last Post: August 16th, 2019, 11:30 AM
  3. Replies: 0
    Last Post: August 4th, 2019, 09:30 AM
  4. [SOLVED] Program not compiling correctly
    By d0mlnance in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 1st, 2013, 09:53 AM
  5. Program not executing correctly
    By daemonlies in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 1st, 2012, 11:14 PM