Introduction
An applet is an piece of software code that runs under the control of a web browser, as distinct from the application which requires an interpreter.
Applets are commonly used to enhance the interactivity of a web page, and deliver client-side content. Applets run in their own frame, and can display graphics, accept input from GUI components, and even open network connections. Applets running under a browser often demonstrate a certain degree of instability depending on the platform under which it is run. Applets are small applications that are accesses on Internet server, transported over the net, automatically installed. Once an Applet arrives on the client , it has limited access to resources so that it can produce an arbitrary multi-media user interface and run complex computations without introducing the risk of viruses or breaching data integrity.
Common use of Java Applets
Ø One of the most common use Java applets is gamming like Addicting Games, Mini Clip and Pogo. For example, when we launch a chess game, it might be rendered as a Java applet. We will activate the applet by clicking within its borders, after which we can move the chess pieces around.
Ø Java applets is used within communication applications, such as instant messaging applications and chat rooms. In the case of chat clients, Java Applets may operate outside the realm of the HTML Web browser and stand on its own. Even then, however, the Java Applet is backed by the environment of the operating system.
Ø Educational tools are developed by Java Applets such as calculators (math), money management practice sheets (finance), composition and theory exercises (music), population growth demonstration charts (biology, social sciences), time management tools (life skills) and many more. Users can input data, move objects around, manipulate shapes or anything else the developer designs the Java applet to do.
Ø Java Applets can also used in the development of spreadsheets and word processors
Ø Java applets are small and light enough to operate in mobile browsers and applications. Due to their relatively small RAM, mobile phones and PDAs often run Java applets as stand-alone programs for use offline or online.
Hello word to Java Applet
Let us begin by compiling and running simple applet .
HelloWorldApplet.java
import java.awt.*;
import java.applet.*;
public class HelloWorldApplet extends Applet{
public void paint (Graphics g){
g.drwaString("HelloWorld!" ,20,20);
}
}
To run the applet in a web browser we need to write a few lines of HTML text. We can test our compiled applet by starting appletviewer with the Java source code file.
The following HTL runs HelloworldApplet:
<applet code="HelloWorldApplet" width=200 height=40>
</applet>
In general there are 3 steps to quickly iterate through the development
- Edit Java soucre file someApplicationApplet.java
- Compile our program with javac someApplicationApplet.java
- Execute appletviewer someApplicationApplet.java to run applet.
The <applet> tag is used to start an applet from both HTML document and from the JDK appletviewer. The appletviewer will execute each applet tag that it finds in a separate window, while we browser will allow many applets on a single page
The syntax for <applet> tag
<applet codebase = codebaseURL code = appletFile alt=alternateTextname =appletInstanceName width=pixels height=pixels align=alignment Vspace =pixels Hspace=pixels>
<param name=AttributeName value=AtrributeValue>
[Html displayed in the absence of Java]
</applet>