0% found this document useful (0 votes)
118 views4 pages

Applet

The document discusses Java applets, including what they are, their lifecycle, how they differ from applications, and how to embed them in HTML using applet tags. An applet is a Java program that runs in a web browser, extends the Applet class, and has init(), start(), stop(), destroy(), and paint() methods corresponding to its lifecycle states.

Uploaded by

harshrajzala2312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views4 pages

Applet

The document discusses Java applets, including what they are, their lifecycle, how they differ from applications, and how to embed them in HTML using applet tags. An applet is a Java program that runs in a web browser, extends the Applet class, and has init(), start(), stop(), destroy(), and paint() methods corresponding to its lifecycle states.

Uploaded by

harshrajzala2312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Applet :

https://www.youtube.com/watch?v=bLVoxxIUpi4 (Applet, Applet Cycle, Demo Prog)

What is Applet?
 An applet is a Java program that runs in a Web browser.

 An applet is a Java class that extends the java.applet.Applet class. (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.
 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.

 Applets are not stand-alone programs. Instead, they run within either a web
browser or an applet viewer. JDK provides a standard applet viewer tool called
applet viewer.
 Output of an applet window is not performed by System.out.println(). Rather it is
handled with various AWT methods, such as drawString().

Lifecycle of Java Applet


1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
Diagram of Applet Life Cycle:

Lifecycle of Applet:
1. Initialization or Born State: Applets enters the initialization state when it is first
loaded. This is achieved by calling the init() method of Applet class. The applet is
Born.

 public void init() is used to initialized the Applet. It is invoked only once.

2. Running State: Applet enters the running state when the system calls the start()
method of Applet class.

 public void start () is invoked after the init () method or browser is maximized.
It is used to start the Applet.

3. Idle or Stop State: An applet becomes idle when it is stopped from running.
Stopping occurs automatically when we leave the page containing the currently
running applet.

 public void stop(): is used to stop the Applet. It is invoked when Applet is
stop or browser is minimized.

4. Dead State: An applet is said to be dead when it is removed from memory. This
occurs automatically by invoking the destroy() method when we quit the browser.
 public void destroy(): is used to destroy the Applet. It is invoked only once.
5. Display State: Applet moves to the display state whenever it has to perform some
output operation on the screen. This happens immediately after the running state.
The paint() method is called to accomplish this task.
 public void paint(Graphics g): is used to paint the Applet. It provides
Graphics class object that can be used for drawing oval, rectangle, arc etc.

Difference between Applet and Application


Applet Application

Small Program Large Program

Java program that can run over the Can be executed on standalone computer
internet. system

Applet is portable and can be executed Need JDK, JRE, JVM installed on client
by any JAVA supported browser. machine.

Applets are created by extending the Applications are created by writing public
java.applet.Applet static void main(String[] s) method.

Applets can be embedded in HTML pages and Applications have no special support in HTML for
embedding or downloading.
downloaded over the Internet

Example:
import java.awt.*;
import java.applet.*;

public class Myclass extends Applet public class MyClass


{ {
public void init() { } public static void main(String args[]) {}
public void start() { } }
public void stop() {}
public void destroy() {}
public void paint(Graphics g) {}
}

Java applets compile using javac but run using Application compile using javac and run using java
appletviewer command or browser. command.

Applets don’t have the main() method , it will Application have the main() method
require paint() to run applet
Applet Tag
Applet tag <applet> is required to load and start an
applet either in a browser window or in
an appletviewer(provided by JDK).

You might also like