Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,475 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,744 people online right now. Registration is fast and FREE... Join Now!




In need of help with java memory game

 

In need of help with java memory game

Zoofnick

4 Dec, 2008 - 03:38 AM
Post #1

New D.I.C Head
*

Joined: 4 Dec, 2008
Posts: 3

Hello, I've been trying to make a memory game (the classic match pairs one).

CODE


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


public class Memory extends JFrame implements ActionListener, EventListener{
    private JButton ab = new JButton ("Clicky");
    private JButton bc = new JButton ("Clicky");
    private JButton cd = new JButton ("Clicky");
    private JButton de = new JButton ("Clicky");
    private JButton ef = new JButton ("Clicky");
    private JButton gh = new JButton ("Clicky");
    private JButton hi = new JButton ("Clicky");
    private JButton ij = new JButton ("Clicky");
    private JButton jk = new JButton ("Clicky");
    
    private int[] num = new int [4];





    public Memory () {
        setLayout (new GridLayout (3,5));
        add (ab); add (bc); add (cd); add (ef); add (gh); add (hi); add (ij); add (jk);
                getContentPane() .setBackground(Color.blue);
                ab.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
                bc.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
                cd.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
                de.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
                ef.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
                fg.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
                gh.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
                hi.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
                ij.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));
                jk.setFont (new Font ("TimesNewRoman", Font.ITALIC, 16));

                ab.addActionListener (this);
                ab.addMouseListener  (this);
                bc.addActionListener (this);
                de.addActionListener (this);
                ef.addActionListener (this);
                fg.addActionListener (this);
                gh.addActionListener (this);
                hi.addActionListener (this);
                ij.addActionListener (this);
                jk.addActionListener (this);



                setSize (350,200);
                setVisible(true);
                setDefaultCloseOperation(EXIT_ON_CLOSE);





    }


public void actionPerformed(ActionEvent a) {

        if (a.getSource () == ab){

            ab.setIcon (new ImageIcon ("H:\\Mina bilder\\simon cowell.jpg"));
            ab.setText("");




}

        else if (a.getSource () == bc){

            Icon i1 = new ImageIcon ("H:\\Mina bilder\\simon cowell.jpg");

            bc.setIcon (i1);

            bc.setText("");



}




        else if (a.getSource () == cd){
        }

        else if (a.getSource () == de){
        }

        else if (a.getSource () == ef){
        }

        else if (a.getSource () == fg){
        }

        else if (a.getSource () == gh){
        }

        else if (a.getSource () == hi){
        }

        else if (a.getSource () == jk){
        }

        else if (a.getSource () == kl){



        }
    }

public static void main (String[] args){

Memory n = new Memory();

}

}



As you can see I've been making JLabels and adding action listeners to them. However I'm at a loss for how to get the program to recognize when two similar pictures have been selected and so on and so forth. Is this a valid approach or am I just way off?

Any help would be appreciated.

User is offlineProfile CardPM
+Quote Post


pbl

RE: In Need Of Help With Java Memory Game

4 Dec, 2008 - 04:07 PM
Post #2

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
Better you learn how to use arrays before going further: biggrin.gif

CODE

import java.awt. *;
import java.awt.event. *;

import javax.swing. *;


public class Memory extends JFrame implements ActionListener {

    private JButton[][] button;
    private ImageIcon icon = new ImageIcon ("H:\\Mina bilder\\simon cowell.jpg");

    public Memory () {
        setLayout (new GridLayout (3,5));

        button = new JButton[3][5];
        Font font = new Font ("TimesNewRoman", Font.ITALIC, 16);
        for(int j = 0; j < 5; j++) {
            for(int i = 0; i < 3; i++) {
                button[i][j] = new JButton("Cliky");
                button[i][j].setFont(font);
                add(button[i][j]);
                button[i][j].addActionListener(this);
            }
        }
        setSize (350,200);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


    public void actionPerformed(ActionEvent a) {

        Object o = a.getSource();
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 5; j++) {
                if(button[i][j] == o) {
                    button[i][j].setText("");
                    button[i][j].setIcon(icon);
                    button[i][j].setEnabled(false);
                }
            }
        }
    }

    public static void main (String[] args){

        Memory n = new Memory();

    }

}


After that we could talk about your other problem
User is offlineProfile CardPM
+Quote Post

Zoofnick

RE: In Need Of Help With Java Memory Game

5 Dec, 2008 - 03:05 AM
Post #3

New D.I.C Head
*

Joined: 4 Dec, 2008
Posts: 3

Wow, that was a much smarter way of going about it, alot less code. I understand most of what you have done but I'm not sure about how to proceed, how do I make the different buttons display different images and then match them against each other?

I'm sorry if I sound stupid but I have never quite done something like this before.

This post has been edited by Zoofnick: 5 Dec, 2008 - 03:06 AM
User is offlineProfile CardPM
+Quote Post

Zoofnick

RE: In Need Of Help With Java Memory Game

7 Dec, 2008 - 01:41 AM
Post #4

New D.I.C Head
*

Joined: 4 Dec, 2008
Posts: 3

Bump
User is offlineProfile CardPM
+Quote Post

pbl

RE: In Need Of Help With Java Memory Game

7 Dec, 2008 - 04:09 PM
Post #5

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
I guess you'll also need an array of Image the same size of your array of JButton
I would personally write my own class MyButton that extends JButton that will hold in its instance variables:
- the image to display
- if I am turned up or not

You will have to have a variable int nbImageDisplayed to know where you are in your turn (are you showing the first image or the second)

You will need JbuutonShowed[2] if both show the same image you can disable both of them
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:25AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month