Advertisement



< Prev
Next >



Applet Class





Applet is a Java program that can be transported over the internet and executed by a Java-enabled web-browser(if a browser is supporting the applets) or an applet can be executed using the appletviewer utility provided with JDK. An appLet us created using the Applet class, which is a part of java.applet package. Applet class provides several useful methods to give you full control over the execution of an applet.









Note :


At present, some modern versions of browsers like Google Chrome and Mozilla Firefox have stopped supporting applets, hence applets are not displayed or viewed with these browsers although some browsers like Internet Explorer and Safari are still supporting applets.




Some methods of Applet class


Methods Description
void init() The first method to be called when an applet begins its execution.
void start() Called automatically after init() method to start the execution of an applet.
void stop() Called when an applet has to pause/stop its execution.
void destoy() Called when an appLet us finally terminated.
String getParameter(String ParameterName) Returns the value of a parameter defined in an applet
Image getImage(URL url) Returns an Image object that contains an image specified at location, url
void play(URL url Plays an audio clip found at the specified at location, url
showStatus(String str) Shows a message in the status window of the browser or appletviewer.





The lifecycle of an applet through its methods-






Advertisement




import java.awt.*;
import java.applet.*;

/*
<applet  code="Applet1" width=400 height=200>
</applet>
*/

public class Applet1 extends Applet
{

public void init()
{
System.out.println("Initializing an applet");
}

public void start()
{
System.out.println("Starting an applet");
}

public void stop()
{
System.out.println("Stopping an applet");
}

public void destroy()
{
System.out.println("Destroying an applet");
}

}





How to run an applet?


To run an applet we must define an <applet> tag inside the comments, within our applet program file. An applet program doesn't need a main() method to run, hence we run an applet either in :

In order to run the applet code mentioned above, by using appletviewer, type the following command at command prompt-
appletviewer Applet1.java
Where Applet1.java is the name of java file that contains the code of an applet.

Output


Right after running the applet program using appletviewer a new applet window is displayed to us -






Note :


System.out.println() prints the output only in the command prompt window. The appLet us also being executed using the appletviewer at command prompt, hence, at the command prompt, you will be displayed three messages due to the execution of init(), start() and paint() method defined in Applet1.java

D:\Java>appletviewer Applet1.java
Initializing an applet
Starting an applet


As soon as you close the window of your applet, a couple of methods will be called in sequence - Hence, you will get a couple of new messages at the command prompt due to the execution of stop () and destroy() method.

D:\Java>appletviewer Applet1.java
Initializing an applet
Starting an applet
Stopping an applet
Destroying an applet




Please share this article -





< Prev
Next >
< Anonymous Thread
Paint() method in Applet >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement