advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement


Reviews : Java Books :
Beginning Java 2- JDK 1.3 Version : Images and Animation

Buy this book
Title: Beginning Java 2- JDK 1.3 Version
ISBN: 1861003668
US Price: $ 49.99
Canadian Price:
C$ 74.95
UK Price: £ 35.99
Publication Date: March 2000
Pages: 1230
© Wrox Press Limited, US and UK.

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.

How to Add Java Applets to Your Site

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.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs