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


Partners & Affiliates











advertisement

Articles : Tutorials : DragonClock Applet and Tutorial :

Other Required Methods

Contents
Introduction
Object Oriented Programming and Java
Setting Up an Applet
Applet Methods
Other Required Methods
Adding the Applet to Your Webpage

In addition to the required methods for any applet, DragonClock requires other methods to display and update a clock. We will now examine these additional methods. If you remember from lesson 2, these are the first three lines of DragonClock:

import java.applet.*;
import java.awt.*;
import java.util.Calendar;

 

The first line says we have an applet instead of an application. The second line gives us access to the Graphics class, and the third line gives us access to the Calendar class. We need the Graphics class because that will paint the clock display. Graphics has one method we will use: paint.

You may wonder why we do not use the Date class to read the current time. Date has been depreciated. Calendar is now the preferred class to use. Calendar has three methods we will use: HOUR_OF_DAY, MINUTE, and SECOND. We will now examine these classes and methods in more detail.

paint()

In paint() we do some initialization, get the current time, and build a string that will display the time. Lets examine the code.

The code will be in red and comments will be in blue. Any commentary will be in regular text.

public void paint(Graphics g)
  {
	/**
	  GRAPHICS method.  This is what paints the clock on the screen.  Uses
	  CALENDAR method for grabbing current time instead of DATE.
	**/
	g.setFont(f);
	d = Calendar.getInstance();
	s = "";
	chour = d.get(Calendar.HOUR_OF_DAY);
	cminute = d.get(Calendar.MINUTE);
	csecond = d.get(Calendar.SECOND);
	AorPM = "AM";

OK, we have defined our font (always a good idea!), created an instance of Calendar, cleared the string that will be our clock display, and finally grabbed the current time. We have a flag to indicate AM or PM which we will initialize to AM.

Now, lets get down to business! Take the current hour and do some tests on it. If the hour is under '10', that is to say one digit, then add a '0' in front of the digit. We want a 12-hour display instead of a 24-hour, so if the hour is over '12' we subtract '12' from the number and set the flag to 'PM'.

About the only manipulation needed for current minutes and seconds is adding a '0' if the number is only one digit.

We want the display to look like this:
hours : minutes : seconds 'AM or PM'
It is a simple matter to add the colon and the AMorPM flag to the string we are building.

	// Get current HOUR
	if (chour < 10)
	{
	  s += "0";
	}
	if (chour > 11)
	{
	  AorPM = "PM";
	}
	if (chour > 12)
	{
	  chour -= 12;
	}
	s += chour + ":";

	// Get current MINUTE
	if (cminute < 10)
	{
	  s += "0";
	}
	s += cminute + ":";

	// Get current SECONDS
	if (csecond < 10)
	{
	  s += "0";
	}
	s += csecond + " " + AorPM;

Now, we are ready to actually draw the clock display. Graphics.drawString is the method we employ. drawString takes three attributes. The first is the string to draw. The second and third are the X and Y offset (in integers) of the beginning of the string.

	g.drawString(s,2,14);
} // end of PAINT method

run()

run() is a method of the Runnable class. All run() does is put the thread to sleep for .2 second to allow the computer to attend to other matters. Sometimes you will want to put error-checking code into your programs. Sometimes you must enter the code. Error checking is required for putting a thread to sleep.

public void run()
  
	/**
	  THREAD method.
	  Basically, we say "If the THREAD is active (Duh, that means the
	  darn thing is working), then put the THREAD to sleep for 200
	  milliseconds and then repaint the screen.
	**/
	while (thread != null)
	{
	  try
	  {
		Thread.sleep(200);
	  }
	  /**
		If there is a problem with putting the THREAD to sleep,
		then we want to CATCH the error code and print it to
		the screen.
	  **/
	  catch (InterruptedException e)
	  {
		String err = e.toString();
		System.out.println(err);
	  }
	  repaint();
	}
	thread = null;
} // end of RUN method

This concludes the examination of the methods used in DragonClock. In the next lesson we will attach the applet to the webpage.

Next ->


Java and java Development Kit are trademarks of Sun Microsystems. All material is copyright of P & G Dragonsites and may not be reproduced in whole or in part in any media without prior permission.
For questions or comments please contact us at pgdragonsites@thecenter.zzn.com



Reprinted with permission from the author. Click here to visit the original version
Download this tutorial (~30 KB)

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs