Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing my JFrame & JPanel in a new game

I have a simple JPanel for tic-tac-toe, drawing lines... so class TTT extends JPanel, and holds a GameLogic object inside.

all is good, application is a JFrame in Main adding TTT and all good.

BUT, when i want to restart a new game,
I call "restart" in my TTT which basiclly does : gameLogic = new GameLogic(); & repaint();

now my data array is clean and it should paint only lines..

any way Windows is not changed at all.

I've tried everything with no luck. Any suggestions?

GameBoard.java:

public class GameBoard extends javax.swing.JPanel {
private GameBoardLogic GameLogic;

//.....

public void Restart()
{
    GameLogic = new GameBoardLogic();
    removeAll();
    repaint();
}

Main.java:

public class Main {
private static GameBoard TTT;
private static JFrame application;

public static void main(String[] args) {

    application = new JFrame("Tic-Tac-Tow");
    TTT = new GameBoard();
    application.add(TTT);
    application.setSize(350, 350);
    application.setVisible(true);
    //.....

    if ( JOptionPane.showConfirmDialog(null, "Do you want to play again?") == 
                                                 JOptionPane.YES_OPTION )
    {
        application.removeAll();
        TTT.Restart();
        application.add(TTT);
        application.validate();
     }
like image 540
zaxy78 Avatar asked Dec 09 '22 00:12

zaxy78


1 Answers

This works for me

panel.removeAll();
//add your elements
revalidate();
repaint();
like image 91
Petr Mensik Avatar answered Dec 28 '22 18:12

Petr Mensik