Beginning Java 2- JDK 1.3 Version Images and Animation
Starting the Clock
The operation of the clock will be controlled by a Timer
object, but before it starts the hands should be set to correspond to the
current time. The static getInstance() method in the
Calendar class that we discussed back in Chapter 10 returns a
reference to a Calendar object that corresponds to the current
time recorded in the system clock. We can then use the get()
method to get the second, minute, and hour values for the current time as
integers. We can use these to figure out the angles for the hand positions
and then call the setPosition() method for the
Hands object. Here's how that can be coded:
public void start()
{
Calendar now = Calendar.getInstance();
// Calendar for this instant
// Get current seconds, minutes, and hours
seconds = now.get(now.SECOND);
minutes = now.get(now.MINUTE);
hours = now.get(now.HOUR);
timer = new java.util.Timer(true); // Timer to run clock
// Use fixed-rate execution to maintain time
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
increment(); // Increment the time
hands.repaint() // and repaint the hands
}
},
0, // ...starting now
ONE_SECOND); // and at one second intervals
}
The constants TWO_PI and ONE_SECOND need to be
added as members of the applet class along with the timer
field. We must also add the fields to store the time. The following
statements will do that:
final double TWO_PI = 2.0*Math.PI;
final int ONE_SECOND = 1000; // One second in milliseconds
java.util.Timer timer; // Timer to control the clock
int seconds, minutes, hours; // The current time
After saving the current time as seconds, minutes, and hours, we create a
Timer object to control the clock. We use the
scheduleAtFixedRate() method to run the clock because we want
the clock to be updated at intervals of one second, and if an update is
delayed for any reason, we don't want subsequent updates to be delayed. The
TimerTask object that is defined by an anonymous class calls
the repaint() method for the Hands object, then
the increment() method to update the time to the next second.
We can implement the increment() method in the
NewClock class like this:
// Increment the time by one second
public void increment()
{
if(++seconds>= 60) // If seconds reach 60
++minutes; // ...increment minutes
if(minutes>=60) // If minutes reach 60
++hours; // ...increment hours
seconds %= 60; // Seconds 0 to 59
minutes %= 60; // Minutes 0 to 59
hours %= 12; // Hours 0 to 11
}
When we have accumulated 60 seconds after incrementing the time, we must
increment the count of the number of minutes by one. Similarly, when
minutes reaches 60 we must increment the hour count.
Stopping the Clock
Stopping the clock is just a question of canceling the timer:
public void stop()
{
timer.cancel(); // Cancel the clock timer
}
How It Works
While this example provides an illustration of using the fixed-rate
execution mode with a timer, the more interesting aspect is the use of the
glass pane to hold the animated portion of an image. You can apply this
technique to any animation with any component that has a content pane and a
glass pane. This includes components defined by the JWindow,
JDialog, JFrame and JInternalFrame
classes, as well as the JApplet class as we have just seen.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|