Now I can start with a request that was put in to "do something about timers" (For my next trick, I'm going to turn water into funk)
A timer is a way of making something happen over and over again, kind of like a loop, only you can turn it on and off at will, set how fast or slow it happens, and determine when it starts. It requires a couple of new import statements; and since you already have your "Menu" programs open, we might as well use them to do this. Here's what you need to do:
A) add the two import statements at the top of your program:
import java.util.Timer;
import java.util.TimerTask;
B) Declare a Timer object and two integers that will be used in controlling when this event starts and how fast it will go:
Timer myClock;//declare Timer object
int countDown = 5000; // delay for 5 sec.
int howOften = 1000; // repeat every sec.
C) create the timer with his line of code...type it in just above the point where the frame shows up (Gumby.show() )
myClock = new Timer();
D) create another menuitem and add it to one of your menus. You can use one you've already declared, such as inky or pinky or blue
E) create another else if statement in your actionPerfomed method, and put in the following code, like so
else if (e.getSource() == inky)
{
myClock.scheduleAtFixedRate(new TimerTask( )
{
public void run( )
{
System.out.println("hello");
}
}, countDown, howOften);
}//end timer stuff
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment