Setting Up an Applet
An applet differs from an application in structure and use. An application is a standalone program. An application can exist by itself. An applet must exist inside an HTML file (webpage) and run inside a browser. Applets do not have a main() method. All applets extend the java.applet.Applet method.
Servlets are somewhat similar to applets except that servlets run inside a server. Discussion of servlets is beyond the scope of this tutorial.
DragonClock.java
To better understand applets let us examine an applet (DragonClock.class) which will paint a digital clock to the computer screen. DragonClock is not intended to be the be-all and end-all of digital clocks. It will give you a good basic understanding of how to write an applet.
The class Java.applet.Applet has four methods. All applets need not have all four. By extending the Applet class, you have access to all four. Let us worry about what must first be addressed before we worry about applet methods.
Before init()
There are several things that must be before we begin calling applet methods and displaying clocks. As I stated before, all applets must extend the Applet class. Before we can extend a class, we must first import that class. Therefore, all applets must import the Applet class.
DragonClock must paint the clock to the computer screen. We must extend the Graphics class in order to do that. Graphics is part of the awt class. So therefore it is the awt class that must be imported.
DragonClock must also import java.util.Calendar in order to grab the current time. So we must import that class also.
The very first lines of an applet are devoted to importing the necessary classes for the program. The first three lines of DragonClock are:
import java.applet.*;
import java.awt.*;
import java.util.Calendar;
Once we import the necessary classes, we can declare our own class and begin writing the applet. The first class to be declared in an applet must be the same as the name of the applet. So then the first class of DragonClock.java would be DragonClock. Because DragonClock is an applet, DragonClock must therefore extend the Applet class.
DragonClock uses a thread to display and update the clock. In order to run a thread you must do one of two things. You can choose to extend the java.lang.Thread class. You can also choose to implement Runnable. DragonClock implements Runnable. Runnable has one method, run. Therefore, DragonClock must have a run method at some point. Any work done by the thread should be done in the run method. The class declaration line of DragonClock is:
public class DragonClock extends Applet implements Runnable
OK, we have classes imported, we have methods available, and we have a class defined which extends Applet and implements Runnable. We have all the tools (methods) we need to display a digital clock. The next lesson will examine the methods that comprise the applet.
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)
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.