0

i have a Jframe which has some panels as instance variables and one of the panels is a grid board (i am implementing Lines of action game). After my game ends I have a button "Play again" which i want to reinitialize my board panel. I tried a lot of things like removing my panel from the content pane and re-initializing it, but nothing worked so far. Here are some of the things i tried (i didn't try them all at once )

public class Frame extends JFrame implements  MouseListener{

JLabel l = new JLabel();
Panel1 Boards;
Panel2 newGame;
Panel3 winner;
Point lastCheckerSelected;
Board game = new Board();
    public Frame() {
    setResizable(false);
    setTitle("Lines Of Action");
    setBounds(290, 350, 1000, 700);
    setLayout(null);
   winner=new Panel3(); 
    winner.playAgain.addMouseListener(this);
    getContentPane().add(winner);

    Boards= new Panel1();

     getContentPane().add(Boards);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    l.setIcon(new ImageIcon(
            "E:\\background0213.jpg"));
    l.setBounds(0 ,0 ,  1000 , 700);
    getContentPane().add(l);
    validate(); 
    newGame=new Panel2();
    newGame.b.addMouseListener(this);
  getContentPane().add(newGame);

  for(int i=0;i<8;i++){
      for(int j=0;j<8;j++){
          Boards.x[i][j].addMouseListener(this);
          Boards.y[i][j].addMouseListener(this);
      }
  }

}
 public void mouseClicked(MouseEvent e) {
    if(e.getSource().equals(newGame.b)) {
           Boards.setVisible(true);
           newGame.setVisible(false);
           game=new Board();
     }
if(this.game.getWinner()==1) {
    winner.setVisible(true);
    winner.whiteWins.setVisible(true);
}
if(this.game.getWinner()==2) {
    winner.setVisible(true);
    winner.greywins.setVisible(true);
}
if(e.getSource().equals(winner.playAgain)) {
            //this.getContentPane().remove(Boards);
        //  this.game= new Board();                                          
        //  Boards = new Panel1();      
        //  this.getContentPane().add(Boards);
            //Boards.setVisible(true);
        //  validate();
        //  Boards.repaint();


        }
}

public static void main(String[] args) {
    Frame frame = new Frame();
    frame.setVisible(true);



}

I still cant make my new panel appear ( removing the Boards panel from the content pane makes it disappear which is good but the new one does not appear) here is my panel =1 that contains the board

public class Panel1 extends JPanel  {
JButton[][] x = new JButton[8][8];
JButton[][] y=new JButton[8][8];
Graphics g;
public Panel1() {
    setBounds(0, 30 ,400 ,400); 
    setLayout(new GridLayout(8, 8));
    setOpaque(false);
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            x[i][j] = new JButton();
            x[i][j].setSize(50, 50);
            if ((i % 2 == 0 && j % 2 == 0) || (i % 2 != 0 && j % 2 != 0)) {
                x[i][j].setBackground(Color.DARK_GRAY.darker());
            //  x[i][j].setBackground(new Color(111,89,81,150));
            }
            else {
                // Color.OPAQUE = 2;
                x[i][j].setBackground(Color.red.darker().darker());
            //  x[i][j].setBackground(new Color(223,37,32,150));
            }

            x[i][j].setEnabled(false);
            add(x[i][j]);
        }
    }
for(int i = 0; i < 8 ;i++){
    for(int j=0;j < 8;j++){
        y[i][j]=new JButton();
    x[i][j].add(y[i][j]);   
    //  y[i][j].setSize(100,100);

    //  y[i][j].setEnabled(false);
    //  y[i][j].setOpaque(false);
    //  y[i][j].setContentAreaFilled(false);
//      y[i][j].setBorderPainted(false);
        y[i][j].setVisible(false);

    }
}
for(int i=1;i<7;i++){
//  y[0][i].setOpaque(true);
    y[0][i].setBackground(Color.white);
    y[0][i].setEnabled(true);
    y[0][i].setVisible(true);
//  y[7][i].setOpaque(true);
    y[7][i].setBackground(Color.white);
    y[7][i].setEnabled(true);
    y[7][i].setVisible(true);

}
for(int i=1;i<7;i++){
//  y[i][0].setOpaque(true);
    y[i][0].setEnabled(true);
    y[i][0].setBackground(new Color(102,125,153));
    y[i][0].setVisible(true);
//  y[i][7].setOpaque(true);
    y[i][7].setEnabled(true);
    y[i][7].setBackground(new Color(102,125,153));
    y[i][7].setVisible(true);
}
//  addMouseListener(this);
    setVisible(false);
}




}

4 Answers4

1

Try these four together. Need to see more of your code to make sure this work.

if(e.getSource().equals(winner.playAgain)) {
    this.getContentPane().remove(Boards);
    Boards = new Panel1();
    this.getContentPane().add(Boards);
    this.invalidate();
    this.validate();
    this.repaint();
}
johnchen902
  • 9,531
  • 1
  • 27
  • 69
  • if there a difference between revalidate and validate ? i cannot type this.revalidate as it say its undefined for type frame oO and it doesnt work with validate :( it still removes the old board but i cannot see a new board so i can play again – user2227557 May 10 '13 at 14:45
  • 1
    You can (re-)validate a `JComponent`, such as `JPanel`, for [example](http://stackoverflow.com/a/5812981/230513). – trashgod May 10 '13 at 14:50
  • @user2227557 You're using `JFrame`? Then there are no `revalidate`. `invalidate` it and then `validate` it instead. Sometimes you need to add `repaint`. – johnchen902 May 10 '13 at 15:00
1

You could also try this:

if(e.getSource().equals(winner.playAgain)) 
{
   Boards.removeAll();
   revalidate();
   repaint();
}

I don't think you need to create a new Panel1 instance.

629
  • 308
  • 1
  • 6
  • 15
  • this removes my board also which is not what i really want :( – user2227557 May 10 '13 at 14:46
  • What components do you have in that JPanel? – 629 May 10 '13 at 14:49
  • public class Panel1 extends JPanel { JButton[][] x = new JButton[8][8]; JButton[][] y=new JButton[8][8]; – user2227557 May 10 '13 at 14:53
  • Only buttons? Don't you have listeners etc? Can you give us some clear code from `Panel1`? I am trying to suggest a method to manually reset your panel's state to its default. – 629 May 10 '13 at 14:58
  • ahhh i could manually reset it god-.- i will try dont know why i never though of that xD i was just focusing on reinitializing it and making it work – user2227557 May 10 '13 at 15:02
  • worked xD thx for the idea dont know how it didnt occur to me to just manually rebuild the board :D – user2227557 May 10 '13 at 15:10
1

i have a Jframe which has some panels as instance variables and one of the panels is a grid board (i am implementing Lines of action game). After my game ends I have a button "Play again" which i want to reinitialize my board panel. I tried a lot of things like removing my panel from the content pane and re-initializing it, but nothing worked so far.

I think that CardLayout is best of choices

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

After the components are made visible in the screen, if you remove and add components, then you have to call Component.repaint() or Component.validate() to call the repainting again. Do this inside your actionPerformed() of your playAgainButton()

K Adithyan
  • 386
  • 4
  • 12