This tutorial is basically for Begineers who have no knowkedge of Applets before reading this tutorial . So , I am expecting that you know some basics of Java programming . If you are also new to Java programming I suggest you to go through java tutorial also here .
So , what is Applet in Short ?
Applet is small java program that can be placed on the HTML pages . Applet are featured with graphics and you can easily animate graphics by the help of Applet .
Example of Applet can be considered as the google ads you see on the Web pages that includes rich graphics .
Now starting with the other topics that need to be covered in the turorial .
Classification of Java programs
Java programs are classified into three groups:
- Java Applications
- Java Applets
- Java Servlets.
Java Applications
Java Applications are executed on the command prompt on the desktop system like other windows application .
Java Applets
Applets are executed on the web browsers on client side .
So what is Applet Finally ?
An applet is a dynamic and interactive program that can run inside a web page displayed by a browser.
It is possible to download the applets and play applets on our system.
Applets can be used as rich graphics java programs on the Web .
Limitations of Applets
- Applets cannot read or write to the file system.
- Applets cannot communicate with any other server than the one ion which they were stored originally.
- Applets cannot run any programs on the system.
So, after reading limitations of Applets we are sure that applets can’t harm our computer with any other code . This feature of the applet makes it more popular on the Web .
Now , We should take a simple exmaple of applet in java . To create a applet on eclipse just follow these instructions :
- Create a Java Project .
- Add package and then class to the project .
- Now name the class as ” myApplet ” and put the given below in the class .
- Now run Project as Applet…Enjoy .
import java.awt.*;
import java.applet.*;
public class myApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString (“Hello”, 70, 30);
}
}
A simple Applet with Parameters
public class myApplet extends Applet
{
String name;
public void init() { name=getParameter(“t1”); }
public void paint (Graphics g)
{
g.drawString (name, 70, 30);
}
}
If you want to embeeded the applet into Webpages just put the below code in the Webpage . Your applet.class file should be present in the same folder of your webpage .
HTML file to Run an Applet
Inside HTML file include
You can tune size of applet by changing WIDTH and HEIGHT values as required .
Now , it’s time to learn meathods that are present in applet class as derived from extended applet class .
Here are the Applet Methods
init() method :
This method gets called as soon as applet is started. Initialization of all variables, creation of objects, setting of parameters etc can be done in this method.
start() method :
This method is executed after the init() method. When a browser is used to run an applet, any time it is reloaded, the execution begins from the start() method.
stop() method :
This method is used to halt the running of an applet. When a browser is used to run an applet, as soon as web browser is closed, this method is called.
destroy() method :
This method is used to free the memory occupied by the variables and objects initialized in the applet. Any clean up activity that need to be performed can be done in this method.
paint() method :
This method helps in drawing and creating a colored background or an image onto the applet. It always takes a Graphics Class object as an parameter.
repaint() method :
This method calls the update() method, to clear the screen. The update() method in turn calls the paint() method.
Methods in a Graphics Class
These methods are inbuild methods which you can use to draw graphics on applet . You just have to pass coordinates for drawing figures and graphics .
drawBytes( byte[], int start, int end, int x, int y);
drawChars(char[], int start, int end, int x, int y);
drawString (String str, int x, int y);
drawLine(int x1, int y1, int x2, int y2);
drawRect(int x1, int y1, int w, int h);
fillRect(int x1, int y1, int w, int h);
drawRoundRect (int x1, int y1, int w, int h, int w1, int h1);
fillRoundRect (int x1, int y1, int w, int h, int w1, int h1);
draw3DRect(int x1, int y1, int w, int h, true/false);
drawPolygon (int xs, int ys, pts);
fillPolygon (int xs, int ys, pts);
Other methods in a Graphics Class
drawOval(int x1, int y1, int w, int h);
fillOval(int x1, int y1, int w, int h);
drawArc(int x1, int y1, int w, int h, angle1, angle2);
fillArc(int x1, int y1, int w, int h, angle1, angle2);
Now taking about Fonts and Colors available in applet :
setFont (Font f);
setColor (Color c);
drawImage (Image img, int x, int y, this);
At last Questions for Revision of Applet ?
- Define Applet?
- Explain the significance of methods present in Applet Class.
- Draw an applet of Displaying The hello message on the html page Showing smiling face onto the html page.
You can tune size of applet by changing WIDTH and HEIGHT values as required .
Now , it’s time to learn meathods that are present in applet class as derived from extended applet class .
Here are the Applet Methods
init() method :
This method gets called as soon as applet is started. Initialization of all variables, creation of objects, setting of parameters etc can be done in this method.
start() method :
This method is executed after the init() method. When a browser is used to run an applet, any time it is reloaded, the execution begins from the start() method.
stop() method :
This method is used to halt the running of an applet. When a browser is used to run an applet, as soon as web browser is closed, this method is called.
destroy() method :
This method is used to free the memory occupied by the variables and objects initialized in the applet. Any clean up activity that need to be performed can be done in this method.
paint() method :
This method helps in drawing and creating a colored background or an image onto the applet. It always takes a Graphics Class object as an parameter.
repaint() method :
This method calls the update() method, to clear the screen. The update() method in turn calls the paint() method.
Methods in a Graphics Class
These methods are inbuild methods which you can use to draw graphics on applet . You just have to pass coordinates for drawing figures and graphics .
drawBytes( byte[], int start, int end, int x, int y);
drawChars(char[], int start, int end, int x, int y);
drawString (String str, int x, int y);
drawLine(int x1, int y1, int x2, int y2);
drawRect(int x1, int y1, int w, int h);
fillRect(int x1, int y1, int w, int h);
drawRoundRect (int x1, int y1, int w, int h, int w1, int h1);
fillRoundRect (int x1, int y1, int w, int h, int w1, int h1);
draw3DRect(int x1, int y1, int w, int h, true/false);
drawPolygon (int xs, int ys, pts);
fillPolygon (int xs, int ys, pts);
Other methods in a Graphics Class
drawOval(int x1, int y1, int w, int h);
fillOval(int x1, int y1, int w, int h);
drawArc(int x1, int y1, int w, int h, angle1, angle2);
fillArc(int x1, int y1, int w, int h, angle1, angle2);
Now taking about Fonts and Colors available in applet :
setFont (Font f);
setColor (Color c);
drawImage (Image img, int x, int y, this);
At last Questions for Revison of Applet ?
Define Applet? Explain the significance of methods present in Applet Class.
Draw an applet of Displaying The hello message on the html page
Showing smiling face onto the html page.
Leave a Reply