Greetings again forum goers!

I have another question abound with plenty of obvious nooble errors I'm sure.

I am making a memory game(You know the old game where you turn over cards and try to match two in a row, and memorize previous locations etc.
) and this is the first time using Javax.swing for me. So I wanted to try and randomize the game each time you play. Thing is, I think I'm using the actionListener, or the actionPerformed calls wrong, or maybe I'm using my constrctor poorly. It isn't making much sense to me. It also could be something having to do with Java's swing GUI calling things, that I myself don't actually invoke(The stuff that happens behind the scenes) though I doubt it is that.

So after that long winded explanation here is the problem:

Each time I click a button on this game, the image changes to another randomized image, I don't want that to happen. I want the images to be randomized once, and then not be randomized again until a new game is called up, or the game is exited and reentered.

Here is the code I have..Hope its not too confusing.
(Ignore the sound stuff, I will be putting that in the game later, but wanted to get the basic construct for it set up)

memGame.java file is as follows:

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.sound.midi.*;

/**
 *
 * @author  *********
 * @version *********
 */
public class memGame extends JFrame
{
    MyButton gmButtons[] = new MyButton[16];
    
    // Image files
    ImageIcon img1 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\1UPmushCard.gif");
    ImageIcon img2 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\cloudCard.gif");
    ImageIcon img3 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\flowerCard.gif");
    ImageIcon img4 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\goombaCard.gif");
    ImageIcon img5 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\luigiCard.gif");
    ImageIcon img6 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\marioCard.gif");
    ImageIcon img7 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\mushRegCard.gif");
    ImageIcon img8 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\starCard.gif");
    ImageIcon guess = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\unTurnedCard.gif");
    ImageIcon vals[] = new ImageIcon[16];
    
    /*Sound files
    File songA = new File("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\SoundsfromMario\\charSel.mid");
    File songB = new File("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\SoundsfromMario\\hammerinAway.mid");
    File songC = new File("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\SoundsfromMario\\memGameMain.mid"); 
    boolean soundOn;
    */
    
    int turnNumber = 0;
    boolean Game;
    
    /** Creates a new instance of memGame */
    public memGame() 
    {
        super("Mario Memory Game");
        randomize();
        Container win = getContentPane();
        JPanel mp2 = new JPanel();
        JLabel turnCount = new JLabel("Number of Turns: ");
        JPanel mp = new JPanel();
        
        /* Setting up the background music for game
        JButton playSound = new JButton("Music On");
        JButton stopSound = new JButton("Music Off");
        
        playSound.addActionListener(new ActionListener()
                                    {
                                        public void actionPerformed(ActionEvent e)
                                        {
                                            soundOn = true;
                                            setSound(songA,soundOn);
                                        }
                                    });
        stopSound.addActionListener(new ActionListener()
                                    {
                                        public void actionPerformed(ActionEvent e)
                                        {
                                            soundOn = false;
                                            setSound(songB,soundOn);
                                        }
                                    });
         */
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setSize(400,550);
        win.setLayout(new BorderLayout());
        
        mp.setLayout(new GridLayout(4,4,8,8));
        mp.setBackground(Color.darkGray);
        win.add(mp,BorderLayout.CENTER);
        
        makeMenu();

        mp2.setLayout(new FlowLayout());
        //mp2.add(playSound);
        //mp2.add(stopSound);
        mp2.add(turnCount);
        win.add(mp2,BorderLayout.SOUTH);
        
        //randomize();
        
        for(int i=0; i < gmButtons.length; i++)
        {
            gmButtons[i] = new MyButton(i);
            mp.add(gmButtons[i]);
            gmButtons[i].setIcon(guess);
            gmButtons[i].addActionListener(gmButtons[i]);
        }
    }
    
    public void makeMenu()
    {
        JMenuBar menuBar = new JMenuBar();
        JMenu gMenu = new JMenu("Game");
        JMenu gMenu2 = new JMenu("Extra");
        //JMenu gMenu3 = new JMenu("Music");
        
        JMenuItem newG = new JMenuItem("New Game");
        newG.addActionListener(new ActionListener()
                               {
                                   public void actionPerformed(ActionEvent e)
                                   {
                                       turnNumber = 0;
                                       randomize();
                                       for(int i=0; i < gmButtons.length; i++)
                                       {
                                           gmButtons[i].setIcon(guess);
                                       }
                                   }
                               });
        JMenuItem quit = new JMenuItem("Quit Game");
        quit.addActionListener(new ActionListener()
                               {
                                   public void actionPerformed(ActionEvent e)
                                   {
                                       System.out.println("Exit command executed...");
                                       System.exit(0);
                                   }
                               });
        JMenuItem help = new JMenuItem("Help");
        help.addActionListener(new ActionListener()
                               {
                                   public void actionPerformed(ActionEvent e)
                                   {
                                       JOptionPane helpMe = new JOptionPane();
                                       
                                       helpMe.showMessageDialog(memGame.this,"Object of the Game:\nTry to use memory " +
                                                                "and some guessing to match each card with " +
                                                                "its partner.\nThe lower the number of turns the " +
                                                                "better you are at the memory Game!\n" +
                                                                "\nMenu Features:\n" +
                                                                "Selecting \"New Game\" from the menu starts over\n" +
                                                                "Quit of course Quits the current game window.", "Mario Memory Game Help",3);
                                   }
                               });
        JMenuItem about = new JMenuItem("About");
        about.addActionListener(new ActionListener()
                               {
                                   public void actionPerformed(ActionEvent e)
                                   {
                                       JOptionPane abt = new JOptionPane();
                                       
                                       abt.showMessageDialog(memGame.this,"Title: Mario Memory Game\n" +
                                                                          "Author: \n" +
                                                                          "Class: \n" +
                                                                          "\nMario is a registered TradeMark of " +
                                                                          "the Nintendo Corporation\n" +
                                                                          "This product intended for personal use only!\n" +
                                                                          "\nAny reproduction or distribution of this product " +
                                                                          "for monetary\nor commercial use is strictly " +
                                                                          "prohibited, due to the nature of content", "About The Author",1);
                                   }
                               });
         
        /*Song menu
                               
        JCheckBoxMenuItem song1 = new JCheckBoxMenuItem("Song 1");
        song1.addActionListener(new ActionListener()
                               {
                                   public void actionPerformed(ActionEvent e)
                                   {
                                       if(soundOn == true)
                                       {
                                           setSound(songA,soundOn);
                                       }
                                   }
                               });
                               
        JCheckBoxMenuItem song2 = new JCheckBoxMenuItem("Song 2");
        song2.addActionListener(new ActionListener()
                               {
                                   public void actionPerformed(ActionEvent e)
                                   {
                                       if(soundOn == true)
                                       {
                                           setSound(songB,soundOn);
                                       }
                                   }
                               });
                               
        JCheckBoxMenuItem song3 = new JCheckBoxMenuItem("Song 3");
        song3.addActionListener(new ActionListener()
                               {
                                   public void actionPerformed(ActionEvent e)
                                   {
                                       if(soundOn == true)
                                       {
                                           setSound(songC,soundOn);
                                       }
                                   }
                               });                      
                               
        gMenu3.add(song1);
        gMenu3.add(song2);
        gMenu3.add(song3);
        */
                               
        gMenu.add(newG);
        gMenu.add(quit);
        gMenu2.add(help);
        gMenu2.add(about);
        
        menuBar.add(gMenu);
        menuBar.add(gMenu2);
        //menuBar.add(gMenu3);
        setJMenuBar(menuBar);
    }
    
    public void randomize()
    {
        int j = 0;
        ImageIcon temp;
        
        vals[0] = img1;
        vals[1] = img1;
        vals[2] = img2;
        vals[3] = img2;
        vals[4] = img3;
        vals[5] = img3;
        vals[6] = img4;
        vals[7] = img4;
        vals[8] = img5;
        vals[9] = img5;
        vals[10] = img6;
        vals[11] = img6;
        vals[12] = img7;
        vals[13] = img7;
        vals[14] = img8;
        vals[15] = img8;
        
        for(int i = 0 ; i < 16; i++)
        {
            j = (int)Math.floor(Math.random() * 16);
            temp = vals[i];
            vals[i] = vals[j];
            vals[j] = temp;
        }
    }
    
    public ImageIcon[] getVals()
    {
        return vals;
    }
}

And here is the MyButton.java

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

/**
 *
 * @author  **********
 * @version **********
 */
public class MyButton extends JButton implements ActionListener
{
    private int position = 0;
    private Icon theIm;
    
    public MyButton()
    {
        super();
        position = 0;
    }
    
    public MyButton(int pos) 
    {
        super();
        position  = pos;
    }
    public void setPos(int pos)
    {
        position = pos;
    }
    public int getPos()
    {
        return position;
    }
    
    public void changeIcon(ImageIcon vals[])
    {
        
    }
    public void actionPerformed(ActionEvent e) 
    {
        memGame mG = new memGame();
        ImageIcon vals[] = mG.getVals();
        MyButton press = new MyButton();
        press = (MyButton)e.getSource();
        position = press.getPos();
        System.out.println("Position is:" + position);
        System.out.println("Val at that position is: " + (Object)(vals[position]).toString() );
        press.setIcon(vals[position]);
    }
    
}

So if anyone could help me out, I know it's a lot of code there, but If anyone could pinpoint my problem I would be quite grateful

public class memGame extends JFrame
{
    /** Creates a new instance of memGame */
    public memGame() 
    {
        super("Mario Memory Game");
        randomize();
    }

And here is the MyButton.java

public class MyButton extends JButton implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
        memGame mG = new memGame();
        ImageIcon vals[] = mG.getVals();
    }
}

Right here is your problem. Everytime a button is pressed, a new memGame is created and everytime a new memGame is created values are randomized. DOH.

There are lots of other issues too. I know your starting out, but there are some kind of basic programming mistakes. For instance in your Button class, its creating a new button everytime it is clicked. If you're a button why would you want to make a new button everytime you're clicked?

PM me and i will send you some sample code that i creaded that involves Cards, Decks and ToggleButtons. It isn't finished, but hopefully it will send you in the right direction.

Regards,

Nate

Right here is your problem. Everytime a button is pressed, a new memGame is created and everytime a new memGame is created values are randomized. DOH.

There are lots of other issues too. I know your starting out, but there are some kind of basic programming mistakes. For instance in your Button class, its creating a new button everytime it is clicked. If you're a button why would you want to make a new button everytime you're clicked?

PM me and i will send you some sample code that i creaded that involves Cards, Decks and ToggleButtons. It isn't finished, but hopefully it will send you in the right direction.

Regards,

Nate

Nate you rule!

I actually saw that error too after an extended period of staring at it last night, and I put the MyButton class as a subclass of memGame and that solved it. I also found out, I could have called memGame.this as a parameter to the MyButton constructor, and that would have gotten memGames data members in there, without having to call a new constructor of it. Regardless, I fixed the problem, with the former choice.

I want to say thanks so much for your help. I know how much of a pain in the butt it can be to read someone else's code...especially if not so fantastically written. I actually may PM you soon to still get that code, cause I could always use a helping hand, but for now I actually have to go to class...grr.

Thanks again!

This article has been dead for over six months. Start a new discussion instead.