Java Reference
In-Depth Information
} // paintComponent
/** Calculate the ball position in the next frame. **/
void calcPosition () {
// Increment by 25 millseconds per frame
double dt = 0.025;
// Calculate position and velocity at each step
fY = fY + fVy*dt— 490.* dt * dt;
fVy = fVy — 980.0 * dt;
// Convert to the pixel coordinates
fYPixel = fFrameHt — (int)(fY * fYConvert);
// Reverse direction when ball hits bottom.
if ((fYPixel + fRadius) >= (fFrameHt-1)) {
fVy = Math.abs (fVy);
// Subtract friction loss
fVy —= 0.1 * fVy;
// Stop when speed at bottom drops
// below an arbitrary limit
if (fVy < 15.0) {
fDropDone = true;
}
}
} // calcPosition
/** Provide a flag on drop status. **/
public boolean isDone () {
return fDropDone;
}
} // class Drop2DPanel
8.7 Timers
A timer provides for periodic updates and scheduling of tasks. For example, a
timer can:
signal the redrawing of frames for an animation
issue periodic reminders as with a calendar application
trigger a single task, e.g. an alarm, to occur at a particular time in the future
As we saw in the previous section, with the Thread class you could create your
own simple timer using the Thread.sleep (long millis) method to delay
action for a given amount of time. This approach, however, has some drawbacks.
 
Search WWH ::




Custom Search