BY MAHMOUD HOSSEINPOOR
Is a Java program that runs with the help of a web
browser
All applets are sub-classes of the class ‘Applet’
To create an applet, you need to import the following
two packages:
java.applet
java.awt
BY: MAHMOUD Java Applets 2
An applet defines its structure from four events that take
place during the execution
For each event, a method is automatically called
Methods
init( )
start( )
stop( )
destroy( )
BY: MAHMOUD Java Applets 3
Other related methods
paint( )
repaint( )
showStatus( )
getAppletInfo( )
Applet methods init(), start(), stop(), destroy(), and
paint() are inherited by an applet
Each of these methods is empty by default. Hence, these
methods are overridden
BY: MAHMOUD Java Applets 4
An applet is compiled using the syntax
javac Applet1.java
To execute an applet, create an HTML file
which uses the applet tag
The applet tag has two attributes
▪ Width
▪ Height
To pass parameters to the applet, use the
‘param’ tag, followed by the ‘value’ tag
Applet can be executed using an applet
viewer BY: MAHMOUD Java Applets 5
Import java.applet.Applet;
Import java.awt.Graphics;
Public class HelloWorldApplet extends Applet{
public void paint(Graphics g){
g.drawString(“Hello World”,50,35);
}
}
BY: MAHMOUD Java Applets 6
<html>
<head>
<title>Hello World</title>
</head>
<body>
This is an applet<p>
<applet codebase=“classes” code=“HelloWorldApplet.class”
width=200 height=300></applet>
</body>
</html>
BY: MAHMOUD Java Applets 7
Applications run using the Java interpreter, while
applets run on any browser that supports Java, or use an
‘AppletViewer’ that is included in the JDK
The execution of applications begins with the ‘main()’
method. This is not the case with applets
Applications use the ‘System.out.println()’ to display the
output, while applets use the ‘drawstring()’ method to
display the output
BY: MAHMOUD Java Applets 8
Cannot read or write files on the user’s file system
Cannot communicate with an Internet site, but only with
one that serves the web page, including the applet
Cannot run any programs on the reader’s system
Cannot load any programs stored on the user’s system
BY: MAHMOUD Java Applets 9
BY: MAHMOUD Java Applets 10
To pass parameter, the PARAM parameter is to be
included in HTML tag
Example
<applet code = "Mybutton1" width = “100” height = “100”>
<PARAM NAME = “Mybutton” value = “Display Dialog”>
</applet>
BY: MAHMOUD Java Applets 11
Provided by AWT package
Provides a collection of methods to draw the following
graphical figures
Oval
Rectangle
Square
Circle
Lines
Text in different fonts
BY: MAHMOUD Java Applets 12
Methods to draw and fill polygons
Syntax
drawPolygon(int x[ ], int y[ ], int numPoints);
fillPolygon(int x[ ], int y[ ], int numPoints);
BY: MAHMOUD Java Applets 13