0% found this document useful (0 votes)
22 views35 pages

Applet 11

Uploaded by

Janhavi More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views35 pages

Applet 11

Uploaded by

Janhavi More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Applet

Applet
• Applet is java program that can be embedded
into HTML pages.
• Java applets runs on web browsers such as
Mozilla and internet explorer.
• Applet is designed to run remotely on the
client browser. Applets are used to make the
web site more dynamic and entertaining.
Application Applet

An application is used to design stand An applet is used to design web


alone application. application.
Application contains main() method for Applet does not contain main() method.
initiating the execution of code. Applets, when loaded, automatically calls
certain methods of Applet class to start
and execute the applet code.
Creating applets
• All applets are subclasses of Applet class
which is available in java.applet package.
• So to create an applet ,import java.applet
package.
• Also import java.awt package to provide user
interface to an applet so that an applet can
have controls like buttons, checkbox etc.
Applet class Hierachy
Java.lang.Object

Java.awt.Component

Java.awt.Container

Java.awt.Panel

Java.applet.Applet
Life cycle of applet
• Stages of life cycle of an applet :
1) init() 2) start()
3) paint() 4) stop() 5) destroy()
init ( ): The init( ) method is the first method to be called. This is where
you should initialize Variables. It loads applet into memory.
This method is called only once during the run time of your applet.
start( ): The start( ) method is called after init( ). start() method starts the
execution of an applet. It is also called to restart an applet after it has Been
stopped. Whereas init( ) is called once—the first time an applet is
loaded—start( )is called each time an applet’s HTML document is
displayed onscreen.
paint ( ): The paint ( ) method is called each time your applet’s output
must be redrawn. paint ( ) is also called when the applet begins execution.
Whatever the cause, whenever the applet must redraw its output, paint( )
is called. The paint ( ) method has one parameter of type Graphics.
stop ( ): When stop ( ) is called, the applet is probably running. You
should use stop ( ) to suspend threads that don’t need to run when the
applet is not visible.
destroy( ): The destroy ( ) method is called when the environment
determines that your applet needs to be removed completely from
memory.
Following options are used to run an applet
• Web browser
• Appletviewer
1 .abc.html 2. applet1.java
import java.applet.Applet;
<html> public class applet1 extends Applet
<body> {
public void init()
<applet code=applet1.class width=200 {
height=200> System.out.println("applet initialized");
}
</applet> public void start()
{
</body> System.out.println("applet started");
</html> }
public void stop()
----------------------------------- {
3. Compile applet1.java System.out.println("applet stopped");
}
4. Open abc.html file in browser public void destroy()
{
System.out.println("applet destroyed");
}
}
public void stop()
applet1.java {
import java.applet.Applet; System.out.println("applet stopped");
/* <applet code=applet1.class width=200 }
height=200> public void destroy()
{
</applet>*/ System.out.println("applet destroyed");
}
public class applet1 extends Applet }
{
public void init() -----------------------------------
{ 3. Compile applet1.java
System.out.println("applet initialized");
} 4. Run using
public void start() > appletviewer applet1.java
{
System.out.println("applet started");
}
Using paint() method
• To display messages/graphics in an applet.
• paint() is method of java.awt.Component class
display message in applet using paint()
method
applet1.java
import java.applet.*;
import java.awt.*;

/* <applet code=applet1.class width=200 height=200>


</applet>*/

public class applet1 extends Applet


{
public void paint(Graphics g)
{
g.drawString("Welcome in Java Applet.",40,20);
}
}
Methods of Graphics class

1) drawLine()
• To draw a line.
• Syntax:
void drawLine(int x1,int y1,int x2,int y2);
• This method draws line from (x1,y1)
to(x2,y2).
Program using drawLine()

import java.applet.*;
import java.awt.*; >javac DrawLine.java
> appletviewer DrawLine.java
/* <applet code=DrawLine.class
width=200 height=200>
</applet>*/

public class DrawLine extends Applet


{
public void paint(Graphics g)
{
g.drawLine(20,20,100,100);
}
}
Methods of Graphics class

2) drawRect()
• To draw a rectangle.
• Syntax:
void drawRect(int top,int left,int right,int
bottom);
Program using drawRect()

import java.applet.*; >javac DrawRect.java


import java.awt.*; > appletviewer DrawRect.java

/* <applet code=DrawRect.class
width=200 height=200>
</applet>*/

public class DrawRect extends Applet


{
public void paint(Graphics g)
{
g.drawRect(20,20,100,100);
}
}
import java.awt.*;
import java.applet.*;
/*<applet code=DemoApplet.class width=200
height=200>
</applet>*/
public class DemoApplet extends Applet
{
public void paint(Graphics g)
{
g.fillRect(50,50,60,60);
}
}
import java.awt.*;
import java.applet.*;
/*<applet code=DemoApplet.class width=200
height=200>
</applet>*/
public class DemoApplet extends Applet
{
public void paint(Graphics g)
{
g.drawRoundRect(50,50,60,60,10,10);
}
}
Methods of Graphics class

3) drawOval()
• To draw an oval.
• Syntax:
void drawOval(int top,int left,int right,int
bottom);
Program using drawOval()

import java.applet.*; >javac DrawOval.java


import java.awt.*; > appletviewer DrawOval.java

/* <applet code=DrawOval.class
width=200 height=200>
</applet>*/

public class DrawOval extends Applet


{
public void paint(Graphics g)
{
g.drawOval(15,25,60,150);
}
}
Methods of Graphics class

3) drawArc()
• To draw an arc.
• Syntax:
void drawArc(int top,int left,int right,int
bottom,int start_angle,int end_angle);
Program using drawArc()

import java.applet.*; >javac DrawOval.java


import java.awt.*; > appletviewer DrawOval.java

/* <applet code=DrawArc.class width=200


height=200>
</applet>*/

public class DrawArc extends Applet


{
public void paint(Graphics g)
{
g.drawArc(50,50,100,100,0,180);
}
}
Methods of Graphics class

3) drawPolygon()
• To draw polygon.
• Syntax:
void drawPolygon(int xpoints[],int
ypoints[],int num_points);
Program using drawPoly()
import java.applet.*; int num=8;
import java.awt.*;
g.drawPolygon(xpoints,ypoints,num);
/* <applet code=DrawPoly.class width=200 }
height=200> }
</applet>*/

public class DrawPoly extends Applet


{
public void paint(Graphics g)
{

int xpoints[]={25,75,125,85,125,75,25,65};

int ypoints[]={50,90,50,100,150,110,150,100};
Setting Background and Foreground Color :
To set the color of the background of an applet window, setBackground () method is used.
The general form of the setBackground () method is
void setBackground(mycolor)
Similarly, to set the foreground color to a specific color, that is, the color of text,
setForeground () method is used.
The general form of the setForeground () method is
void setForeground(mycolor)
where, mycolor is one of the color constants or the new color created by the user
The list of color constants is given below:
• Color.red
• Color.orange
• Color.gray
• Color.darkGray
• Color.lightGray
• Color.cyan
• Color.pink
• Color.white
• Color.blue
• Color.green
• Color.black
• Color.yellow
Program to change background color of applet as
“green” and foreground color of applet as “red”
import java.applet.*;
import java.awt.*;
/* <applet code=AppletColor.class
width=200 height=200>
</applet>*/

public class AppletColor extends Applet


{
public void init()
{
setBackground(Color.pink);
setForeground(Color.red);
}
public void paint(Graphics g)
{
g.drawString(“Hello”,60,100);
}
Passing parameters to Applets
• We can pass parameters to an applet using
<applet> tag.
• Parameters are passed to applets in NAME-
VALUE pair in <PARAM> tags between the
opening and closing APPLET tags
• getParameter() returns the value of the
parameter that is set in the <PARAM> tag.
import java.applet.*;
import java.awt.*;

/* <applet code=AppletParamPass.class
width=200 height=200>

<param name=company value="tcs">

</applet>*/

public class AppletParamPass extends


Applet
{
public void paint(Graphics g)
{
String c;
c=getParameter("company");
g.drawString(c,50,50);
}
}
import java.awt.*; if ( (row % 2) == (col % 2) )
import java.applet.*; g.setColor(Color.white);
/* <applet code=Demo.class height=200 else
width=200> g.setColor(Color.black);
</applet>*/
public class Demo extends Applet { g.fillRect(x, y, 20, 20);
}
public void paint(Graphics g) {
int row; }
int col;
int x,y; }

for ( row = 0; row < 8; row++ ) }


{

for ( col = 0; col < 8; col++) {


x = col * 20;
y = row * 20;

You might also like