Apple Ts

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

What is an applet?

Applet: A small Java program that can be inserted into a web page and
run by loading that page in a browser.

An applet is a special kind of Java program that is designed to be


transmitted over the Internet and automatically executed by a Java-
compatible web browser.

Applets are small applications that are accessed on an Internet server,


transported over the Internet, automatically installed, and run as part of
a web document.
Applet classes in Java
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
» javax.swing.JAp
plet
How Applets Differ from Applications
Although both the Applets and stand-alone applications are Java
programs, there are certain restrictions are imposed on Applets due to
security concerns:

– Applets don’t use the main() method, but when they are loaded, automatically
call certain methods (init, start, paint, stop, destroy).
– They are embedded inside a web page and executed in browsers.
– Takes input through Graphical User Input ( GUI ).
– They cannot read from or write to the files on local computer.
– They cannot run any programs from the local computer.

The above restrictions ensures that an Applet cannot do any damage to the local
system.
Life cycle of an Applet

init()
Begin Born
stop()
start()

Running Idle

destroy()
paint() start()

Dead End
Life cycle of an Applet
It is important to understand the order in which these
methods are called.

When an applet is started , the following sequence of


method calls takes place:
1. init( )
2. start( )
3. paint( )

When an applet is terminated, the following sequence of


method calls takes place:
1. stop( )
2. destroy( )
Applet States

Initialisation
 The init( ) method is the first method to be called.
 This is where you should initialize variables.
This method is called only once during the run time of your applet.

Running – more than once


The start( ) method is called after init( ).
It is also called to restart an applet after it has been stopped.
It is called each time an applet’s HTML document is displayed on screen.
So, if a user leaves a web page and comes back, the applet resumes execution at start( ).

Display – more than once


 paint() happens immediately after the applet enters into the running state.
It is responsible for displaying output.
 paint( ) is also called when the applet begins execution.
 The paint( ) method is called each time your applet’s output must be redrawn.
 The paint( ) method has one parameter of type Graphics.
Idle
 The stop( ) method is called when a web browser leaves the HTML
document containing
the applet—when it goes to another page.

Dead/Destroyed State – only once


 The destroy( ) method is called when the environment determines that
your applet needs
to be removed completely from memory.

 At this point, you should free up any resources the applet may be using.
The stop( )
method is always called before destroy( ).
Building Applet Code: An Example
import java.awt.*;
import java.applet.Applet;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString (“A Simple Applet",20, 20);
}
}

Begins with two import classes.


java.awt.* -- required for GUI
java.applet.* -- every applet you create must be a subclass of
Applet, which is in java.applet package.
The class should start with public, because is accessed from outside.
Running an Applet

There are two ways in which you can run an applet:

Executing the applet within a Java-compatible browser.

Using a tool called , appletviewer. An applet viewer


executes your applet in a window. This is generally the
fastest and easiest way to test your applet.
Executing in a web browser.
To execute an applet in a web browser, you need to write a short HTML file
that contains a tag ( Applet ) that loads the applet.

HTML file that contains a SimpleApplet

<APPLET code=“SimpleApplet“ width=400 height=300>


</APPLET>

Save the file with .html extension (Example: Simple.html)

After you create this file, open your browser and then load this file, which
causes SimpleApplet to be executed.

width and height specify the dimensions of the display used by the applet.
Executing by using appletviewer
There are two ways
1. Use earlier html page, which contains applet tag, then execute by using
following command.
C:\>appletviewer htmlfilename.html
2. Include a comment at the beginning of your source code file that
contains the applet tag, then start applet viewer with your java source
code file. C:\>appletviewer SimpleApplet.java

import java.awt.*;
import java.applet.Applet;
/* <applet code=“SimpleApplet” width=200 height=60 ></applet> */
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString (“A Simple Applet",20, 20);
}
}
Four of these methods init(), start(), stop(), and destroy() are
defined by Applet.

Another, paint() is defined by the AWT Component class


Types of applets

There are two types of applets:

First is based on the Applet class


 These applets use the AWT to provide the GUI.
 This style of applet has been available since java was created.
 It is used for simple GUI’s.

The second is based on the Swing class JApplet.


 These applets use the Swing classes to provide the GUI.
 Swing offers a richer and easy to use interface than AWT .
 Swing based applets are more popular.
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class EventJApplet extends JApplet implements ActionListener{
JButton b;
JTextField tf;
public void init(){

tf=new JTextField();
tf.setBounds(30,40,150,20);

b=new JButton("Click");
b.setBounds(80,150,70,40);

add(b);add(tf);
b.addActionListener(this);

setLayout(null);
}

public void actionPerformed(ActionEvent e){


tf.setText("Welcome");
}
}
myapplet.html
<html>
<body>
<applet code="EventJApplet.class" width="300" height="
300">
</applet>
</body>
</html>
Feature Application Applet
main() method Present Not present
Requires a browser like
Execution Requires JRE
Chrome

Called as stand-alone
Requires some third party
application as application
Nature tool help like a browser to
can be executed from
execute
command prompt

Can access any data or cannot access any thing on


Restrictions software available on the the system except browser’s
system services

Requires highest security for


Does not require any
Security the system as they are
security
untrusted

You might also like