0 Replies Latest reply: Sep 6, 2013 11:49 AM by 803787 RSS

    Toggle JButton Visibility

    803787

      Hi all,

       

      I am trying to do a very basic memory game where I have 2 players competing at the same time.

      What I did is that I created a 2D Jbutton array for the images and another 2D Jbutton array for the back card. When a card is clicked the back card is set to visibility false and the image to visibility true.

      My problem is that this works perfectly for the 1st image clicked and then for the second image it just doesn't show.

      Can anyone help me? I tried to revalidate the jframe but all for nothing. This is my code:

       

      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      import java.awt.geom.*;
      import java.util.*;
      public class Grid extends JFrame implements ActionListener{
          JPanel pane = new JPanel(null);
          int col;
          int row;
          ImageIcon images[];
          JButton player1cards[][];
          JButton player2cards[][];
          JButton player1Buttons[][];
          JButton player2Buttons[][];
          int player1counter = 0;
          int player2counter = 0;
          int player1score = 0;
          int player2score = 0;
          String p1chosen1;
          String p1chosen2;
          String p2chosen1;
          String p2chosen2;
          String game;
          int level;
          JLabel pl1score;
          JLabel pl2score;
          boolean p1GuessedPics[][];
          boolean p2GuessedPics[][];
           
          public Grid(int rows, int columns, int level, String game){
              super("Level "+level);
              setLocationRelativeTo(null); 
              setSize(1000,700);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setResizable(false);
              pl1score = new JLabel("Score: "+player1score);
              pl2score = new JLabel("Score: "+player2score);
              this.game = game;
              this.level = level;
              p1GuessedPics = new boolean[rows][columns];
              p2GuessedPics = new boolean[rows][columns];
              setGuessedArrays(p1GuessedPics);
              setGuessedArrays(p2GuessedPics);
              
              images = new ImageIcon[rows*columns];
              for(int i = 0; i<(images.length/2); i++){
                  images[i] = new ImageIcon("Game\\"+game+"\\L"+level+"\\"+i+".jpg");
              }
              
              int c =0;
              for(int i = (images.length/2); i<images.length; i++){
                  images[i] = new ImageIcon("Game\\"+game+"\\L"+level+"\\"+(c+""+c)+".jpg");
                  c++;
              }
              col = columns;
              row = rows;
              player1cards = new JButton[rows][columns];
              player2cards = new JButton[rows][columns];
              fillImageArray(player1cards);
              fillImageArray(player2cards);
              setArrayBounds(player1cards, 0, 0);
              setArrayBounds(player2cards, ((col*120)+5), 0); 
              hideArray(player1cards);
              hideArray(player2cards);
              
               player1Buttons = new JButton[rows][columns];
               player2Buttons = new JButton[rows][columns];
              fillButtonArray(player1Buttons);
              fillButtonArray(player2Buttons);
              setArrayBounds(player1Buttons, 0, 0);
              setArrayBounds(player2Buttons, ((col*120)+5), 0);   
              
              
              pl1score.setBounds(5,600,50,100);
              pane.add(pl1score);
              pl2score.setBounds(((col*120)+10),600,50,100);
              pane.add(pl2score);
              
              this.add(pane);
              setVisible(true);
             
          }
          
          public void setGuessedArrays(boolean guessed[][]){
              for(int r = 0; r<row; r++){           
                for(int c= 0; c<col; c=c+1){
                    guessed[r][c] = false;
                  }
              }
          }
          public void hideArray(JButton arrayImages[][]){
               for(int r = 0; r<row; r++){           
                for(int c= 0; c<col; c=c+1){
                       arrayImages[r][c].setVisible(false);
                       
                }
              }        
          }
          public void fillButtonArray(JButton arrayButton[][]){
              ImageIcon backCard = new ImageIcon("Game\\Back\\back.jpg");
              for(int r = 0; r<arrayButton.length; r++){
                  for(int c = 0; c<arrayButton[r].length; c++){
                      arrayButton[r][c] = new JButton(backCard);
                  }
              }
          }
          
          public void fillImageArray(JButton arrayImages[][]){
              images = shuffleArray(images);
              int i = 0;
              for(int r = 0; r<arrayImages.length; r++){
                  for(int c = 0; c<arrayImages[r].length; c++){
                      arrayImages[r][c] = new JButton(images[i]);
                      i++;
                  }
              }
          }
          
        
          public static ImageIcon[] shuffleArray(ImageIcon[] shuffledArray)
          {
              Random rnd = new Random();
              for (int i = shuffledArray.length - 1; i > 0; i--)
              {
                  int index = rnd.nextInt(i + 1);
                  // Simple swap
                  ImageIcon a = shuffledArray[index];
                  shuffledArray[index] = shuffledArray[i];
                  shuffledArray[i] = a;
              }
              return shuffledArray;
          }
          
           
          public void setArrayBounds(JButton arrayButton[][], int wantedX, int wantedY){
              int x = wantedX;
              int y = wantedY;
              int w = 120;
              int h = 160; 
              int length = arrayButton.length;
      
      
              for(int r = 0; r<row; r++){           
                for(int c= 0; c<col; c=c+1){
                        arrayButton[r][c].setBounds(x,y,w,h);
                        x=x+120;
                        pane.add(arrayButton[r][c]);
                        arrayButton[r][c].addActionListener(this);
                       
                }
                y=y+160;
                x=wantedX;
              }          
          }
          
          public void paint(Graphics g) {
              super.paint(g);
              Graphics2D g2 = (Graphics2D) g;
              Line2D lin = new Line2D.Float(((col*120)+5), 0, ((col*120)+5),700);
               g2.setStroke(new BasicStroke(5));
              g2.draw(lin);
      
      
          }
          
         
          public String fixName(String name){
              String fixed = name.replace("Game\\"+game+"\\L"+level+"\\", "");
              fixed = fixed.replace(".jpg","");
              return fixed;
          }
          
          public void redrawCards(int player){
              try{
                  Thread.sleep(3000);
              if(player == 1){
                  for(int r = 0; r<row; r++){
                      for(int c = 0; c<col; c++){
                          if(p1GuessedPics[r][c] == false){
                              player1Buttons[r][c].setVisible(true);
                              player1cards[r][c].setVisible(false);
                          }
                      }
                  }
              }else{
                    for(int r = 0; r<row; r++){
                      for(int c = 0; c<col; c++){
                          if(p2GuessedPics[r][c] == false){
                              player2Buttons[r][c].setVisible(true);
                              player2cards[r][c].setVisible(false);
                          }
                      }
                  }
              }
          }catch(InterruptedException ex) {
          Thread.currentThread().interrupt();
      }
          }
                  
          public void showCard(int player, int r, int c) {
              
              if(player == 1){
                           
                           player1Buttons[r][c].setVisible(false);
                           player1cards[r][c].setVisible(true);
                           
                           pane.validate();
                  pane.repaint();
                  repaint();
                           
                          }else{
                              player2Buttons[r][c].setVisible(false);
                            player2cards[r][c].setVisible(true);
                          }
                      }
      
      
                      
                      
           public void actionPerformed(ActionEvent ae) {
               JButton jb = (JButton)ae.getSource();
               boolean found = false;
               int p1g1r = -1;
               int p1g2r = -1;
               int p1g1c = -1;
               int p1g2c = -1;
               for(int r = 0; (r<row) && !found; r++){
                for(int c= 0; (c<col) && !found; c=c+1){
                        if(jb==player1Buttons[r][c] && player1counter!=2){
                            showCard(1, r, c);
                            
                            found = true;
                            
                            if(player1counter == 0){
                             p1chosen1 = ((ImageIcon) player1cards[r][c].getIcon()).getDescription();
                             p1chosen1 = fixName(p1chosen1);
                              System.out.println(p1chosen1);
                              p1g1r = r;
                             p1g1c = c;
                              
                            }else{
                             p1chosen2 = ((ImageIcon) player1cards[r][c].getIcon()).getDescription();
                              p1chosen2 = fixName(p1chosen2);
                               System.out.println(p1chosen2);
                               p1g2r = r;
                               p1g2c = c;
                            }
                            player1counter++;
                          }
                          if(player1counter == 2){
                              if(p1chosen1.charAt(0)==(p1chosen2.charAt(0))){
                                  player1score++;
                                  pl1score.setText("Score: "+player1score);
                                  p1GuessedPics[p1g1r][p1g1c] = true;
                                  p1GuessedPics[p1g2r][p1g2c] = true;
                                  player1counter = 0;
                                  
                              }else{
                                  redrawCards(1);
                                  player1counter = 0;
                               
                              }
                          
                        if(jb==player2Buttons[r][c]&& player2counter!=2){
                            player2Buttons[r][c].setVisible(false);
                            player2cards[r][c].setVisible(true);
                            found = true;
                            if(player2counter == 0){
                              p2chosen1 = ((ImageIcon) player2cards[r][c].getIcon()).getDescription();
                              p2chosen1 = fixName(p2chosen1);
                              p2GuessedPics[r][c] = true;
                            }else{
                              p2chosen2 = ((ImageIcon) player2cards[r][c].getIcon()).getDescription();
                              p2chosen2 = fixName(p2chosen2);
                              p2GuessedPics[r][c] = true;
                            }
                            player2counter++;
                          }
           }
      }
      }
      }
      }