Applet 11
Applet 11
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
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.*;
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>*/
2) drawRect()
• To draw a rectangle.
• Syntax:
void drawRect(int top,int left,int right,int
bottom);
Program using drawRect()
/* <applet code=DrawRect.class
width=200 height=200>
</applet>*/
3) drawOval()
• To draw an oval.
• Syntax:
void drawOval(int top,int left,int right,int
bottom);
Program using drawOval()
/* <applet code=DrawOval.class
width=200 height=200>
</applet>*/
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()
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>*/
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>*/
/* <applet code=AppletParamPass.class
width=200 height=200>
</applet>*/