This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Changing a ball's color every second Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing a ball Watch "Changing a ball New topic

Changing a ball's color every second

Saito Hajime

Joined: Sep 13, 2009
Posts: 1
I need to change a bouncing balls color from red to blue and blue to red every second.
what I've guessed so far is...
If (something)
else (something)
could someone help me figure out the code to make it change every second? I know it'll have to do with time... but I just can't figure out what to put within the paratheses.
I've tried a few different things like...
PAUSE1 = 50
if PAUSE1 >= 1000)
else if (ball1.getX( ) 20 * <= 1000)
idk I can't seem to figure out what to put... I'm very new to this.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20184

You may want to check out javax.swing.Timer for GUI applications, and java.util.Timer for non-GUI applications.

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45307
Welcome to JavaRanch

Agree with Rob's suggestion about a javax.swing.Timer. You will see that link gives suggestions about how to use it, and a useful "How to" link.
The bit about .Color is bad design. You ought to have private fields only and gain access via methods. I suggest you add a Color[] field to the Ball class, and to the Ball constructor. The set up a nextColour() method in the Ball class, which uses the ++ and % operators to loop through the colours available to that Ball. That way you can have a Ball which changes red-blue-red and another which changes red to green.
It is sorta covered in the JavaRanch Style Guide.
subject: Changing a ball's color every second
It's not a secret anymore!