Unit No.1
Unit No.1
UNIT :- I
Applet
Reference Book
E. Balagurusamy, “Programming With JAVA” , Tata McGraw
Hill, 6th Edition
1. Applets do not use the main() method for initiating the execution of the
code
2. Unlike stand alone application, applets can not be run independently
3. Applets can not read form or write to the files in the local computer
4. Applets can not communicate with other servers on the network
5. Applets can not run any program from the local computer
Preparing to Write Applets
Before we try to write applets, we must make sure that Java is installed
properly, and also ensure that either the Java appletviewer or a Java
enabled browser is available
Steps involved in developing and testing in applet are
<HTML> Comment
<! Section
---- -------
-------------
>
Head Section
<HEAD>
Title Tag
</HEAD>
Body Section
<BODY>
Applet Tag <applet code = “AppletClassName.class” width
</BODY> =“300” Height=“300”>
</applet>
</HTML>
Save as .html and use appletviewer filename.html
Applet Life Cycle
Basic Methods of Applet
public void start()- start() called after the init() method and it
starts an applet
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello Java", 100,100);
}
}
/*
<html>
<body>
<applet code= "HelloJava.class" width ="300"
height="300" >
</applet>
</body>
</html>
*/
*/
Example
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void init()
{
resize(200,200);
setBackground(Color.cyan);
}
public void paint(Graphics g)
{
g.drawString("Hello Java", 100,100);
}
}
/*
<html>
<body>
<applet code= "HelloJava.class" width ="300"
height="300" >
</applet>
</body>
</html>
import java. applet.*; g.drawString(msg,10,30);
import java. awt.*; }
}
public class Sample extends Applet
{
String msg; /*
public void init() <html>
{ <body>
setBackground(Color.red); // set background <applet code="Sample.class" width="300"
and forground color height="300">
of applet </applet>
window </body>
setForeground(Color.black); </html>
msg="Hello Friends .... "; // initialize the */
string to dispaly
}
public void start()
{
msg += "How are you?....";
}
public void paint(Graphics g)
{
msg+= "Bye…….";
import java. applet.*; {
import java. awt.*; msg+= "Bye…….";
g.setFont(f1);
public class Sample extends Applet g.drawString(msg,10,30);
{ }
String msg; }
Font f1;
public void init()
{ /*
setBackground(Color.red); // set background <html>
and forground color <body>
of applet <applet code="Sample.class" width="300"
window height="300">
setForeground(Color.black); </applet>
msg="Hello Friends .... "; // initialize the </body>
string to display </html>
f1 =new Font (“Aerial”,Font.BOLD, 18); */
}
public void start()
{
msg += "How are you?....";
}
public void paint(Graphics g)
Passing a Parameter to Applet
import java.applet.Applet; /*
import java.awt.Graphics; <html>
<body>
public class Test extends Applet <applet code="Test.class"
{ width="300" height="300">
String str; <param name="string"
public void init() value="Applet">
{ </applet>
</body>
str=getParameter("string"); </html>
if (str ==null) */
str= "Java";
str="Hello"+str;
}
public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}
Passing a Parameter to Applet
import java.awt.Graphics;
import java.applet.Applet; public void paint(Graphics g)
{
public class Rectangle extends Applet g.drawRect(x,y,w,h);
{ }
int x,y,w,h; }
public void init() /*
{ <html>
<body>
x=Integer.parseInt(getParameter("xValue" <applet code="Rectangle.class"
)); width="300" height="300">
<param name="xValue" value="20">
y=Integer.parseInt(getParameter("yValue" <param name="yValue" value="40">
)); <param name="wValue"
value="100">
w=Integer.parseInt(getParameter("wValue <param name="hValue"
")); value="120">
</applet>
h=Integer.parseInt(getParameter("hValue" </body>
)); </html>
Passing a Parameter to Applet
import java.awt.*; String s1,s2,s;
import java.applet.*; g.drawString("Input a }
number in each box",10,50); /*
public class UserIn extends <html>
Applet try <body>
{ { <applet code
TextField text1, text2; s1=text1.getText(); ="UserIn.class" width="300"
public void init() x= Integer.parseInt(s1); height="300">
{ s2=text2.getText(); </applet.
text1=new TextField(8); y=Integer.parseInt(s2); </body>
text2=new TextField(8); } <html>
add(text1); catch(Exception e) */
add(text2); {}
text1.setText("0"); z=x+y;
text2.setText("0"); s= String.valueOf(z);
} g.drawString("The sum
public void paint(Graphics g) is:",10,75);
{ g.drawString(s,100,75);
int x=0,y=0,z=0; }
Passing a Parameter to Applet
getCodeBase() and getDocumentBase()