Java Applets

Java applet
A Java applet is a small application written in Java and delivered to users in the form of bytecode. The user launches the Java applet from a web page and it is then executed within a Java Virtual Machine (JVM) in a process separate from the web browser itself. A Java applet can appear in a frame of the web page, a new application window, Sun's AppletViewer or a stand-alone tool for testing applets. Java applets were introduced in the first version of the Java language in 1995.
Java applets can be written in any programming language that compiles to Java bytecode. They are usually written in Java but other languages such as Jython,[8] JRuby,[9] Pascal,[10] Scala or Eiffel (via SmartEiffel) may be used as well. Java applets run at very fast speeds comparable to, but generally slower than, other compiled languages such as C++. Until approximately 2011, Java applets had been many times faster than JavaScript. Unlike JavaScript, Java applets have access to 3D hardware acceleration, making them well suited for non-trivial, computation intensive visualizations. As browsers have gained support for hardware accelerated graphics thanks to the canvas technology (or specifically WebGL in the case of 3D graphics), as well as just in time compiled JavaScript, the speed difference has become less noticeable.
Since Java's bytecode is cross-platform (or platform independent), Java applets can be executed by browsers (or other clients) for many platforms, including Microsoft Windows, FreeBSD, Unix, OS X and Linux. It is also trivial to run a Java applet as an application with very little extra code so that it can be run directly from the integrated development environment (IDE).
There are some important differences between an applet and a standalone Java application, including the following:
* An applet is a Java class that extends the java.applet.Applet class.
* A main() method is not invoked on an applet, and an applet class will not define main().
* Applets are designed to be embedded within an HTML page.
* When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine. * A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.
* The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime.
* Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed.
* Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
Life Cycle of an Applet:
There are four additional methods that you will often need to implement in your applets: init, start, stop, and destroy. Each of these corresponds to a major event in the life of an applet. Let us see what each of them does.

* init:: - This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.
* start:: -:This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.
*. stop:- This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.
* destroy: This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.
* paint: Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.
These import statements bring the classes into the scope of our applet class:
1. java.applet.Applet
2. java.awt.Graphics.
The Applet CLASS:
Every applet is an extension of the java.applet.Applet class. The base Applet class provides methods that a derived Applet class may call to obtain information and services from the browser context.
The Applet class must be the superclass of any applet that is to be embedded in a Web page or viewed by the Java Applet Viewer. The Applet class provides a standard interface between applets and their environment.
Applet includes the following method which are given below: -
* Get applet parameters
* Get the network location of the HTML file that contains the applet
* Get the network location of the applet class directory
* Print a status message in the browser
* Fetch an image
* Fetch an audio clip
* Play an audio clip
* Resize the applet
Method Index
destroy() Called by the browser or applet viewer to inform this applet that it is being reclaimed and that it should destroy any resources that it has allocated.
getAppletContext() Determines this applet's context, which allows the applet to query and affect the environment in which it runs.
 getAppletInfo() Returns information about this applet.
getAudioClip(URL) Returns the AudioClip object specified by the URL argument
getAudioClip(URL, String) Returns the AudioClip object specified by the URL and name arguments
getCodeBase() Gets the base URL
getDocumentBase() Gets the document URL
getImage(URL)  Returns an Image object that can then be painted on the screen.
getImage(URL, String) Returns an Image object that can then be painted on the screen.
getLocale() Gets the Locale for the applet, if it has been set.
getParameter(String) Returns the value of the named parameter in the HTML tag
getParameterInfo() Returns information about the parameters than are understood by this applet.
init() Called by the browser or applet viewer to inform this applet that it has been loaded into the system
isActive() Determines if this applet is active.
play(URL) Plays the audio clip at the specified absolute URL.
play(URL, String) Plays the audio clip given the URL and a specifier that is relative to it.
resize(Dimension) Requests that this applet be resized.
resize(int, int) Requests that this applet be resized.
setStub(AppletStub) Sets this applet's stub.
showStatus(String) Requests that the argument string be displayed in the "status window".
start() Called by the browser or applet viewer to inform this applet that it should start its execution.
stop() Called by the browser or applet viewer to inform this applet that it should stop its execution.

The Graphic CLASS:
The Graphics class is the abstract super class for all graphics contexts which allow an application to draw onto components that can be realized on various devices, or onto off-screen images as well.
A Graphics object encapsulates all state information required for the basic rendering operations that Java supports. State information includes the following properties.
* The Component object on which to draw.
* A translation origin for rendering and clipping coordinates.
* The current clip.
* The current color.
* The current font.
* The current logical pixel operation function.
* The current XOR alternation color.
Invoking an Applet:
An applet may be invoked by embedding directives in an HTML file or include the embedding directives in comments in same file and these comments are not compiled by the java compiler and viewing the file through an applet viewer or Java-enabled browser.
The < applet > tag is the basis for embedding an applet in an HTML file. Below is an example that invokes the "Hello, World" applet:
Example: -
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Hello World", 25, 50);
}
}
/*
< html >
< title >The Hello, World Applet< /title >
< hr >
< applet code="HelloWorldApplet.class" width="320" height="120" >
If your browser was Java-enabled, a "Hello, World" message would appear here.
< /applet >
< hr >
< /html >
*/
Output: -
C:\java\applet>javac HelloWorldApplet.java
C:\java\applet>appletviewer HelloWorldApplet.java

Application Conversion to Applets:
Useful steps for converting an application to an applet.
1. Make an HTML page with the appropriate tag to load the applet code.
2. Supply a subclass of the JApplet class. Make this class public. Otherwise, the applet cannot be loaded.
3. Eliminate the main method in the application. Do not construct a frame window for the application. Your application will be displayed inside the browser.
4. Move any initialization code from the frame window constructor to the init method of the applet. You don't need to explicitly construct the applet object.the browser instantiates it for you and calls the init method.
5. Remove the call to setSize; for applets, sizing is done with the width and height parameters in the HTML file.
6. Remove the call to setDefaultCloseOperation. An applet cannot be closed; it terminates when the browser exits.
7. If the application calls setTitle, eliminate the call to the method. Applets cannot have title bars. (You can, of course, title the web page itself, using the HTML title tag.)
8. Don't call setVisible(true). The applet is displayed automatically.
Using Appletviewer:
The appletviewer command connects to the documents or resources designated by urls and displays each applet referenced by the documents in its own window. Note: if the documents referred to by urls do not reference any applets with the OBJECT, EMBED, or APPLET tag, then appletviewer does nothing.


Free Web Hosting