aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing a ball Watch "Changing a ball New topic
Author

Changing a ball's color every second

Saito Hajime
Greenhorn

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)
{
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

Joined: Oct 27, 2005
Posts: 19761
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing a ball's color every second