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. |