944,037 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2025
  • Java RSS
Sep 15th, 2005
0

GUId Greif!

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import javax.sound.midi.*;
  8.  
  9. /**
  10.  *
  11.  * @author *********
  12.  * @version *********
  13.  */
  14. public class memGame extends JFrame
  15. {
  16. MyButton gmButtons[] = new MyButton[16];
  17.  
  18. // Image files
  19. ImageIcon img1 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\1UPmushCard.gif");
  20. ImageIcon img2 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\cloudCard.gif");
  21. ImageIcon img3 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\flowerCard.gif");
  22. ImageIcon img4 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\goombaCard.gif");
  23. ImageIcon img5 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\luigiCard.gif");
  24. ImageIcon img6 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\marioCard.gif");
  25. ImageIcon img7 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\mushRegCard.gif");
  26. ImageIcon img8 = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\starCard.gif");
  27. ImageIcon guess = new ImageIcon("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\MemoryGame\\unTurnedCard.gif");
  28. ImageIcon vals[] = new ImageIcon[16];
  29.  
  30. /*Sound files
  31.   File songA = new File("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\SoundsfromMario\\charSel.mid");
  32.   File songB = new File("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\SoundsfromMario\\hammerinAway.mid");
  33.   File songC = new File("C:\\Documents and Settings\\My Documents\\My eBooks\\CS340\\SoundsfromMario\\memGameMain.mid");
  34.   boolean soundOn;
  35.   */
  36.  
  37. int turnNumber = 0;
  38. boolean Game;
  39.  
  40. /** Creates a new instance of memGame */
  41. public memGame()
  42. {
  43. super("Mario Memory Game");
  44. randomize();
  45. Container win = getContentPane();
  46. JPanel mp2 = new JPanel();
  47. JLabel turnCount = new JLabel("Number of Turns: ");
  48. JPanel mp = new JPanel();
  49.  
  50. /* Setting up the background music for game
  51.   JButton playSound = new JButton("Music On");
  52.   JButton stopSound = new JButton("Music Off");
  53.  
  54.   playSound.addActionListener(new ActionListener()
  55.   {
  56.   public void actionPerformed(ActionEvent e)
  57.   {
  58.   soundOn = true;
  59.   setSound(songA,soundOn);
  60.   }
  61.   });
  62.   stopSound.addActionListener(new ActionListener()
  63.   {
  64.   public void actionPerformed(ActionEvent e)
  65.   {
  66.   soundOn = false;
  67.   setSound(songB,soundOn);
  68.   }
  69.   });
  70.   */
  71.  
  72. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  73. setResizable(false);
  74. setSize(400,550);
  75. win.setLayout(new BorderLayout());
  76.  
  77. mp.setLayout(new GridLayout(4,4,8,8));
  78. mp.setBackground(Color.darkGray);
  79. win.add(mp,BorderLayout.CENTER);
  80.  
  81. makeMenu();
  82.  
  83. mp2.setLayout(new FlowLayout());
  84. //mp2.add(playSound);
  85. //mp2.add(stopSound);
  86. mp2.add(turnCount);
  87. win.add(mp2,BorderLayout.SOUTH);
  88.  
  89. //randomize();
  90.  
  91. for(int i=0; i < gmButtons.length; i++)
  92. {
  93. gmButtons[i] = new MyButton(i);
  94. mp.add(gmButtons[i]);
  95. gmButtons[i].setIcon(guess);
  96. gmButtons[i].addActionListener(gmButtons[i]);
  97. }
  98. }
  99.  
  100. public void makeMenu()
  101. {
  102. JMenuBar menuBar = new JMenuBar();
  103. JMenu gMenu = new JMenu("Game");
  104. JMenu gMenu2 = new JMenu("Extra");
  105. //JMenu gMenu3 = new JMenu("Music");
  106.  
  107. JMenuItem newG = new JMenuItem("New Game");
  108. newG.addActionListener(new ActionListener()
  109. {
  110. public void actionPerformed(ActionEvent e)
  111. {
  112. turnNumber = 0;
  113. randomize();
  114. for(int i=0; i < gmButtons.length; i++)
  115. {
  116. gmButtons[i].setIcon(guess);
  117. }
  118. }
  119. });
  120. JMenuItem quit = new JMenuItem("Quit Game");
  121. quit.addActionListener(new ActionListener()
  122. {
  123. public void actionPerformed(ActionEvent e)
  124. {
  125. System.out.println("Exit command executed...");
  126. System.exit(0);
  127. }
  128. });
  129. JMenuItem help = new JMenuItem("Help");
  130. help.addActionListener(new ActionListener()
  131. {
  132. public void actionPerformed(ActionEvent e)
  133. {
  134. JOptionPane helpMe = new JOptionPane();
  135.  
  136. helpMe.showMessageDialog(memGame.this,"Object of the Game:\nTry to use memory " +
  137. "and some guessing to match each card with " +
  138. "its partner.\nThe lower the number of turns the " +
  139. "better you are at the memory Game!\n" +
  140. "\nMenu Features:\n" +
  141. "Selecting \"New Game\" from the menu starts over\n" +
  142. "Quit of course Quits the current game window.", "Mario Memory Game Help",3);
  143. }
  144. });
  145. JMenuItem about = new JMenuItem("About");
  146. about.addActionListener(new ActionListener()
  147. {
  148. public void actionPerformed(ActionEvent e)
  149. {
  150. JOptionPane abt = new JOptionPane();
  151.  
  152. abt.showMessageDialog(memGame.this,"Title: Mario Memory Game\n" +
  153. "Author: \n" +
  154. "Class: \n" +
  155. "\nMario is a registered TradeMark of " +
  156. "the Nintendo Corporation\n" +
  157. "This product intended for personal use only!\n" +
  158. "\nAny reproduction or distribution of this product " +
  159. "for monetary\nor commercial use is strictly " +
  160. "prohibited, due to the nature of content", "About The Author",1);
  161. }
  162. });
  163.  
  164. /*Song menu
  165.  
  166.   JCheckBoxMenuItem song1 = new JCheckBoxMenuItem("Song 1");
  167.   song1.addActionListener(new ActionListener()
  168.   {
  169.   public void actionPerformed(ActionEvent e)
  170.   {
  171.   if(soundOn == true)
  172.   {
  173.   setSound(songA,soundOn);
  174.   }
  175.   }
  176.   });
  177.  
  178.   JCheckBoxMenuItem song2 = new JCheckBoxMenuItem("Song 2");
  179.   song2.addActionListener(new ActionListener()
  180.   {
  181.   public void actionPerformed(ActionEvent e)
  182.   {
  183.   if(soundOn == true)
  184.   {
  185.   setSound(songB,soundOn);
  186.   }
  187.   }
  188.   });
  189.  
  190.   JCheckBoxMenuItem song3 = new JCheckBoxMenuItem("Song 3");
  191.   song3.addActionListener(new ActionListener()
  192.   {
  193.   public void actionPerformed(ActionEvent e)
  194.   {
  195.   if(soundOn == true)
  196.   {
  197.   setSound(songC,soundOn);
  198.   }
  199.   }
  200.   });
  201.  
  202.   gMenu3.add(song1);
  203.   gMenu3.add(song2);
  204.   gMenu3.add(song3);
  205.   */
  206.  
  207. gMenu.add(newG);
  208. gMenu.add(quit);
  209. gMenu2.add(help);
  210. gMenu2.add(about);
  211.  
  212. menuBar.add(gMenu);
  213. menuBar.add(gMenu2);
  214. //menuBar.add(gMenu3);
  215. setJMenuBar(menuBar);
  216. }
  217.  
  218. public void randomize()
  219. {
  220. int j = 0;
  221. ImageIcon temp;
  222.  
  223. vals[0] = img1;
  224. vals[1] = img1;
  225. vals[2] = img2;
  226. vals[3] = img2;
  227. vals[4] = img3;
  228. vals[5] = img3;
  229. vals[6] = img4;
  230. vals[7] = img4;
  231. vals[8] = img5;
  232. vals[9] = img5;
  233. vals[10] = img6;
  234. vals[11] = img6;
  235. vals[12] = img7;
  236. vals[13] = img7;
  237. vals[14] = img8;
  238. vals[15] = img8;
  239.  
  240. for(int i = 0 ; i < 16; i++)
  241. {
  242. j = (int)Math.floor(Math.random() * 16);
  243. temp = vals[i];
  244. vals[i] = vals[j];
  245. vals[j] = temp;
  246. }
  247. }
  248.  
  249. public ImageIcon[] getVals()
  250. {
  251. return vals;
  252. }
  253. }

And here is the MyButton.java
Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.util.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. /**
  8.  *
  9.  * @author **********
  10.  * @version **********
  11.  */
  12. public class MyButton extends JButton implements ActionListener
  13. {
  14. private int position = 0;
  15. private Icon theIm;
  16.  
  17. public MyButton()
  18. {
  19. super();
  20. position = 0;
  21. }
  22.  
  23. public MyButton(int pos)
  24. {
  25. super();
  26. position = pos;
  27. }
  28. public void setPos(int pos)
  29. {
  30. position = pos;
  31. }
  32. public int getPos()
  33. {
  34. return position;
  35. }
  36.  
  37. public void changeIcon(ImageIcon vals[])
  38. {
  39.  
  40. }
  41. public void actionPerformed(ActionEvent e)
  42. {
  43. memGame mG = new memGame();
  44. ImageIcon vals[] = mG.getVals();
  45. MyButton press = new MyButton();
  46. press = (MyButton)e.getSource();
  47. position = press.getPos();
  48. System.out.println("Position is:" + position);
  49. System.out.println("Val at that position is: " + (Object)(vals[position]).toString() );
  50. press.setIcon(vals[position]);
  51. }
  52.  
  53. }

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
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
kharri5 is offline Offline
56 posts
since Jan 2005
Sep 16th, 2005
0

Re: GUId Greif!

Quote originally posted by kharri5 ...

Java Syntax (Toggle Plain Text)
  1.  
  2. public class memGame extends JFrame
  3. {
  4. /** Creates a new instance of memGame */
  5. public memGame()
  6. {
  7. super("Mario Memory Game");
  8. randomize();
  9. }

And here is the MyButton.java
Java Syntax (Toggle Plain Text)
  1. public class MyButton extends JButton implements ActionListener
  2. {
  3. public void actionPerformed(ActionEvent e)
  4. {
  5. memGame mG = new memGame();
  6. ImageIcon vals[] = mG.getVals();
  7. }
  8. }
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
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Sep 16th, 2005
0

Re: GUId Greif!

Quote originally posted by hooknc ...
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!
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
kharri5 is offline Offline
56 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problems installing Java SDK
Next Thread in Java Forum Timeline: Swing Prolblem !!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC