• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Changing a ball's color every second

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22700
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Skool. Stay in. Smartness. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic