1. 1
Presented by
Mariena A. A.
Asst. Professor and Head, Department of Computer Science
Little Flower College, Guruvayoor.
Java Programming
2. Java can be used to create two types of program
1.Java Applications 2.Applet
An application is a stand alone program that runs on your computer
under the operating system. No need of other softwares to run and
Main is the beginning function of application.
Applet is a java program that runs on web browser and embedded on
web page. It has strict security rules that are enforced by web
browser.
Java applet is a tiny application program transmitted over the internet
to users in the form of java bytecode and executed by java enabled
web browser.
It provides interactive features to web applications that cannot
provided by HTML alone.
Applets do not have main method.
To write an applet program you have to derive a subclass from Applet
class and override a number of standard methods. 2
4. Applets
Browser from the client machine gives request to the server and server returns the
webpage.
If the HTML page contains an applet tag which includes the class file, then browser
displays the webpage and executes the applet program contained in the webpage.
Applets are run under the control of a browser.
It can’t access the resources of native machine.It access the resources of browser.
Import two packages because Graphics class is a member of AWT.
We are embedding the applet class file into webpage using HTML file. Use Applet
tag in the HTML file and specify the class name and the width and hight of the
applet.
HTML program executed by the browser and class file executed by the JVM of the
browser.
We can execute with the help of Appletviewer.
Compile using javac simple.java
Appletviewer view.html
4
6. Applet life cycle
Already inbuilt on the Applet class. Redefine the existing method by the
technique of override.
6
7. Applet life cycle
The lifecycle of applet: The browser calls four methods of the applet class to
execute the applet.
Public void init(): It is called once when the applet is first loaded. The body
includes statements related to variable initialization, object creation, load images
or sound files, set up the screen, get parameters out of the HTML file. After the
invocation of init() method the applet enters into the born state.
public void start():This method is automatically invoked after init() method.
Unlike init() it can be invoked more than once. It is called every time the browser
displays the webpage containing the applet. it often starts the threads. Plays sound
files, or does computation. It is executed when browser is maximized.
7
8. Applet life cycle
public void stop(): It is invoked automatically when the webpage containing the
applet is closed. After this method applet enters into idle state. The user can stop
the running applet by invoking stop() method. It is executed when browser is
minimized. For ex: stop playing sound files and suspend any threads.
public void destroy(): After stop() it is called. It is invoked to remove the applet
permanently from the memory. It releases all the resources held by the applet.
This method is invoked only once during the entire life cycle of an applet. The
applet is dead when this method is invoked. If the browser needs to display the
applet again it will reload the applet and call the applet’s init() method.
Again we have to call init() to initialization.
8
11. Example
import java.io.*;
import java.awt.*;
public class FirstApplet extends Applet
{
public void paint(Graphics g)
{
g.drawstring(“My First Applet”,25,50);
}
}
The applet begins with two import statements. The first one imports AWT classes
and second imports the applet package. Every applet you create is a subclass of
Applet class.
The class should be public because it will be accessed by code that is outside the
program. Inside FirstApplet paint() is declared. This method is defined by the
AWT and must be overridden by the applet. It has one parameter of type
Graphics.
11
12. Execution
Applet could not run its own. It needs to be inserted into the HTML document
that contain applet code.
HTML document can be executed by web browser or appletviewer.
<applet code=FirstAppplet.class
width=600
height=250>
</applet>
To run using Appletviewer we have to use the following in the command line.
AppletViewer FirstApplet.html
We can execute applet in two ways
1.Using java compatible web browser( like execution of HTML file)
2.Appletviewer
12
15. Execution
We have to compile this program using javac HelloIndia.java
Take notepad and type HTML document
Save the HTML with the same name of the class and run in the command line
using
Appletviewer HelloIndia.html
15