Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- PLEASE read the instructions carefully -I have included the code for the progr

ID: 3767650 • Letter: #

Question

- PLEASE read the instructions carefully
-I have included the code for the program that i have created.. It is a memory game where all images buttons are a picture that are not showing.. to play the game you need to select 2 buttons and see if those 2 buttons match.. if they match they stay facing up, if they do not match they need to be flipped back over. currently they do not flip back over if they do not match.. this is what i need help with .. in the program i have links to pictures that i have not included in this file.. please dont worry about that as this is not important for the helping with the code that i need.

** what i need
-I need code that will only display 2 images at any time, a third image can not be displayed.. If a match occurs, the items are disabled and cannot be flipped over again keeping the images visible.. ** IF NO match occurs the images are flipped back over ( or to the gray button color) AUTOMATICALLY using a timer

import java.awt.Color;
import java.awt.EventQueue;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MemoryApp {

   private JFrame frame;

   /**
   * Launch the application.
   */
   public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           public void run() {
               try {
                   MemoryApp window = new MemoryApp();
                   window.frame.setVisible(true);
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       });
   }

   /**
   * Create the application.
   */
   public MemoryApp() {
       initialize();
   }

   /**
   * Initialize the contents of the frame.
   */
   private void initialize() {
       frame = new JFrame();
       frame.setBounds(100, 100, 1054, 740);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.getContentPane().setLayout(null);
      
       //creating a ImageIcon array to hold the pictures that will be placed as image icons into buttons
       ImageIcon[] words = new ImageIcon[12];
       words[0] = new ImageIcon(getClass().getResource("drum.png"));
       words[1] = new ImageIcon(getClass().getResource("egg.png"));
       words[2] = new ImageIcon(getClass().getResource("hotdog.png"));      
       words[3] = new ImageIcon(getClass().getResource("kang.png"));
       words[4] = new ImageIcon(getClass().getResource("ladyclip.png"));
       words[5] = new ImageIcon(getClass().getResource("oldman.png"));
       words[6] = new ImageIcon(getClass().getResource("drum2.png"));      
       words[7] = new ImageIcon(getClass().getResource("egg2.png"));
       words[8] = new ImageIcon(getClass().getResource("hotdog2.png"));
       words[9] = new ImageIcon(getClass().getResource("kang2.png"));      
       words[10] = new ImageIcon(getClass().getResource("ladyclip2.png"));
       words[11] = new ImageIcon(getClass().getResource("oldman2.png"));
      
       //Create a random way of finding the random pics
       Random random = new Random();
      
       ImageIcon randomWords[] = new ImageIcon[12];
       //loop for the random parts
       for (int i = 0; i < 12; i++) {

   while (true) {

   // Check if the word already exists in the randomWords
   /**
   * here i needed to create an Image icon that would check to see if that
   * image was already used using a while loop
   */
       ImageIcon randomString = words[random.nextInt(12)];

   boolean isExist = false;
   for (int j = 0; j <= i; j++) {

   if (randomWords[j] != null
   && randomWords[j].equals(randomString)) {
   isExist = true;
   }
   }

   if (isExist) {
   // If exist continue get another random number
   continue;
   } else {
   randomWords[i] = randomString;
   break;
   }
   }

   }
  
      
      
       JButton btnNewButton = new JButton("");
       btnNewButton.setBackground(Color.GRAY);
       btnNewButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (btnNewButton.getBackground().equals(Color.GRAY))
               {
                   btnNewButton.setBackground(Color.BLACK);
                   btnNewButton.setIcon(randomWords[0]);
               }
                  
                   else
                   {
                       btnNewButton.setBackground(Color.GRAY);
                       btnNewButton.setIcon(null);
                   }
           }
       });
       btnNewButton.setBounds(10, 11, 168, 135);
       frame.getContentPane().add(btnNewButton);
      
       JButton button = new JButton("");
       button.setBackground(Color.GRAY);
       button.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (button.getBackground().equals(Color.GRAY))
               {
                   button.setBackground(Color.BLACK);
                   button.setIcon(randomWords[1]);
               }
                  
                   else
                   {
                       button.setBackground(Color.GRAY);
                       button.setIcon(null);
                   }

                  
           }
          
       });
       button.setBounds(188, 11, 168, 135);
       frame.getContentPane().add(button);
      
       JButton button_1 = new JButton("");
       button_1.setBackground(Color.GRAY);
       button_1.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (button_1.getBackground().equals(Color.GRAY))
               {
                   button_1.setBackground(Color.BLACK);
                   button_1.setIcon(randomWords[2]);
               }
                  
                   else
                   {
                       button_1.setBackground(Color.GRAY);
                       button_1.setIcon(null);
                   }
           }
       });
       button_1.setBounds(366, 11, 168, 135);
       frame.getContentPane().add(button_1);
      
       JButton button_2 = new JButton("");
       button_2.setBackground(Color.GRAY);
       button_2.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (button_2.getBackground().equals(Color.GRAY))
               {
                   button_2.setBackground(Color.BLACK);
                   button_2.setIcon(randomWords[3]);
               }
                  
                   else
                   {
                       button_2.setBackground(Color.GRAY);
                       button_2.setIcon(null);
                   }
              
           }
       });
       button_2.setBounds(544, 11, 168, 135);
       frame.getContentPane().add(button_2);
      
       JButton button_3 = new JButton("");
       button_3.setBackground(Color.GRAY);
       button_3.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (button_3.getBackground().equals(Color.GRAY))
               {
                   button_3.setBackground(Color.BLACK);
                   button_3.setIcon(randomWords[4]);
               }
                  
                   else
                   {
                       button_3.setBackground(Color.GRAY);
                       button_3.setIcon(null);
                   }
           }
       });
       button_3.setBounds(10, 157, 168, 135);
       frame.getContentPane().add(button_3);
      
       JButton button_4 = new JButton("");
       button_4.setBackground(Color.GRAY);
       button_4.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if ( button_4 .getBackground().equals(Color.GRAY))
               {
                   button_4 .setBackground(Color.BLACK);
                   button_4 .setIcon(randomWords[5]);
               }
                  
                   else
                   {
                       button_4 .setBackground(Color.GRAY);
                       button_4 .setIcon(null);
                   }
           }
       });
       button_4.setBounds(188, 157, 168, 135);
       frame.getContentPane().add(button_4);
      
       JButton button_5 = new JButton("");
       button_5.setBackground(Color.GRAY);
       button_5.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (button_5 .getBackground().equals(Color.GRAY))
               {
                   button_5 .setBackground(Color.BLACK);
                   button_5 .setIcon(randomWords[6]);
               }
                  
                   else
                   {
                       button_5 .setBackground(Color.GRAY);
                       button_5 .setIcon(null);
                   }
           }
       });
       button_5.setBounds(366, 157, 168, 135);
       frame.getContentPane().add(button_5);
      
       JButton button_6 = new JButton("");
       button_6.setBackground(Color.GRAY);
       button_6.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (button_6 .getBackground().equals(Color.GRAY))
               {
                   button_6 .setBackground(Color.BLACK);
                   button_6 .setIcon(randomWords[7]);
               }
                  
                   else
                   {
                       button_6 .setBackground(Color.GRAY);
                       button_6 .setIcon(null);
                   }
           }
       });
       button_6.setBounds(544, 157, 168, 135);
       frame.getContentPane().add(button_6);
      
       JButton button_7 = new JButton("");
       button_7.setBackground(Color.GRAY);
       button_7.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if ( button_7 .getBackground().equals(Color.GRAY))
               {
                   button_7.setBackground(Color.BLACK);
                   button_7.setIcon(randomWords[8]);
               }
                  
                   else
                   {
                       button_7 .setBackground(Color.GRAY);
                       button_7.setIcon(null);
                   }
           }
       });
       button_7.setBounds(10, 303, 168, 135);
       frame.getContentPane().add(button_7);
      
       JButton button_8 = new JButton("");
       button_8 .setBackground(Color.GRAY);
       button_8.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if ( button_8 .getBackground().equals(Color.GRAY))
               {
                   button_8.setBackground(Color.BLACK);
                   button_8.setIcon(randomWords[9]);
               }
                  
                   else
                   {
                       button_8.setBackground(Color.GRAY);
                       button_8.setIcon(null);
                   }
           }
       });
       button_8.setBounds(188, 303, 168, 135);
       frame.getContentPane().add(button_8);
      
       JButton button_9 = new JButton("");
       button_9 .setBackground(Color.GRAY);
       button_9.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if ( button_9 .getBackground().equals(Color.GRAY))
               {
                   button_9.setBackground(Color.BLACK);
                   button_9.setIcon(randomWords[10]);
               }
                  
                   else
                   {
                       button_9.setBackground(Color.GRAY);
                       button_9.setIcon(null);
                   }
           }
       });
       button_9.setBounds(366, 303, 168, 135);
       frame.getContentPane().add(button_9);
      
       JButton button_10 = new JButton("");
       button_10 .setBackground(Color.GRAY);
       button_10.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if (button_10 .getBackground().equals(Color.GRAY))
               {
                   button_10.setBackground(Color.BLACK);
                   button_10.setIcon(randomWords[11]);
               }
                  
                   else
                   {
                       button_10.setBackground(Color.GRAY);
                       button_10.setIcon(null);
                   }
           }
       });
       button_10.setBounds(544, 303, 168, 135);
       frame.getContentPane().add(button_10);
   }
}

Explanation / Answer

you have spread out the logic for the program in different blocks which is not ideal.

I have written a new implementation of the program:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;


public class MemoryApp extends JFrame {

static String files[] = {"1.JPG","2.JPG",
"3.JPG", "4.JPG",
"5.JPG", "6.JPG"};
static JButton buttons[];
ImageIcon closedIcon;
int numButtons;
ImageIcon icons[];
Timer myTimer;
  
int click_count = 0; // variable to count clicks in the program
int odd_click_count = 0; // variable to count odd numbre of click
int index = 0; // variable to get latest index of the elements.

public MemoryApp() {

setTitle("Memory Game");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new GridLayout(2, files.length));

closedIcon = new ImageIcon("closed.JPG");
numButtons = files.length * 2;
buttons = new JButton[numButtons];
icons = new ImageIcon[numButtons];
for (int i = 0, j = 0; i < files.length; i++) {
icons[j] = new ImageIcon(files[i]);
buttons[j] = new JButton("");
buttons[j].addActionListener(new ImageButtonListener());
buttons[j].setIcon(closedIcon);
add(buttons[j++]);

icons[j] = icons[j - 1];
buttons[j] = new JButton("");
buttons[j].addActionListener(new ImageButtonListener());
buttons[j].setIcon(closedIcon);
add(buttons[j++]);
}

Random gen = new Random();
for (int i = 0; i < numButtons; i++) {
int rand = gen.nextInt(numButtons);
ImageIcon temp = icons[i];
icons[i] = icons[rand];
icons[rand] = temp;
}

// Pack and display the window.
pack();
setVisible(true);

myTimer = new Timer(1000, new TimerListener());
// myTimer.start();
}

private class TimerListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
buttons[index].setIcon(closedIcon);
buttons[odd_click_count].setIcon(closedIcon);
myTimer.stop();
}
}

private class ImageButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
  
if (myTimer.isRunning())
return;
  
click_count++;
System.out.println(click_count);
  
for (int i = 0; i < numButtons; i++) {
if (e.getSource() == buttons[i]) {
buttons[i].setIcon(icons[i]);
index = i;
}
}
  
if (click_count % 2 == 0) {
if (index == odd_click_count) {
click_count--;
return;
}
if (icons[index] != icons[odd_click_count]) {
myTimer.start();
}
} else {
odd_click_count = index;
}
}
}

public static void main(String[] args) {
new MemoryApp();
}
}