An applet is a special kind of Java program that runs in a Java enabled browser. This is a Java program that can run over the network using the browser. Applet is typically embedded inside a web page and runs in the browser.
Applets are small Java applications that can be
accessed on an Internet server, transported over Internet, and can be automatically
installed and run as a part of a web document.
Some important differences between an applet and a
standalone Java application are:
- An applet is a Java class that extends the java.applet.Applet class.
- An applet class will not define main() method.
- 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.
Lifecycle of Java Applet
1. Applet is initialized.
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.
public void init()
2. Applet is started.
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.
public void start()
3. Applet is painted.
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.
public void paint(Graphics g)
4. Applet is stopped.
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.
public void stop()
5. Applet is destroyed.
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.
public void destroy()
Every Applet
application must import two packages - java.awt and java.applet.
java.awt.* imports
the Abstract Window Toolkit (AWT) classes. Applets interact with the user
(either directly or indirectly) through the AWT. The AWT contains support for a
window-based, graphical user interface.
java.applet.* imports
the applet package, which contains the class Applet. Every applet that you
create must be a subclass of Applet class. Every Applet application must
declare a paint() method. This method is defined by AWT class and
must be overridden by the applet. The paint() method is called each time when
an applet needs to redisplay its output.
The class in the program must be declared as public, because it will be accessed by code that is outside the program. Object is created by Java Plugin software that resides on the browser.
Running an Applet:
There are two ways to run an applet:
- Executing the Applet within Java-compatible web browser.
- Using an Applet viewer, such as the standard tool, applet viewer. An applet viewer executes your applet in a window
Example 1:
//Firstapplet.java
import java.applet.Applet;
import java.awt.Graphics;
public class Firstapplet extends Applet{
public void paint(Graphics g){
g.drawString("welcome",150,150);
}
}
//myapplet.html
<html>
<body>
<applet code="Firstapplet.class" width="300" height="300">
</applet>
</body>
</html>
Example 2:
To execute the applet by appletviewer tool, create an applet that contains applet tag in comment and compile it. After that run it by: appletviewer First.java. Now Html file is not required.
//Firstapplet.java
import java.applet.Applet;
import java.awt.Graphics;
public class Firstapplet extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="Firstapplet.class" width="300" height="300">
</applet>
*/
To execute the applet by appletviewer tool, write in command prompt:
c:\>javac Firstapplet.java
c:\>appletviewer Firstapplet.java
No comments:
Post a Comment