943,844 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4573
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 21st, 2007
0

Changing the icons of JButtons

Expand Post »
Hi,
I am trying to create a simon-says type game, where there are four icons of different colors. One icon lights up. The user clicks it. Next, a new icon lights up. The user clicks the sequence... etc, etc, etc.
Right now, the first round works. Unfortunately, the icons do not change after that point. Any help would be appreciated. Thanks!

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class IanSays extends JFrame implements ActionListener
  7. {
  8. ImageIcon dark [] = {new ImageIcon("darkRed.png"),
  9. new ImageIcon("darkBlue.png"), new ImageIcon("darkGreen.png"),
  10. new ImageIcon("darkYellow.png")};
  11. ImageIcon light [] = {new ImageIcon("red.png"),
  12. new ImageIcon("blue.png"), new ImageIcon("green.png"),
  13. new ImageIcon("yellow.png")};
  14. JButton btns [] = {new JButton(dark[0]),new JButton(dark[1]),
  15. new JButton(dark[2]), new JButton(dark[3])};
  16.  
  17. Container cont;
  18.  
  19. String code = "";
  20.  
  21. String guess = "";
  22.  
  23. int guesses = 0;
  24.  
  25. public IanSays()
  26. {
  27. super("Ian Says");
  28. setSize(415,425);
  29. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. setVisible(true);
  31.  
  32. cont = getContentPane();
  33. cont.setLayout(null);
  34.  
  35. for(int i = 0; i < btns.length; i++)
  36. {
  37. cont.add(btns[i]);
  38. btns[i].addActionListener(this);
  39. if(i<2)
  40. {
  41. btns[i].setBounds(i*200,0,200,200);
  42. }
  43. else
  44. {
  45. btns[i].setBounds((i-2)*200,200,200,200);
  46. }
  47. }
  48. play();
  49. }
  50.  
  51. public void play()
  52. {
  53. System.out.println("play");
  54. for(int i = 0; i < code.length(); i++)
  55. {
  56. System.out.println(" in play loop");
  57. reset();
  58.  
  59. cont.validate();
  60.  
  61. try
  62. {
  63. Thread.sleep(250);
  64. }
  65. catch(Exception e){}
  66.  
  67. char letter = code.toCharArray()[i];
  68. if(letter=='r')
  69. {
  70. System.out.println(" RED");
  71. btns[0].setIcon(light[0]);
  72. }
  73. if(letter=='b')
  74. {
  75. System.out.println(" BLUE");
  76. btns[1].setIcon(light[1]);
  77. }
  78. if(letter=='g')
  79. {
  80. System.out.println(" GREEN");
  81. btns[2].setIcon(light[2]);
  82. }
  83. if(letter=='y')
  84. {
  85. System.out.println(" YELLOW");
  86. btns[3].setIcon(light[3]);
  87. }
  88.  
  89. cont.validate();
  90.  
  91. try
  92. {
  93. Thread.sleep(250);
  94. }
  95. catch(Exception e){}
  96. }
  97.  
  98. try
  99. {
  100. Thread.sleep(250);
  101. }
  102. catch(Exception e){}
  103.  
  104. newColor();
  105.  
  106. try
  107. {
  108. Thread.sleep(250);
  109. }
  110. catch(Exception e){}
  111. reset();
  112. System.out.println(" end of play");
  113. }
  114.  
  115. public void newColor()
  116. {
  117. int rand = (int)(Math.random()*4);
  118. if(rand==0)
  119. {
  120. btns[rand].setIcon(light[rand]);
  121. code+="r";
  122. }
  123. if(rand==1)
  124. {
  125. btns[rand].setIcon(light[rand]);
  126. code+="b";
  127. }
  128. if(rand==2)
  129. {
  130. btns[rand].setIcon(light[rand]);
  131. code+="g";
  132. }
  133. if(rand==3)
  134. {
  135. btns[rand].setIcon(light[rand]);
  136. code+="y";
  137. }
  138. cont.validate();
  139. }
  140.  
  141. public void reset()
  142. {
  143. for(int i = 0; i < btns.length; i++)
  144. {
  145. btns[i].setIcon(dark[i]);
  146. }
  147. }
  148.  
  149. public void actionPerformed(ActionEvent e)
  150. {
  151. for(int i = 0; i < btns.length; i++)
  152. {
  153. if(e.getSource()==btns[i])
  154. {
  155. if(i==0)
  156. guess+="r";
  157. if(i==1)
  158. guess+="b";
  159. if(i==2)
  160. guess+="g";
  161. if(i==3)
  162. guess+="y";
  163. guesses++;
  164. break;
  165. }
  166. }
  167.  
  168. String codeSeg = code.substring(0,guesses);
  169. System.out.println("code "+code);
  170. System.out.println("codeseg "+codeSeg);
  171. System.out.println("guess "+guess);
  172. if(!codeSeg.equals(guess))
  173. {
  174. System.out.println("lose!");
  175. //You Lose
  176. }
  177. else
  178. {
  179. if(guesses==code.length())
  180. {
  181. System.out.println("next rnd!");
  182. guess = "";
  183. guesses = 0;
  184. play();
  185. }
  186. }
  187. }
  188.  
  189. public static void main (String[]args)
  190. {
  191. new IanSays();
  192. }
  193. }
Similar Threads
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 21st, 2007
0

Re: Changing the icons of JButtons

Try using Util.Random class for your random generator. Something like:
java Syntax (Toggle Plain Text)
  1. java.util.Random randomGen = new java.util.Random(System.currentTimeMillis());
  2. int rand = randomGen.nextInt(4);
will create a new seeded random generator every time. I think this should be more stable than using Math.random() but let us know if that's not the problem.
Last edited by darkagn; Aug 21st, 2007 at 8:54 pm.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Aug 21st, 2007
0

Re: Changing the icons of JButtons

Thanks, but I'm pretty sure that is not the problem. I think the problem has to do with the JFrame or JButtons not updating... Thanks!
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 21st, 2007
0

Re: Changing the icons of JButtons

Ah, sorry. I misunderstood what you meant. I think maybe you may need to call the repaint() method on the JFrame in the newColor() and reset() methods?
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Aug 21st, 2007
0

Re: Changing the icons of JButtons

I tried that, but it did not work. The odd thing is that it works on the first round... but not any of the others.
Thanks for your help!
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 21st, 2007
0

Re: Changing the icons of JButtons

Click to Expand / Collapse  Quote originally posted by Ghost ...
The odd thing is that it works on the first round... but not any of the others.
Do you mean that a full sequence of colours runs correctly the first time and then the next time the sequence is the same?
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Aug 21st, 2007
0

Re: Changing the icons of JButtons

The first time, there is only one blink in the sequence. The next time, there are two blinks... and so on.
Only the first blink works correctly
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 21st, 2007
0

Re: Changing the icons of JButtons

I think you should call newColor() at the start of the play() method as opposed to the end?
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Aug 21st, 2007
0

Re: Changing the icons of JButtons

Actually, the game is played like this:
*The computer lights up 1 button.
*All buttons go dark
*The player clicks the button the computer lit
*The computer re-lights the original button, and then all buttons go dark, and then the computer lights a new button.
*All buttons go dark.
*The player clicks the previously lit buttons in the correct order.
The process repeats until the player messes up.
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 22nd, 2007
0

Re: Changing the icons of JButtons

Sorry Ghost, I was at work but now I'm at home and can look more closely at your code.

I'm thinking that your problem lies in the reading of the second (and subsequent possibly) characters to your code string. Instead of
java Syntax (Toggle Plain Text)
  1. char letter = code.toCharArray()[i];
try
java Syntax (Toggle Plain Text)
  1. char letter = code.charAt(i);
I'm not sure if this will make any difference but I can't see anything wrong with the rest of your code. Does
java Syntax (Toggle Plain Text)
  1. cont.validate();
repaint the buttons? Do they return to dark after the first round?

EDIT: I just compiled your code on my PC, it looks like the code follows what I press rather than the other way around. On the very first pass, code.length() = 0, so there is no code to guess. I pressed yellow, and the second pass had a code of 'y'.
Last edited by darkagn; Aug 22nd, 2007 at 9:34 am.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007

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: layout textarea
Next Thread in Java Forum Timeline: Please Help me......error in compiling





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


Follow us on Twitter


© 2011 DaniWeb® LLC