Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing a ball's color every second

 
Saito Hajime
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
{
ball1.Color(Color=RED);
}
else (something)
ball1.Color(Color=BLUE);
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)
{
ball1.setColor(Color.BLUE);
}
else if (ball1.getX( ) 20 * <= 1000)
{
ball1.setColor(Color.GREEN);
}
idk I can't seem to figure out what to put... I'm very new to this.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/UseCodeTags

You may want to check out javax.swing.Timer for GUI applications, and java.util.Timer for non-GUI applications.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic