You are on page 1of 16

Applet

Programming

1
Introduction
• Applets are small Java programs that are primary used in
Internet computing.
• They can be transported over the Internet from one
computer to another and run using the Applet Viewer or
any Web browser that supports Java.
• It can perform arithmetic operations, display graphics, play
sounds, accept user input, create animation, and play
interactive games.

Applets can embed into web pages in two ways.

• An applet developed locally and stored in a local system is


known as local Applet.
• A remote applet is that which is developed by someone
else and stored on a remote computer connected to the
internet.
2
Local and Remote Applets

Local Applet

Local Computer
Loading local applets

Internet

Local Computer Remote Computer


(Client) Remote (Server)
Applet
Loading a remote applets 3
How Applets Differ from
Applications
• Applets don’t use the main() methods for initiating the
execution of the code. Applets, when loaded, automatically
call certain methods of Applet class to start and execute
the applet code.
• Unlike stand-alone applications, applets can’t be run
independently. They are run from inside a web page using
a special feature known as HTML tag.
• Applets can’t read from or write to the files in the local
computer.
• Applet can’t communicate with other servers on the
network
• Applets can’t run any program from the local computer.
• Applets are restricted from using libraries from other
language such as C or C++.
4
The HelloJava Applet
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Hello Java”, 10, 100);
}
}

5
Chain Of Classes inherited by
Applet class

6
Applet Life Cycle

7
Adding Applet to HTML File
<HTML>
<HEAD>
<TITLE>
WELCOME TO JAVA APPLETS
</TITLE>
</HEAD>
<BODY>
<CENTER>
<H1> Welcome to the world of Applets </H1>
</CENTER><BR>
<CENTER>
<APPLET
CODE = HelloJava.class
WIDTH = 400
HEIGHT = 200>
</APPLET>
</CENTER>
</BODY>
</HTML>
8
Applet HelloJavaParam
import java.awt.*;
import java.applet.*;
public class HelloJavaParam extends Applet
{
String str;
public void init( )
{
str = getParameter(“string”);
if(str == null)
str = “Java”;
str = “Hello” + str;
}
public void paint(Graphics g)
{
g.drawString(str, 10, 100);
}
}
9
The HTML file for HelloJavaParam
applet
<HTML>
<! PARAMETERIZED HTML FILE >
<HEAD>
<TITLE> Welcome to java Applets </TITLE>
</HEAD>
<BODY>
<APPLET CODE = HelloJavaParam.class
WIDTH = 400
HEIGHT = 200>
<PARAM NAME = “string”
VALUE = “Applet! ” >
</APPLET>
</BODY>
</HTML>
10
Displays of HelloJavaParam
applet

Output without PARAM tag

11
Displaying numerical values
import java.awt.*;
import java.applet.*;
pubic class NumValues extends Applet
{ public void paint (Graphics g)
{ int value1 = 10;
int value2 = 20;
int sum = value1 + value2;
String s = “sum : ” + String.valueOf(sum);
g.drawString(s, 100, 100);
}
}
• HTML file
<html>
<applet code = NumValues.class
width = 300 height = 300>
</applet>
</html>
12
Displaying numerical values
Output of Program

13
Getting Input from the User
import java.awt.*;
import java.applet.*;
public class UserIn extends Applet{
TextField text1, text2;
public void init()
{
text1= new TextField(8);
text2= new TextField(8);
add(text1);
add(text2);
text1.setText("0");
text2.setText("0");
}
public void paint (Graphics g)
{
int x=0, y=0, z=0;
String s1, s2, s;
g.drawString("Input a number in each box", 10, 50);
14
try
{
s1= text1.getText();
x= Integer.parseInt(s1);
s2=text2.getText();
y=Integer.parseInt(s2);
}
catch(Exception ex){}
z=x+y;
s=String.valueOf(z);
g.drawString("THE SUM IS : ", 10, 75);
g.drawString(s, 100, 75);
}
public boolean action(Event event,Object object)
{
repaint();
return true;
}
}

15
Output of Program

16

You might also like