Applet Class Notes PDF
Applet Class Notes PDF
Applet Class Notes PDF
When a HTML (Hyper Text Markup Language) page wants to communicate with the user on Internet, it
can use a special Java program, called 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 displays the results.
Applets can be used for multiple purposes. some of the uses of applets are
applets are used on internet for creating dynamic web pages. there are two types of web pages: static and
dynamic.
static web pages provide some information to the user but the user can't interact with the webpages other
than viewing the information. dynamic web pages interact with the user at the runtime. for example, a
student can type his hall ticket number in a text field and click the retrieve button to get back his results
from his University server. Applets are useful to provide such interaction with the user at runtime.
another use of applets is for creating animation and games where the images can displayed or moved
giving a visual impression that they are alive.
now days most of the websites on internet are dependent on other softwares like PhotoShop, Flash,
DreamWeaver, etc for creating, editing, and providing animation to the images. but for validating the
form data, scripting languages like JavaScript and PHP are better. hence the use of applets is diminishing
rapidly.
We can understand an applet as a Java byte code embedded in a HTML page, generally for the purpose of
achieving communication with the user. We can think of an applet as:
Hierarchy of a class
Object - Container - Componet - Panel - Applet - JApplet
Please note that the 'public static void main(String args[]) , method is not available in case of applets. This
means, we can compile the applet code but we can not run it using a JVM. Now the question is, where are
the 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 the client computer. The client machine contains a browser like Internet
Explorer, Netscape Navigator or Mozilla Firefox where the HTML page is viewed by the user.
the same browser will execute the applet of the HTML page. the browser contains a small virtual machine
called 'applet engine' which understands and runs the applet code.
An applet 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 applet life cycle.
To create an applet, we have Applet class of java. applet package and JApplet class of javax.swing
package.· These classes use the following methods, which are automatically run by any applet program.
So, these methods should be overridden in an applet program.
public void init () : This method is the first method to be called by the browser and it is executed only
once. So, the programmer can use this method to initialize any variables, creating components and
creating threads, etc. When this method execution is completed, browser looks for the next method start()
public void start (): This method is called after initO method and each time the applet is revisited by the
user. For example, the user has minimized the web page that contains the applet and moved to another
page then this method's execution is stopped. When the user comes back to view the web page again,
startO method execution will resume, Any calculations and processing of data should be done in this
method and the results are also displayed.
public void stop () : This method is called by the browser when the applet is to be stopped. If the user
minimizes the web page, then this method is called and when the user again comes back to this page, then
start() method is called. In this way, startO and stopO methods can be called repeatedly. For example, an
applet with some animation might want to use the startO method to resume animation, and the stop()
method to suspend the animation.
public void destroy(): This method is called when the applet is being terminated from memory. the stop()
method will always be called before destroy(). the code related releasing the memory allocated to the
applet and stopping any running threads should be written in this method.
Executing init(), start(), stop() and destroy() methods in that sequence is called 'life cycle' of an applet.
Note that none of these methods are compulsory while writing an applet.
implementation
1. using html file
<html>
<applet>
code, width, height
</applet>
</html>
the drawback of the use of html file is an applet plugin should be install in web browser,
otherwise not displayed anything in the web browser
2. appletviewer tool
after successful compilation, just using appletviewer filename.java
import java.applet.*;
import java.awt.*;
}
next we are embedding this class file into html file
i.e <html>
<applet code=Test2.class width=400 height=500>
</applet>
</html>
compilation
javac Test2.java
after succesful compilation
double click on Test.html
import java.applet.*;
import java.awt.*;
/*<html>
<applet code=Test2.class width=400 height=500>
</applet>
</html>
*/
public class Test2 extends Applet
{
public void init()
{
setBackground(Color.yellow);
}
public void pain(Graphics g)
{
g.drawString("Hello applets", 300, 400);
}
}
for execution
appletviewer Test2.java
import java.applet.*;
import java.awt.*;
/*
<applet code=Demo.class width=400 height=400>
</applet>
*/
}
public void start()
{
msg+="start";
}
in Color class, any color can represent in three major colors i.e Red, Green, Blue
Red Green Blue
0 - 255 0-255 0-255
g.setColor(object/static member);
g.setFont(f);
import java.awt.*;
import java.applet.*;
/*<applet code=ColorDemo width=500 height=500>
</applet>
public class ColorDemo extends Applet
{
public void init()
{ setBackground(Color.black);
setForeground(Color.white);
}
public void paint(Graphics g)
{
// Color c = new Color(255,0,0);
// g.setColor(c);
g.setColor(Color.red);
Font f = new Font("arial",0,30);
g.setFont(f);
g.drawString("Welcome",30,40);
}
}
}
public void paint(Graphics g)
{
Font f = new Font("Lucida Sans",2,14);
g.setFont(f);
g.drawString("Welcome", 40, 60);
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub