Changing the icons of JButtons

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Changing the icons of JButtons

 
0
  #1
Aug 21st, 2007
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!

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 794
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Changing the icons of JButtons

 
0
  #2
Aug 21st, 2007
Try using Util.Random class for your random generator. Something like:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Changing the icons of JButtons

 
0
  #3
Aug 21st, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 794
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Changing the icons of JButtons

 
0
  #4
Aug 21st, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Changing the icons of JButtons

 
0
  #5
Aug 21st, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 794
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Changing the icons of JButtons

 
0
  #6
Aug 21st, 2007
Originally Posted by Ghost View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Changing the icons of JButtons

 
0
  #7
Aug 21st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 794
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Changing the icons of JButtons

 
0
  #8
Aug 21st, 2007
I think you should call newColor() at the start of the play() method as opposed to the end?
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Changing the icons of JButtons

 
0
  #9
Aug 21st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 794
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Changing the icons of JButtons

 
0
  #10
Aug 22nd, 2007
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
  1. char letter = code.toCharArray()[i];
try
  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
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC