| | |
Changing the icons of JButtons
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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!
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)
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class IanSays extends JFrame implements ActionListener { ImageIcon dark [] = {new ImageIcon("darkRed.png"), new ImageIcon("darkBlue.png"), new ImageIcon("darkGreen.png"), new ImageIcon("darkYellow.png")}; ImageIcon light [] = {new ImageIcon("red.png"), new ImageIcon("blue.png"), new ImageIcon("green.png"), new ImageIcon("yellow.png")}; JButton btns [] = {new JButton(dark[0]),new JButton(dark[1]), new JButton(dark[2]), new JButton(dark[3])}; Container cont; String code = ""; String guess = ""; int guesses = 0; public IanSays() { super("Ian Says"); setSize(415,425); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); cont = getContentPane(); cont.setLayout(null); for(int i = 0; i < btns.length; i++) { cont.add(btns[i]); btns[i].addActionListener(this); if(i<2) { btns[i].setBounds(i*200,0,200,200); } else { btns[i].setBounds((i-2)*200,200,200,200); } } play(); } public void play() { System.out.println("play"); for(int i = 0; i < code.length(); i++) { System.out.println(" in play loop"); reset(); cont.validate(); try { Thread.sleep(250); } catch(Exception e){} char letter = code.toCharArray()[i]; if(letter=='r') { System.out.println(" RED"); btns[0].setIcon(light[0]); } if(letter=='b') { System.out.println(" BLUE"); btns[1].setIcon(light[1]); } if(letter=='g') { System.out.println(" GREEN"); btns[2].setIcon(light[2]); } if(letter=='y') { System.out.println(" YELLOW"); btns[3].setIcon(light[3]); } cont.validate(); try { Thread.sleep(250); } catch(Exception e){} } try { Thread.sleep(250); } catch(Exception e){} newColor(); try { Thread.sleep(250); } catch(Exception e){} reset(); System.out.println(" end of play"); } public void newColor() { int rand = (int)(Math.random()*4); if(rand==0) { btns[rand].setIcon(light[rand]); code+="r"; } if(rand==1) { btns[rand].setIcon(light[rand]); code+="b"; } if(rand==2) { btns[rand].setIcon(light[rand]); code+="g"; } if(rand==3) { btns[rand].setIcon(light[rand]); code+="y"; } cont.validate(); } public void reset() { for(int i = 0; i < btns.length; i++) { btns[i].setIcon(dark[i]); } } public void actionPerformed(ActionEvent e) { for(int i = 0; i < btns.length; i++) { if(e.getSource()==btns[i]) { if(i==0) guess+="r"; if(i==1) guess+="b"; if(i==2) guess+="g"; if(i==3) guess+="y"; guesses++; break; } } String codeSeg = code.substring(0,guesses); System.out.println("code "+code); System.out.println("codeseg "+codeSeg); System.out.println("guess "+guess); if(!codeSeg.equals(guess)) { System.out.println("lose!"); //You Lose } else { if(guesses==code.length()) { System.out.println("next rnd!"); guess = ""; guesses = 0; play(); } } } public static void main (String[]args) { new IanSays(); } }
Try using Util.Random class for your random generator. Something like:
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.
java Syntax (Toggle Plain Text)
java.util.Random randomGen = new java.util.Random(System.currentTimeMillis()); int rand = randomGen.nextInt(4);
Last edited by darkagn; Aug 21st, 2007 at 8:54 pm.
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.
*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.
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 try I'm not sure if this will make any difference but I can't see anything wrong with the rest of your code. Does 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'.
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)
char letter = code.toCharArray()[i];
java Syntax (Toggle Plain Text)
char letter = code.charAt(i);
java Syntax (Toggle Plain Text)
cont.validate();
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.
![]() |
Similar Threads
- major problem desktop icons disappeared no easy fix (Windows NT / 2000 / XP)
- XP desktop icons and task bar disappear after login (Windows NT / 2000 / XP)
- ahh boxes as icons (Windows NT / 2000 / XP)
- Desk top icons (Windows 95 / 98 / Me)
- changing desktop icons text colour (Windows NT / 2000 / XP)
- Crunchie, can you help me? (Viruses, Spyware and other Nasties)
- Clickable icons in IE6 v. 2.60 dont work (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: layout textarea
- Next Thread: Please Help me......error in compiling
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number open-source oracle oriented panel print problem program programming project qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor





