0% found this document useful (0 votes)
28 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)
28 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

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 [Link] package.
• So to create an applet ,import [Link]
package.
• Also import [Link] package to provide user
interface to an applet so that an applet can
have controls like buttons, checkbox etc.
Applet class Hierachy
[Link]

[Link]

[Link]

[Link]

[Link]
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 .[Link] 2. [Link]
import [Link];
<html> public class applet1 extends Applet
<body> {
public void init()
<applet code=[Link] width=200 {
height=200> [Link]("applet initialized");
}
</applet> public void start()
{
</body> [Link]("applet started");
</html> }
public void stop()
----------------------------------- {
3. Compile [Link] [Link]("applet stopped");
}
4. Open [Link] file in browser public void destroy()
{
[Link]("applet destroyed");
}
}
public void stop()
[Link] {
import [Link]; [Link]("applet stopped");
/* <applet code=[Link] width=200 }
height=200> public void destroy()
{
</applet>*/ [Link]("applet destroyed");
}
public class applet1 extends Applet }
{
public void init() -----------------------------------
{ 3. Compile [Link]
[Link]("applet initialized");
} 4. Run using
public void start() > appletviewer [Link]
{
[Link]("applet started");
}
Using paint() method
• To display messages/graphics in an applet.
• paint() is method of [Link] class
display message in applet using paint()
method
[Link]
import [Link].*;
import [Link].*;

/* <applet code=[Link] width=200 height=200>


</applet>*/

public class applet1 extends Applet


{
public void paint(Graphics g)
{
[Link]("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 [Link].*;
import [Link].*; >javac [Link]
> appletviewer [Link]
/* <applet code=[Link]
width=200 height=200>
</applet>*/

public class DrawLine extends Applet


{
public void paint(Graphics g)
{
[Link](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 [Link].*; >javac [Link]


import [Link].*; > appletviewer [Link]

/* <applet code=[Link]
width=200 height=200>
</applet>*/

public class DrawRect extends Applet


{
public void paint(Graphics g)
{
[Link](20,20,100,100);
}
}
import [Link].*;
import [Link].*;
/*<applet code=[Link] width=200
height=200>
</applet>*/
public class DemoApplet extends Applet
{
public void paint(Graphics g)
{
[Link](50,50,60,60);
}
}
import [Link].*;
import [Link].*;
/*<applet code=[Link] width=200
height=200>
</applet>*/
public class DemoApplet extends Applet
{
public void paint(Graphics g)
{
[Link](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 [Link].*; >javac [Link]


import [Link].*; > appletviewer [Link]

/* <applet code=[Link]
width=200 height=200>
</applet>*/

public class DrawOval extends Applet


{
public void paint(Graphics g)
{
[Link](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 [Link].*; >javac [Link]


import [Link].*; > appletviewer [Link]

/* <applet code=[Link] width=200


height=200>
</applet>*/

public class DrawArc extends Applet


{
public void paint(Graphics g)
{
[Link](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 [Link].*; int num=8;
import [Link].*;
[Link](xpoints,ypoints,num);
/* <applet code=[Link] 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:
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
Program to change background color of applet as
“green” and foreground color of applet as “red”
import [Link].*;
import [Link].*;
/* <applet code=[Link]
width=200 height=200>
</applet>*/

public class AppletColor extends Applet


{
public void init()
{
setBackground([Link]);
setForeground([Link]);
}
public void paint(Graphics g)
{
[Link](“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 [Link].*;
import [Link].*;

/* <applet code=[Link]
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");
[Link](c,50,50);
}
}
import [Link].*; if ( (row % 2) == (col % 2) )
import [Link].*; [Link]([Link]);
/* <applet code=[Link] height=200 else
width=200> [Link]([Link]);
</applet>*/
public class Demo extends Applet { [Link](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