A Java applicaton is a stand-alone Java program that can be run by using the Java's interpreter from a command line. An applet, however, is a Java program that run within a World Wide Web browser. The applet runs on the client's computer, even though it may be fetched from far away. A reference to an applet is embedded in a Web page using a special tag, <applet>...</applet>
Given the convenience that applets have over applications in term of structure and user
interface and its capability of running on a client's computer directly, the developers of
Java language have put restrictions on what applets can do to the server's and client's
computer including the following:
1. Applets can't read or write to the client's file system or at most, in a very
restricted sense.
2. Applets can't usually communicate with a server other than the one that had
originally stored the applet.
3. Applets can't usually run any program on the client's system.
4. Applets can't usually load programs native to the local platform, including
shared libraries such as DLLs.
Currently, the security features that are being implemented on Java's applets and Java language provide a reasonable safeguard against the unorthodox use of the language.
To create an applet, you create a subclass of the class Applet, in the java.applet package like this:
public class YourClass extends java.applet.Applet {
......
}
A Java-compiled program has a .class file extension. By embedding the class file in the <applet> tag, it becomes an applet, that's it. Here is an example of including an applet in your HTML file:
<html>Below is the output if you run this HTML file in your browser:
This is your applet:
The code for FontColor.java looks like this:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
public class FontColor extends java.applet.Applet {
Font yourfont = new Font("TimesRoman",Font.BOLD,30);
public void paint(Graphics g) {
g.setColor(Color.green);
g.setFont(yourfont);
g.drawString("Welcome to Java Language",2,25);
}
}
For more applets examples, see the next topic on "A Little Bit on Graphics, Image and Animation".
©2001. William NWL6. All rights reserved.
Last Updated: January 17, 2002