0

I'm trying to fiddle with a connect four game and Im trying to modify it to make the user choose whether he/she likes it a two player game or against an AI and choose who takes the turn first (in the existing code first turn is random). So, I made two jframe that takes these as inputs from the user (radio button). However, I cannot make the original java to display after creating an instance of it. Here is what I have now:

Here is the constructor in the java where it calls the method setVisible(true) for the JFrame:

public class testAI2 extends JFrame implements ActionListener
{
    JLabel lblPlayer = new JLabel("Player: ");
    JLabel lblCurrentPlayer = new JLabel("Blue");
    JPanel pnlMenu = new JPanel();
    JPanel pnlBoards = new JPanel();
    JButton btnNewGame2 = new JButton("New Game");

    Board[][] Boards = new Board[7][6];

    boolean winnerExists = false;
    int currentPlayer = 1;
    boolean AI;

    public testAI2(boolean AI)
    {
        super("Four In A Line");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        chooseTurn turn = new chooseTurn();
        currentPlayer = (int) turn.getTurnNumber();

        this.AI = AI;

        btnNewGame2.addActionListener(this);
        switch(currentPlayer)
        {
            case 1:
                lblCurrentPlayer.setForeground(Color.blue);
                lblCurrentPlayer.setText("Blue");
                break;
            case 2:
                lblCurrentPlayer.setForeground(Color.red);
                lblCurrentPlayer.setText("Red");
                break;
        }
        pnlMenu.add(btnNewGame2);
        pnlMenu.add(lblPlayer);
        pnlMenu.add(lblCurrentPlayer);

        pnlBoards.setLayout(new GridLayout(6, 7));

        for(int j = 0; j < 6; j++)
            for(int i = 0; i < 7; i++)
            {
                Boards[i][j] = new Board(i, j);
                Boards[i][j].addActionListener(this);
                pnlBoards.add(Boards[i][j]);
            }

        add(pnlMenu, BorderLayout.NORTH);
        add(pnlBoards, BorderLayout.CENTER);    
        setSize(500, 500);
        setVisible(true);

        if(currentPlayer == 2 && AI) insertTo(minimax());
    }

PlayerorAI.java

private void playerSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                             
        if(twoPlayer.isSelected()){
            setPlayerNumber(2);
            chooseTurn turn = new chooseTurn();
            turn.setVisible(true);
            turn.setLocationRelativeTo(null);
            dispose();
        }
        else if (vsAi.isSelected()){
            setPlayerNumber(1);
            chooseTurn turn = new chooseTurn();
            turn.setVisible(true);
            turn.setLocationRelativeTo(null);
            dispose();
            }
        else
            JOptionPane.showMessageDialog(null, "Please Select One!", "Choose Players Error", JOptionPane.ERROR_MESSAGE);
    }  

chooseTurn.java

private void turnSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                           
        PlayerorAI players = new PlayerorAI();
        int playersNo = players.getPlayerNumber();
        testAI2 test;
        if(firstTurn.isSelected()){
            setTurnNumber(1);            
            if (playersNo == 2){
                test = new testAI2(false);
            }
            dispose();
        }
        else if (secondTurn.isSelected()){
            setTurnNumber(2);
            if (playersNo == 1){
                test = new testAI2(true);
            }
            dispose();
        }
        else
        JOptionPane.showMessageDialog(null, "Please Select One!", "Choose Players Error", JOptionPane.ERROR_MESSAGE);
    }

Now, in the original code he has a main where he calls this constructor (new TestAI2(true) or new TestAI2(false)) and it displays just fine, but why won't it display when I call it from chooseTurn.java? Thanks in advance!

  • Displaying from a constructor is generally a bad idea... use some kind of init() method. – Ondra Žižka Mar 07 '18 at 17:01
  • @OndraŽižka okay that makes sense thank you, I'll try that. It's just that I dont want to modify his code too much – Paul Steven Fantonalgo Nadera Mar 07 '18 at 17:04
  • I understand. UI classes have all kind of callbacks, I don't remember Swing specifically but check the docs for some callback called when before the other window is shown for the first time. – Ondra Žižka Mar 07 '18 at 17:08
  • 1
    See [The use of multiple JFrames, Good / Bad practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (the general consensus says it's bad). I'd go for a `JDialog` instead: [How to make Dialogs](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html). – Frakcool Mar 07 '18 at 17:55
  • 1
    Where are you calling `playerSubmitActionPerformed`? For better help sooner post a proper [mcve] – Frakcool Mar 07 '18 at 17:57

0 Answers0