0

This is a tic tac toe game and it works fine. But when i win the game and click on play again the game closes but i intened that it starts again. I used dispose to close the current window and start another window. In my other game dispose works fine but it isnt here. i have used dispose in last in message function. If someone wins the game i call message function. Edit: MY requiremet is that on clicking play again he current game should close and a new game should start.

package games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;



public class tictoe extends JFrame implements MouseListener
{
private static final Color COLOR_UNCLICKED = Color.white;
private static final Color COLOR_HITT= Color.CYAN;
private static final Color COLOR_HIT = Color.red;
private static final int UNCLICKED = 5;
private static final int HIT = 7;
private static final int HIT1 = 6;
private JLabel title;
private JPanel titlePanel;
private JButton[][] gridButton;
private JPanel gridPanel;
private final int ROWS = 3;
private final int COLS = 3;
private int[][] board;
int count=0;
GridListener gridListener = new GridListener();

Dimension boardSize = new Dimension(340, 400);
public tictoe()
{
    title = new JLabel("TIC TAC TOE");
    titlePanel = new JPanel();
    titlePanel.add(title);
    gridButton = new JButton[ROWS][COLS];
    board= new int [ROWS][COLS];
    gridPanel = new JPanel();
    gridPanel.setPreferredSize(boardSize);
    gridPanel.setLayout(new GridLayout(3, 3));
    for (int r = 0; r < gridButton.length; r++)
        for (int c = 0; c < gridButton[r].length; c++)
        {
        gridButton[r][c] = new JButton();
        //gridButton[r][c].setBackground(COLOR_UNCLICKED);
        gridButton[r][c].setEnabled(true);
        gridButton[r][c].addActionListener(gridListener);
        gridPanel.add(gridButton[r][c]);
        }
    for (int r = 0; r < board.length; r++)
        for (int c = 0; c < board.length; c++)
        {
        board[r][c] = UNCLICKED;
        gridButton[r][c].setEnabled(true);
        gridButton[r][c].setBackground(COLOR_UNCLICKED);
        }
    this.setLayout(new BorderLayout());
    this.add(titlePanel, "North");
    this.add(gridPanel, BorderLayout.CENTER);
    this.setPreferredSize(new Dimension(400, 400));
}

public static void main(String[] args) {
    tictoe n= new tictoe();
    n.pack();
    n.setVisible(true);
    n.setResizable(false);

}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

 class GridListener implements ActionListener
{



public void actionPerformed(ActionEvent evt) {


        //System.out.println(count);

for (int r = 0; r < gridButton.length; r++)
for(int c = 0; c < gridButton[r].length; c++)
{

if (evt.getSource() != gridButton[r][c])
continue;
handleGridButton(r,c);
gridButton[r][c].setEnabled(false);
check();
return;
}
    }
}
 public void handleGridButton(int r, int c)
 {
 if (board[r][c] == UNCLICKED)
 {
     if(count%2==0)
     {
         board[r][c] = HIT;
         gridButton[r][c].setBackground(COLOR_HIT);
         gridButton[r][c].setEnabled(false);
         //((JButton)e.getSource()).setText("x");
     }

 else
 {

     board[r][c] = HIT1;
     gridButton[r][c].setBackground(COLOR_HITT);
     gridButton[r][c].setEnabled(false);
 }
 ++count;
 }
 }
 public void check()
 {
     if(board[0][0]==HIT && board[0][1]==HIT && board[0][2]==HIT)
         message();
     else if (board[0][0]==HIT1 && board[0][1]==HIT1 && board[0][2]==HIT1)
         message();
     else if (board[1][0]==HIT && board[1][1]==HIT && board[1][2]==HIT)
         message();
     else if (board[1][0]==HIT1 && board[1][1]==HIT1 && board[1][2]==HIT1)
         message();
     else if (board[2][0]==HIT && board[2][1]==HIT && board[2][2]==HIT)
         message();
     else if (board[2][0]==HIT1 && board[2][1]==HIT1 && board[2][2]==HIT1)
         message();
     else if(board[0][0]==HIT && board[1][0]==HIT && board[2][0]==HIT)
         message();
     else if(board[0][0]==HIT1 && board[1][0]==HIT1 && board[2][0]==HIT1)
         message();
     else if(board[0][1]==HIT && board[1][1]==HIT && board[2][1]==HIT)
         message();
     else if(board[0][1]==HIT1 && board[1][1]==HIT1 && board[2][1]==HIT1)
         message();
     else if(board[0][2]==HIT && board[1][2]==HIT && board[2][2]==HIT)
         message();
     else if(board[0][2]==HIT1 && board[1][2]==HIT1 && board[2][2]==HIT1)
         message();
     else if(board[0][0]==HIT && board[1][1]==HIT && board[2][2]==HIT)
         message();
     else if(board[0][0]==HIT1 && board[1][1]==HIT1 && board[2][2]==HIT1)
         message();
     else if(board[0][2]==HIT && board[1][1]==HIT && board[2][0]==HIT)
         message();
     else if(board[0][2]==HIT1 && board[1][1]==HIT1 && board[2][0]==HIT1)
         message();



 }
 public void message()
 {
     if(count%2==1)
     { Object[] options = { "Exit", "Play Again" };
     int choice = JOptionPane.showOptionDialog(null, 
         "Player 1 wins", 
         "Quit?", 
         JOptionPane.YES_NO_OPTION, 
         JOptionPane.QUESTION_MESSAGE, 
         null, 
         options, 
         options[0]);

     // interpret the user's choice
     if (choice == JOptionPane.YES_OPTION)
     {
       System.exit(0);
     }
     if (choice == JOptionPane.NO_OPTION)
     {
        dispose();
        tictoe m= new tictoe(); 

     } 
     }
     else
     {
         Object[] options = { "Exit", "Play Again" };
         int choice = JOptionPane.showOptionDialog(null, 
             "Player 2 wins", 
             "Quit?", 
             JOptionPane.YES_NO_OPTION, 
             JOptionPane.QUESTION_MESSAGE, 
             null, 
             options, 
             options[0]);

         // interpret the user's choice
         if (choice == JOptionPane.YES_OPTION)
         {
           System.exit(0);
         }
         if (choice == JOptionPane.NO_OPTION)
         {
           dispose();
            tictoe m= new tictoe(); 

         }
     }
 }
 }
Vaibhav Setia
  • 25
  • 1
  • 8
  • Why do you dispose the window? Just clear the GUI and reset all variables. – QBrute Jul 19 '17 at 06:29
  • or call the `main` method again which should also work fine – XtremeBaumer Jul 19 '17 at 06:29
  • and how do i clear the gui? @QBrute – Vaibhav Setia Jul 19 '17 at 06:30
  • See https://stackoverflow.com/questions/1234912/how-to-programmatically-close-a-jframe, maybe one of the answers there will help. It looks to me as if `dispose()` only works sometimes. The javadoc doesn't say that `dispose()` closes a window, at least that's not how I read it. – ajb Jul 19 '17 at 06:31
  • @XtremeBaumer i m calling the main method again only and it isnt working even if i dont use dispose and call again it doesnt work – Vaibhav Setia Jul 19 '17 at 06:31
  • @ajb one of the comments there says dispose destroys jframe object – Vaibhav Setia Jul 19 '17 at 06:33
  • "Destroying an object" may not be enough (if they're referring to the Java object). There's a window manager responsible for deciding what's displayed and isn't displayed on the screen. The `dispose()` javadoc doesn't say anything about notifying the window manager and asking it to remove the window. And it looks like sometimes the window manager figures out that the Java object is gone and undraws the window, but sometimes it doesn't. I don't really know how it works, but it seems surest to notify the window manager. – ajb Jul 19 '17 at 06:40

1 Answers1

0

For dispose look here: https://stackoverflow.com/a/13360489/3319324

Your tictoe-variable m is killed right after you created it. SO there will be no new TicToe game panel.

M. Haverbier
  • 383
  • 2
  • 13
  • My requirement is it opens another window and closes previous one how do i do that? – Vaibhav Setia Jul 19 '17 at 06:34
  • You could iomplement a main window and a second game window. So if you close the game window, the main window is still in place and you can create a new game window from it. – M. Haverbier Jul 19 '17 at 06:49