Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Applet Class Notes PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Applets

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:

Applet = Java byte code + HTML page.

a java program which is embedded in the webpage


1.Generating dynamic content
2. works at client side
3. response time is fast
4. import java.awt.applet.*;

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.

What is applet life cycle?

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.*;

public class Test2 extends Applet


{
public void init()
{
setBackground(Color.yellow);
}
public void paint(Graphics g)
{
g.drawString("Hello applets", 300, 400);
}

}
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

here is a drawback, we need to install java applet plugin in web browser


2nd way to implement is
we can include the following applet code in java program

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

// Java program for applet life cycle

import java.applet.*;
import java.awt.*;
/*
<applet code=Demo.class width=400 height=400>
</applet>
*/

public class Demo extends Applet


{
String msg="";
public void init()
{
//Color c1 = new Color(0,255,0);
setBackground(Color.yellow);
//setForeground(c1);
setForeground(Color.green);
msg+="init";

}
public void start()
{
msg+="start";
}

public void paint(Graphics g)


{
g.drawString("Welcome",40,10);
g.drawString(msg,50,60);
}
public void stop()
{
msg+="stop";
}
public void destroy()
{
msg+="destroy";
}
}
Color and Font Classes

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);

Color class is in awt pack


Color c1 = new Color(0,0,0); ---- Black
c2 255,255,255 -- White
c3 255,0,0 ---- Red
c4 0,255,0 --- Green
c5 0,0,255 --- Blue
g.setColor(c1);

static members or constants of Color class are


blue, black, green, white, yellow, pink
g.setColor(Color.blue);

Font Class -------awt


g.setFont(object)
Font f = new Font(String str, int, int);
String str -- Font face int --- Font style int - Font size
--> Times New roman 0-- plain
----> Arial, 1 -- Bold
-----> Comic Sans Ms 2 -- Italic
3 -- Bold+italic

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);
}
}

// java program for creation Buttons in applets


import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FontDemo extends Applet implements ActionListener


{
public void init()
{
setBackground(Color.yellow);
setForeground(Color.red);

Button b1 = new Button("Red");


add(b1);
Button b2 = new Button("Green");
add(b2);
Button b3 = new Button("Blue");
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

}
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

String str = e.getActionCommand();


if(str.equals("Red"))
setBackground(Color.red);
else if(str.equals("Green"))
setBackground(Color.green);
else
setBackground(Color.BLUE);

You might also like