Try using Util.Random class for your random generator. Something like:
java.util.Random randomGen = new java.util.Random(System.currentTimeMillis());
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.
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
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?
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
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?
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
I think you should call newColor() at the start of the play() method as opposed to the end?
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
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
char letter = code.toCharArray()[i];
try
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
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'.
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
bryantpurdin, highjacking post is considered rude so please in future don't do that.
Regarding your problem can you please post your code so we can have look at it?
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
use a version control system to keep track of changes, that way you can always revert back to an earlier version.
And oh, an 800 line class is almost always way too large and is just asking to be split into different classes among functional lines.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Sorry, didn't mean to hijack - I just figured since we were both having the same exact problem, I'd let you know I was interested in anything you found out.
Unfortunately, I can't really paste my code. It's about 800 lines long, and I'm not really allowed to by my work.
What I can tell you though is that all I want to do is set the icon inside of the action command, and it's not working. I've tried setting the text, icon, background color - nothing seems to work. Nothing updates the display. I've even made these calls later, sequentially in the code, and the GUIs not getting updated.
It's really frustrating. I don't know how my code got this way. Any ideas on what's going on when you make calls to a component, and that component doesn't display the latest call?
Then just post the action command code. I have a feeling you are not setting the properties on the button you think you are, since calling button.setIcon() in an action does generally update the icon without needing any other calls i.e.
private class ButtonAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
btn.setIcon(btn.getIcon()==ico1 ? ico2 : ico1);
}
}
Will toggle between each icon as the button is pressed.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847