Cycling through multiple colors for a button by pressing it multiple times.

I have two colored buttons. For each button, pressing it is supposed to change its color. They are supposed to cycle through six different colors, with the sixth button press cycling back to the original color. However, when I click either button, they don't change color. Please help, thank you!

EDIT: To clarify, I don't want the button to cycle through all of the colors with a single click; I want the buttons to change to the next color with a single click. I want the first click to change the button's color to blue, the second to change it to red, the third to yellow, the fourth to magenta, the fifth to pink, the sixth back to green, the seventh to blue again, etc.

You want the button color to cycle within a single click?
The code seems to try doing so, the for loops do change the color, but so fast that you only see green in the end.
Also consider using the switch statement, that many if-else don't look good

Campbell Ritchie wrote:The whole thing will go horribly wrong if you push the key for the 2147483648th time

... unless the number of colors is a power of 2 (Yes I can count. 6 isn't.)

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44016

33

posted Nov 29, 2012 14:38:13

0

No, it will still go horribly wrong if you have 4 or 8 colours. It will, of course, as Darryl has said, go wrong in phase with itself. With 6 colours it will go wrong out of phase with itself.
There is a fiddle you can use with the bitwise and operator which will stop it going horribly wrong. That depends on the number of colours being a power of 2.

Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14

posted Nov 29, 2012 16:33:19

0

Actually, I don't want the button to cycle through all of the colors with a single click; I want the buttons to change to the next color with a single click. I guess I wasn't too clear about that, sorry. I want the first click to change the button's color to blue, the second to change it to red, the third to yellow, the fourth to magenta, the fifth to pink, the sixth back to green, the seventh to blue again, etc.

Then the actionPerformed methods should call "leftButton.setBackground" (etc) exactly once, with the colour you want to change the background to.

The problem then is to figure out what that colour is. Something like what Campbell posted might work for you, or you might want to write some code which keeps track of what the current colour is and gets the next colour based on that.

Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14

posted Dec 01, 2012 13:43:25

0

Thanks everyone, I've figured it out!

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com

subject: Cycling through multiple colors for a button by pressing it multiple times.