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

Applets-

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

init()- when the applet inilizes then this method is fired.

Syntax

public void init()

body of the method

start()- when the applet start the execution then this method fires.

Syntax

public void start()

body of the method

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

public void paint(Graphics g)


{

body of the method

stop()- when the applet stops the execution then this method fires.

Syntax

public void stop()

body of the method

destroy()- when the applet release the memory then this method is fires.

Syntax

public void destroy()

body of the method

Executing an applet-for executing an applet we can follow one of the following two
techniques-

1. Creating a seperate html file

2. using appletviewer tool

in both the above cases you must use the <applet> tag like the following-

<applet code="classname" width=value height=value>

</applet>
Example

import java.applet.*;

import java.awt.*;

public class life extends Applet

public void init()

System.out.println("Applet Initlized");

public void start()

System.out.println("Applet Starts the Execution");

public void paint(Graphics g)

g.drawString("Welcome Friends",10,20);

System.out.println("Applet Activated");

public void stop()

System.out.println("Applet Stops the Execution");

public void destroy()

{
System.out.println("Applet Release The Memory");

/*

<applet code="life" width="300" height="300">

</applet>

*/

for compiling use

javac programname.java

for run

appletviewer programname.java

_______________________________________________

Methods of Applet Class

1. setBackground()- sets the background Color

2. setForeground()- sets the font color

Working with Graphics Class-Graphics class provides a lot of methods for creating an
interactive objects, some of them are-

1. setColor()- sets the Color into the object

2. setFont()- sets the font style

3. drawString()- draws a message on Applet

Syntax

drawString(String msg,x,y);

4. drawLine()- draws a line on the applet

Syntax
drawLine(int x1,int y1,int x2,int y2)

5. drawRect()- draws a Rectangle on the applet

Syntax

drawRect(int x,int y,int width,int height)

6. drawOval()- draws an Oval on the applet

Syntax

drawOval(int x,int y,int width,int height)

7. fillRect()- draws a Solid Rectangle on the applet

Syntax

fillRect(int x,int y,int width,int height)

8. fillOval()- draws a Solid Oval on the applet

Syntax

fillOval(int x,int y,int width,int height)

9. drawImage()- draws on Image on The Applet

Syntax

drawImage(Imageobject,int x,int y,int width,int height,this)

Color Class - This class is used for creating a Color object.

Constructor

Color(int red,int green,int blue)

The range will be-

0-255 for each

Font Class - This class construct a Font Object

Constructor
Font(String FontName,int Style,int Size)

________________________________________________________________

Example

import java.applet.*;

import java.awt.*;

public class life extends Applet

Color c;

Font f1,f2;

public void init()

c=new Color(0,255,0);

f1=new Font("Impact",Font.PLAIN,35);

f2=new Font("Arial",Font.PLAIN,20);

public void paint(Graphics g)

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 code="life" width="400" height="400">

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

public class image extends Applet

Image img;

public void init()

img=getImage(getCodeBase(),"login.jpg");
}

public void paint(Graphics g)

g.drawImage(img,0,0,90,90,this);

/*

<applet code="image" width="400" height="400">

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

public class myanimation1 extends Applet

int c,x,y,x1,y1,x2,y2;

Font f;

public void init()

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

public void paint(Graphics g)

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 code="myanimation1" height=200 width=300>

</applet>

*/

________________________________________________

Example 2

import java.applet.*;

import java.awt.*;

public class image extends Applet

Image img1,img2,img3,img4;

int c;

public void init()

{
img1=getImage(getCodeBase(),"login.jpg");

img2=getImage(getCodeBase(),"Nokia.jpg");

img3=getImage(getCodeBase(),"Sony.jpg");

img4=getImage(getCodeBase(),"Samsung.jpg");

c=1;

public void paint(Graphics g)

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 code="image" width="400" height="400">

</applet>

*/

______________________________________________________________________________

Working with AWT Componenets-

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-

1. Label Class-used for showing any message on the window.

Constructor

Label()

Label(String s)

Label(String s,int align)

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)

TextField(String text,int size)

Methods

setText()

getText()

3. Button- This class is used for creating a command button.

Constructors

Button()

Button(String label)

Methods

setLabel()

getLabel()

Example

import java.applet.*;

import java.awt.*;

public class awt extends Applet

Label l1;

TextField t1;

Button b1;

public void init()

{
l1=new Label("Enter Your Name ");

t1=new TextField(10);

b1=new Button("Click Here");

add(l1);

add(t1);

add(b1);

/*

<applet code="awt" width="400" height="200">

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

Checkbox(String s,boolean select)

Checkbox(String s,CheckboxGroup object,boolean select) //create a radio button

Example

import java.applet.*;

import java.awt.*;

public class awt extends Applet


{

Label l1,l2;

Checkbox cb1,cb2,cb3,cb4,cb5;

CheckboxGroup cbg;

public void init()

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 code="awt" width="400" height="200">

</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()

List(int nor,boolean select)

Methods

1. addItem() or add()

2. remove() or removeItem()

3. getSelectedItem()

Example

import java.applet.*;

import java.awt.*;

public class list extends Applet


{

Label l1,l2;

Choice c1;

List li1;

public void init()

l1=new Label("Select Name");

l2=new Label("Select City");

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 code="list" height=200 width=250>

</applet>

*/

_______________________________________________________

TextArea Class - this class used for creating a multiline input textbox

Constructors

TextArea(rows,cols)

Methods

setText()

getText()

You might also like