7 Java Applets








Difference between Applications and Applets
Java applications are those applications that run using Java interpreter.  Java applications and applets are the two fundamental forms of any Java program.

Applet is nothing but a Java program that executes on web pages.  Actually it is a bytecodes, rather than the source code, that we download and then run.  To use an applet, we need a Java enabled browser, which will interpret the bytecodes for us.  It is because of this applet, that Java today is the top among all the programming languages.

Java applications are generally executed from a command-line prompt using JDK(Java Development Kit).  Applets on the other hand are executed on any browser supporting Java.  Netscape Navigator, Microsoft Internet Explorer and the Sun’s HotJava Browser are the most commonly used browsers for executing Java applets. They can also be run using an appletviewer tool that is included in the JDK.

For an applet to run, it must be included in a web page using HTML pages.  When a browser loads a web page including an applet, the browser downloads the applet from the web server and runs it on the users own system.  Java interpreter is not required specifically for running the applets at the user end.  It is already built in the browser.

When a Java program is written and saved, Java’s standard class library is included automatically.  Since applets run inside a browser, the programmer is just required to create an interface and is saved from the task of writing codes to run the applet.

Applet class
Applet is a class and all applets are the subclasses of this applet class.  The applet class is present in the java.applet package.  Applets includes several methods that help in controlling the executing applets.  These methods include starting and stopping of the applets, loading and displaying of images, loading and playing audio clips.

Applet must import java.applet and java.awt.  AWT stands for Abstract Window Toolkit.  Applets run in a window, hence support for that window has to be included.  Applets contain a single default class without any parameters.  Applets are not executed by the Java run-time interpreter; instead they are executed by a Java compatible web browsers or with the help of Applet Viewer.

Execution of applets does not begin with the main( ) method.  The execution of the applet is controlled by some other methods.  The output of the applet is generated and handled by an AWT method called drawstring( ) present in AWT package.  This method specifies the x and y locations and the string to be drawn on the screen.  After writing an applet and compiling it, it is included in an HTML file that uses HTML tag.  A Java enabled web browser then executes the applet.

paint ( ) method
paint ( ) is used to draw an output on the screen.  This method has only one parameter which is of type ‘Graphics’.

drawString ( ) method
This method is the member of the Graphics class.  It is used to draw the string on the screen.

Activities in an Applet
The activities carried out in the life cycle of an Applet is performed by the methods present in the package ‘java.applet’.

Ø  First step is Applet is created.
Ø  Next step is initialization.  This step is occurs when an applet is loaded into memory.  init ( ) method is overridden to provide behavior for the initialization.  This method is called when the appletviewer or browser loads the applet for an execution.
Ø  Once the applet is initialized it is started.  The difference between initialization and starting is that, an applet can be started many times but initialization can happen only once.
Ø  Once the use leaves the page or the page is minimized, the stop ( ) method is called.  This method performs any tasks that are required to suspend the applets execution.
Ø  Finally we have the destroy ( ) method.  Which cleans up behavior for the applet.  This method is called when the applet is being removed from memory- normally when the user of the browser exists the browser session.  This method performs any task that are required to destroy resources allocated to the applet.

Example 1(First Applet)
import java.applet.Applet;
import java.awt.Graphics;
public class FirstApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Welcome to Sandhyashree Sports Group",10,50);
}
}
Compile this file with Java compiler.  To execute this file we need to create an HTML file, which uses the APPLET tag. 

The HTML file will be as follows:-

<HTML><HEAD>
<TITLE>SPORTS GROUP SANDHYASHREE</TITLE>
</HEAD>
<BODY>
<APPLET CODE = "FirstApplet.class" WIDTH = 200 HEIGHT =150>
</APPLET
</BODY></HTML>

Execute the above file with the following command:-
appletviewer FirstApplet.html

Example2 (Applet Life Demo)
import java.awt.*;
import java.applet.Applet;
public class AppletLife extends Applet{
int init_count=0;
int start_count=0;
int stop_count=0;
int destroy_count=0;
public void paint(Graphics g){
g.setColor(Color.yellow);
g.fillRect(0,0,size( ).width,size( ).height);
g.setColor(Color.red);
g.drawLine(120,20,120,220);
g.drawLine(120,220,300,220);
g.setColor(Color.red);
g.drawString("Init Method( )",5,50);
g.drawString("Start Method( )",5,100);
g.drawString("Stop Method( )",5,150);
g.drawString("Destroy Method( )",5,200);
g.setColor(Color.green);
for(int x=(120+25);x<300;x+=25){
g.drawLine(x,20,x,199);
}
g.setColor(Color.black);
g.fillRect(120,30,init_count*25,40);
g.fillRect(120,80,start_count*25,40);
g.fillRect(120,130,stop_count*25,40);
g.fillRect(120,180,destroy_count*25,40);
}
public void init( ){
init_count++;
System.out.println("Init Method Activated");
repaint( );
}
public void stop( )
{
stop_count++;
System.out.println("Stop Method Activated");
repaint( );
}
public void destroy( )
{
destroy_count++;
System.out.println("Destroy Method Activated");
repaint( );
}
public void start( )

{
start_count++;
System.out.println("Start Method Activated");
repaint( );
}
}

HTML Code for above File:
<HTML>
<HEAD>
<TITLE>SPORTS GROUP SANDHYASHREE</TITLE>
</HEAD>
<BODY>
<APPLET CODE = "AppletLife.class" WIDTH = 400 HEIGHT =350>
</APPLET
</BODY>
</HTML>

Program Output is as follows

Practical Session

Example 1(Printing Factorial)
import java.awt.*;
import java.applet.*;
public class Factorial extends Applet{
public void paint(Graphics g){
int number=5;
int factorial=1;
for(int i=number;i>0;i--){
factorial=factorial*i;
}
String num="Factorial of 5 is :"+String.valueOf(factorial);
g.drawString(num,20,50);
}
}
Applet Code For above Program
<HTML>
<HEAD>
<TITLE>Factorial</TITLE>
</HEAD>
<BODY>
<APPLET CODE = "Factorial.class" WIDTH = 300 HEIGHT =250>
</APPLET
</BODY>
</HTML>

Example 2(Sorting Array)
import java.awt.*;
import java.applet.*;
public class ArraySort extends Applet{
int z[]={87,65,54,78,43,21};
int xPos=20;
public void paint(Graphics g){
g.drawString("The Original Series is : ",25,10);
for(int l=0;l<z.length;l++){
xPos +=50;
g.drawString(z[l] + " ",xPos,30);
}
sort(z,g);
}
public void sort(int x[],Graphics e){
int swap;
xPos=20;
int a =x.length;
for(int i=0;i<(a-1);i++){
for(int j=i+1;j<a;j++){
if(x[i]>x[j]){
swap=x[i];
x[i]=x[j];
x[j]=swap;

}
}
}
e.drawString("The Sorted Series is : ",25,60);
for(int l=0;l<a;l++){
xPos +=50;
e.drawString(x[l]+ " ",xPos,80);
}
}
}

Applet Code For Above Program
<HTML>
<HEAD>
<TITLE>SORTING ARRAY</TITLE>
</HEAD>
<BODY>
<APPLET CODE = "ArraySort.class" WIDTH = 500 HEIGHT =250>
</APPLET>
</BODY>
</HTML>

Example 3(Singraph Image Creation)
import java.awt.*;
import java.applet.*;
public class SinGraph extends Applet
{
int x1,x2,y1,y2;
double func(double x)
{
return((Math.cos(x/5) +Math.sin(x/7) +2) *getSize( ).height/4);
}
public void paint(Graphics g){
for(int x=0;x<getSize( ).width;x++){
y1=(int)func(x);
y2=(int)func(x+1);
g.drawLine(x,y1,x+1,y2);
}
}
}

Applet Code For The Above Program
<HTML><HEAD>
<TITLE>Sin Graph Image</TITLE>
</HEAD><BODY>
<APPLET CODE = "SinGraph.class" WIDTH = 600 HEIGHT =450>
</APPLET>
</BODY>
</HTML>


Output of the above Program

Example4(Creating Maps)
import java.awt.Graphics;
import java.awt.Polygon;
public class Map extends java.applet.Applet{
public void paint(Graphics screen){
screen.drawString("India",285,75);
screen.drawLine(85,80,160,75);
screen.drawRect(2,2,500,600);
screen.drawRoundRect(182,61,43,24,10,8);
int x [] = {1,34,54,72,144,234,290,360,350,290,190,191,120,90,81,12,10};
int y [] = {12,15,24,81,209,310,278,274,188,171,174,118,56,68,49,37,12};
int pts = x.length;
Polygon poly = new Polygon (x, y, pts);
screen.drawPolygon(poly);
screen.fillOval(235,140,15,15);
screen.fillOval(225,130,15,15);
screen.fillOval(245,140,15,15);
for (int ax =50; ax<150; ax+=10)
for (int ay =120; ay<320; ay+=10)
screen.drawArc(ax,ay,10,10,0,-180);
}
}

No comments:

Post a Comment