Forums Register Login

Changing a ball's color every second

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1640 times.
Similar Threads
JSF life cyclus questions
changing colors in an if statement
how to make a simple XML file using JSP
Setting color of a rectangle
Arraylist
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:39:44.