Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:54:52+00:00 2026-05-27T14:54:52+00:00

I am trying to create a memory game in Java. Something like this, but

  • 0

I am trying to create a memory game in Java. Something like this, but much more simplier -> http://www.zefrank.com/memory/

Here is my code:

import javax.swing.*;

public class Memoriin {

    public static void main(String[] args) {
        JFrame frame = new MemoriinFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

And:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MemoriinFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    public static final int DEFAULT_HEIGHT = 600;
    public static final int DEFAULT_WIDTH = 800;
    public JButton button[] = new JButton[8];
    ArrayList<ImageIcon> icons = new ArrayList<ImageIcon>();
    ImageIcon tail = new ImageIcon("foto.jpg");

    ImageIcon photo1 = new ImageIcon("foto1.jpg");
    ImageIcon photo2 = new ImageIcon("foto2.jpg");
    ImageIcon photo3 = new ImageIcon("foto3.jpg");
    ImageIcon photo4 = new ImageIcon("foto4.jpg");
    ImageIcon photo1copy = photo1;
    ImageIcon photo2copy = photo2;
    ImageIcon photo3copy = photo3;
    ImageIcon photo4copy = photo4;



    public MemoriinFrame() {
          setTitle("Memory Game");
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
          setLayout(new GridLayout(2, 4));

          addIcons();
          for(int i = 0; i <= 7; i++) {
              button[i] = new JButton();
              button[i].setIcon(tail);
              button[i].addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      performActionEventHandler();
                  }
              });
              add(button[i]);
          }

    }

    public void performActionEventHandler() {
        // how can I link each button with a specific picture?
    }

    public void addIcons() {
        icons.add(photo1);
        icons.add(photo2);
        icons.add(photo3);
        icons.add(photo4);
        icons.add(photo1copy);
        icons.add(photo2copy);
        icons.add(photo3copy);
        icons.add(photo4copy);
        Collections.shuffle(icons);
    }

    public void tailToImage(JButton button) {
        button.setIcon(icons.get(0));
        icons.remove(0);
    }
}

So, I am trying to link a button with a specific picture. I tried to do that, but I had an unnecessary result: if I click a button then picture changes to a random picture. But I have 8 buttons and 8 pictures and I want to link them, so that each button goes with the same picture all game long.

P.S. English is not my native language.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:54:53+00:00Added an answer on May 27, 2026 at 2:54 pm

    In order to associate a button and a Picture, it is wiser to have a Mapping between them. You can use something like.

    Map<JButton, ImageIcon>
    

    Now the above is a very crude relation between a button and an Icon. You may have to improvise on this. Something like this..

    Image source : For foto1 through foto4 I took the avatar of the top 4 users from Stackoverflow.

    enter image description here

    ImageIcon photo1 = new ImageIcon("foto1.jpg");
    ImageIcon photo2 = new ImageIcon("foto2.jpg");
    ImageIcon photo3 = new ImageIcon("foto3.jpg");
    ImageIcon photo4 = new ImageIcon("foto4.jpg");
    ImageIcon photo1copy = new ImageIcon("foto1.jpg");
    ImageIcon photo2copy = new ImageIcon("foto2.jpg");
    ImageIcon photo3copy = new ImageIcon("foto3.jpg");
    ImageIcon photo4copy = new ImageIcon("foto4.jpg");
    
    Map<JButton, ImageIcon> buttonImage = new HashMap<JButton, ImageIcon>();
    
    public MemoriinFrame() {
          setTitle("Memory Game");
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
          setLayout(new GridLayout(2, 4));
    
          for(int i = 0; i <= 7; i++) {
    
              button[i] = new JButton();
              button[i].setIcon(tail);
              button[i].addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      performActionEventHandler((JButton)e.getSource());
                  }
              });
              add(button[i]);
          }
    
          addIcons();
    
    }
    
    public void performActionEventHandler(JButton clickedButton) {
        clickedButton.setIcon(buttonImage.get(clickedButton));
    }
    
    public void addIcons() {
        icons.add(photo1);
        icons.add(photo2);
        icons.add(photo3);
        icons.add(photo4);
        icons.add(photo1copy);
        icons.add(photo2copy);
        icons.add(photo3copy);
        icons.add(photo4copy);
        Collections.shuffle(icons);
    
        for(int i=0;i<icons.size();i++){
            buttonImage.put(button[i], icons.get(i));
        }
    }
    

    NOTE : This is not a COMPLETE bug free answer since I was just playing with it. And it has a lot of scope to be refactored. But this should be very much enough to get you going.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a simple game, but I can't find a certain memory
I was trying to create 2D array in a continuous memory block, but it
I'm trying to create a memory game. I've looked at the Android tutorials for
So I am trying to create a Memory Management System. In order to do
Heyy Everybody! I am trying to create a memory management system, so that a
I am trying to create an offline registry in memory using the offreg.dll provided
I'm trying to avoid the memory leak in PHP. When I create a object
Trying to create a user account in a test. But getting a Object reference
I am trying to create an offline registry in memory using the offreg.dll provided
I'm trying to create integration tests using hsqldb in an in memory mode. At

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.