Professional Documents
Culture Documents
Applet
Applet
Applets are used for creating interactive object for a web page.They are embedded on a web
page.for creating an applet you must import java.applet package & must inherit the Applet
class.
Applet Life Cycle- during execution applet follows a life cycle.and the life cycle will be-
init()--->start()---->paint()--------->stop()---------->destroy()
Syntax
start()- when the applet start the execution then this method fires.
Syntax
paint()- when the applet activates then this method is fires.It contrains Graphics class as
argument so you must import java.awt when you use this method.
Syntax
stop()- when the applet stops the execution then this method fires.
Syntax
destroy()- when the applet release the memory then this method is fires.
Syntax
Executing an applet-for executing an applet we can follow one of the following two
techniques-
in both the above cases you must use the <applet> tag like the following-
</applet>
Example
import java.applet.*;
import java.awt.*;
System.out.println("Applet Initlized");
g.drawString("Welcome Friends",10,20);
System.out.println("Applet Activated");
{
System.out.println("Applet Release The Memory");
/*
</applet>
*/
javac programname.java
for run
appletviewer programname.java
_______________________________________________
Working with Graphics Class-Graphics class provides a lot of methods for creating an
interactive objects, some of them are-
Syntax
drawString(String msg,x,y);
Syntax
drawLine(int x1,int y1,int x2,int y2)
Syntax
Syntax
Syntax
Syntax
Syntax
Constructor
Constructor
Font(String FontName,int Style,int Size)
________________________________________________________________
Example
import java.applet.*;
import java.awt.*;
Color c;
Font f1,f2;
c=new Color(0,255,0);
f1=new Font("Impact",Font.PLAIN,35);
f2=new Font("Arial",Font.PLAIN,20);
g.setFont(f1);
g.setColor(c);
g.drawString("Sushant IT College",120,50);
g.setColor(Color.blue);
g.drawLine(0,60,400,60);
g.setColor(Color.red);
g.setFont(f2);
g.drawString("Naini",195,80);
/*
</applet>
*/
________________________________________________________________
Embeding Image on The Applet- for embedding image on the applet you must use
drawImage() method & image must be in the current folder.
Example
import java.applet.*;
import java.awt.*;
Image img;
img=getImage(getCodeBase(),"login.jpg");
}
g.drawImage(img,0,0,90,90,this);
/*
</applet>
*/
______________________________________________________________________
using repaint() method- this method calles the paint() method recursively.so it is used for
creating an animated program.
Example 1
import java.applet.*;
import java.awt.*;
int c,x,y,x1,y1,x2,y2;
Font f;
{
c=0;
x=20;
y=30;
x1=270;
y1=180;
f=new Font("Arial",Font.BOLD,30);
x2=0;
y2=35;
setBackground(Color.blue);
setForeground(Color.white);
g.setFont(f);
x2+=15;
if(x2>=250)
x2=0;
g.drawString("Dhananjay",x2,y2);
if(c==0)
g.setColor(Color.red);
setBackground(Color.blue);
setForeground(Color.green);
}
else if(c==1)
g.setColor(Color.pink);
setBackground(Color.red);
setForeground(Color.blue);
else if(c==2)
g.setColor(Color.green);
setBackground(Color.yellow);
setForeground(Color.pink);
else if(c>2)
c=0;
c++;
x+=20;
y+=20;
if(x>=270)
x=20;
if(y>=180)
y=20;
x1-=20;
y1-=20;
if(x1<=20)
x1=270;
if(y1<=30)
y1=180;
g.fillOval(x,y,30,30);
g.fillOval(x1,y1,30,30);
for(long i=1;i<=100000000;i++);
repaint();
/*
</applet>
*/
________________________________________________
Example 2
import java.applet.*;
import java.awt.*;
Image img1,img2,img3,img4;
int c;
{
img1=getImage(getCodeBase(),"login.jpg");
img2=getImage(getCodeBase(),"Nokia.jpg");
img3=getImage(getCodeBase(),"Sony.jpg");
img4=getImage(getCodeBase(),"Samsung.jpg");
c=1;
if(c==1)
g.drawImage(img1,0,0,350,350,this);
else if(c==2)
g.drawImage(img2,0,0,350,350,this);
else if(c==3)
g.drawImage(img3,0,0,350,350,this);
else if(c==4)
g.drawImage(img4,0,0,350,350,this);
c=0;
c++;
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
repaint();
}
/*
</applet>
*/
______________________________________________________________________________
AWT stands for Abstract Window Toolkit , it is used for designing an user interface. for adding
a component to the window we will use add() and for removing components to the window
we will use remove method.The AWT Components are-
Constructor
Label()
Label(String s)
Label.LEFT
Label.CENTER
Label.RIGHT
Methods
setText()
getText()
2. TextField Class- This class construct an input field, & used for reading a value.
Constrcutors
TextField()
TextField(int size)
TextField(String text)
Methods
setText()
getText()
Constructors
Button()
Button(String label)
Methods
setLabel()
getLabel()
Example
import java.applet.*;
import java.awt.*;
Label l1;
TextField t1;
Button b1;
{
l1=new Label("Enter Your Name ");
t1=new TextField(10);
add(l1);
add(t1);
add(b1);
/*
</applet>
*/
________________________________________________
4. Checkbox Class- This class is used for creating a checkbox & for creating radio button we
must pass the object of CheckboxGroup class.
Constructors
Checkbox()
Checkbox(String s)
Example
import java.applet.*;
import java.awt.*;
Label l1,l2;
Checkbox cb1,cb2,cb3,cb4,cb5;
CheckboxGroup cbg;
l1=new Label("Hobbies");
l1.setForeground(Color.red);
l2=new Label("Gender");
cb1=new Checkbox("Cricket",true);
cb2=new Checkbox("Singing");
cb3=new Checkbox("Reading");
cbg=new CheckboxGroup();
cb4=new Checkbox("Male",cbg,true);
cb5=new Checkbox("Female",cbg,false);
add(l1);
add(cb1);
add(cb2);
add(cb3);
add(l2);
add(cb4);
add(cb5);
}
}
/*
</applet>
*/
_______________________________________________________________
6. Choice & List Class - Choice class is used for creating a dropdown list while list class is used
for ceating a list box.
Constructors
Choice()
List()
Constructors
List()
Methods
1. addItem() or add()
2. remove() or removeItem()
3. getSelectedItem()
Example
import java.applet.*;
import java.awt.*;
Label l1,l2;
Choice c1;
List li1;
c1=new Choice();
li1=new List(3,true);
c1.add("Ravi");
c1.add("Vipin");
c1.add("Janmejay");
c1.add("Shashank");
c1.add("Dev");
li1.add("Allahabad");
li1.add("Delhi");
li1.add("Kanpur");
li1.add("Lucknow");
add(l1);
add(c1);
add(l2);
add(li1);
}
/*
</applet>
*/
_______________________________________________________
TextArea Class - this class used for creating a multiline input textbox
Constructors
TextArea(rows,cols)
Methods
setText()
getText()