Upload
aamershahbaaz
View
218
Download
0
Embed Size (px)
Citation preview
7/30/2019 APPLETS03.doc
1/15
APPLETS
When a html page wants to communicate with the user on
Internet, it can use a special java program called as
applet to communicate and respond to the user.
The user can interact by typing some details or by clicking
the components available in the applet program.
The program then processes and then displays results.
APPLET = JAVA BYTE CODE + HTML PAGE
That is, we can understand an applet as a java byte code
embedded in a html page.
Therefore, applet can be defined as java byte code embedded
in a web page.
The java.applet package provides an API ( application
programming interface) that gives applets some capabilities
that applications dont have. Ex: applets can play sounds
which other programs cant do yet.
When you use a Java technology-enabled browser to view a
page that contains an applet, the applet's code is
transferred to your system and executed by the browser's
Java Virtual Machine (JVM).
Applets are classified as: LOCAL APPLET & REMOTE APPLET
Local applet is developed by user and it is present inour local machine only.
Remote applet is developed by others and is present ininternet. It can be downloaded to our local machine and
it can run in our machine also.
Syntax for writing html code:
. . . . .
7/30/2019 APPLETS03.doc
2/15
. . . . . .
. . . . . .
Creating an APPLET:
To create an applet we need to write a java program and
compile it to get byte code.
Then we should include it into a HTML page on a
particular location wherever we want it to be
displayed.
This page is the stored in web server.
A client machine communicates with web server, the
server then sends the HTML page that contains the
applet.
The page is then transmitted to the client where
the applet is executed on the clients web
browser.
Thus, applets are executed at client side by the
web browser.
Thus, applets travel thousands of kilometers of
distance on internet and reach the client machines
before their execution on the client.
To create an applet we have Applet class of
java.applet package and JApplet class of
javax.swing package. The classes use the following
methods which are automatically run by any appletprogram. So these methods should be overridden in
applet program.
1.public void init():
This method is the first method to be called bythe browser and it is executed only once.
7/30/2019 APPLETS03.doc
3/15
So the programmer can use this method toinitialize any variables, creating components
and creating threads etc.,
When execution of this method is completedbrowser looks for the next method start().
2.Public void start():
This method is called after init() method and each timethe applet is revisited by the user.
For example, the user has minimized the web page thatcontains the applet and moved to another page then this
methods execution is stopped.
When the user comes back to view the webpage againstart() method execution will resume.
Any calculations and processing of data should be donein this method and the results are also displayed.
3. Public void stop():
This method is called by the browser when the applet isto be stopped.
If the user minimizes the web page, then this method iscalled and when the user again comes back to this page,
then start() method is called.
In this way, start() and stop() methods can be calledrepeatedly.
For example, an applet with some animation might want touse the start() method to resume animation, and the
stop() method to suspend animation.
4. public void destroy():
This method is called when applet is being terminatedfrom memory.
The stop() method will always be called beforedestroy().
The code related to releasing the memory allocated tothe applet and stopping any running threads should be
written in this method.
7/30/2019 APPLETS03.doc
4/15
EXECUTING init(),start(),stop() AND destroy() METHODS IN
THE ABOVE SEQUENCE IS CALLED AS LIFE CYCLE OF APPLET.
Note: writing all these methods is not compulsory while
writing an applet.
Applet dosent have main() method. That is,public static
void main(String args[]) is not available in applet. So
applet code can be compiled but cannot be executed by JVM.
Now the question is where does this applets run?
Once the applet is created we compile and obtain its byte
code.
This byte code is embedded in HTML page and the page is
sent to client computer.
The client machine contains a browser like HTML page and
the page in sent to client computer.
The client machine contains a browser like internet
explorer, netscape navigator or Mozilla firefox where HTML
page is viewed by user.
The same b rowser will execute the applet of the HTML page.
The browser contains a small virtual machine called applet
engine which understands and run the applet.
7/30/2019 APPLETS03.doc
5/15
APPLET LIFE CYCLE:
Initialization state:
Stop()
Start()
Paint()
Display
Born
Dead
Running
Idle
Begin
(load Applet)initialization
Start()
stopped
Destroy()
Destroyed
Exit of Browser
Applet state transition diagram
7/30/2019 APPLETS03.doc
6/15
Applet enters the initialization state when it is
first loaded. This is achieved by calling the init() method of Applet
class.
Create objects needed by the applet.
Set up initial values. Load images or fonts.
Set up colors
Running state:
Applet enters the running state when the system calls
the start() method of Applet class. This occurs automatically after
the applet is initialized. Starting can also occur if the applet is
already in stopped (idle) state.
Idle or Stopped State:
An applet becomes idle when it is stopped from
running. Stopping occurs automatically when we leave the page
containing the currently running applet. We can calling the stop ()
method explicitly.
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.
Display State:
Applet moves to the display state whenever it has to
perform some output operation on the screen. This happens immediately
7/30/2019 APPLETS03.doc
7/15
after the applet enters into the running state. To display a message
we can take the help of paint() method of comoponent class of
java.awt package.
An applet life cycle is born with init() method and starts
functioning with start() method. To stop the applet the stop()
method is called and to terminate the applet completely from
memory the destroy() method is called. Once the applet is
terminated, we should reload the HTML page again to get the
applet start once again from init() method. This cyclic way of
executing the methods is called as applet life cyle.
CREATING A SIMPLE APPLET
Java program to create an applet with pink color background and
a message
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet{
public void init()
{
setBackground(Color.pink);
}
public void paint(Graphics g)
{
g.drawString("this is my first applet",50,100);
}
}
Save the above program as : myapplet.java
Compile the above program as: myapplet.java
Now myapplet.class file is created.
This byte code should be embedded inti a html page using
tag as shown below:
7/30/2019 APPLETS03.doc
8/15
Save the above code asmyapplet.html
This html page contains the applet which can be opened in the
browser or an applet viewer supplied by sun micro systems can be
used to test the applet.
For this purpose, open browser and write the path of .html file
Along with directory path. The applet will be opened in the
browser or give the following command in command prompt :
appletviewer myapplet.html
output:
7/30/2019 APPLETS03.doc
9/15
Java program to create applet with some background color and
font color with a message. This message string is stored in msg
and is displayed in paint() method.
import java.awt.*;
import java.applet.*;
public class myapplet1 extends Applet
{
String msg=" ";
public void init()
{
setBackground(Color.yellow);
setForeground(Color.red);
Font f = new Font("Arial",Font.BOLD,20);
setFont(f);
msg +=" this is init";}
public void start()
{
msg += " this is start";
}
public void stop()
{
msg += " this is stop";
}
public void destroy()
{
msg +=" this is destroy";
}
public void paint(Graphics g)
{
g.drawString(msg,50,100);
}
}
Output:
7/30/2019 APPLETS03.doc
10/15
Program to draw lines
import java.awt.*;
import java.applet.*;
7/30/2019 APPLETS03.doc
11/15
public class lines extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.cyan);
g.drawLine(10,20,170,40);
g.setColor(Color.pink);
g.drawLine(170,40,80,140);
g.setColor(Color.green);
g.drawLine(80,140,10,20);
}
}
Program to create rectangle
import java.awt.*;
import java.applet.*;
public class rectangle extends Applet
{
7/30/2019 APPLETS03.doc
12/15
public void paint(Graphics g)
{
setBackground(Color.pink);
g.drawRect(30,30,60,60);
}
}
Program to fill rectangle with a color
import java.awt.*;
import java.applet.*;
public class rectangle extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.pink);
7/30/2019 APPLETS03.doc
13/15
g.drawRect(30,30,60,60);
g.fillRect(30,30,60,60);
setForeground(Color.yellow);
}
}
Program to draw polygons
import java.awt.*;
import java.applet.*;
public class polygon extends Applet
{
int x1[]={100,10,100,200,100};
int y1[]={10,100,200,100,10};
int n1=5;
int x2[]={200,300,400,300,200};
int y2[]={100,200,100,10,100};
int n2=5;
public void paint(Graphics g)
7/30/2019 APPLETS03.doc
14/15
{
g.drawPolygon(x1,y1,n1);
g.fillPolygon(x2,y2,n2);
}
}
Program to display a banner which is moving
import java.awt.*;
import java.applet.*;
/*
*/
public class Banner extends Applet implements Runnable
{Thread t;
String s;
public void init()
{
t = new Thread(this);
7/30/2019 APPLETS03.doc
15/15
s = "This is a moving banner";
t.start();
}
public void run()
{
char ch;
while(true) {
try{
repaint();
Thread.sleep(250);
ch = s.charAt(0);
s = s.substring(1, s.length());
s += ch;
} catch(InterruptedException e) {
System.out.println("Exception occured: " + e);
}
}
}
public void paint(Graphics g)
{
g.drawString(s, 20, 20);}
}