Applet is the client site designing in Java. To design applet there is a need to use applet api of java. Applet is the class of java applet package. It also uses the AWT api to design the client site. Applet has a life cycle. Applet does not have main function. To run appet we need to write HTML code. We can debug the applet in appletviewer also.
2. What is Applet
Applets are the dynamic client site components which
runs on the browser.
Client site components are used to forward request of
the user to server.
3. Features of Applets
•Runs only on client site
•Runs on browser
•Applet program does not require main
function
•Applets are dynamic
•Applet class is always public so that it can
call from outside
•To run applet HTML code is required
4. Limitation of Applets
•Applets are Heavy weight components
•Dependent on operating system
•Typical Java Coding
•Require plug-in to run on browser
6. Built-in Classes for Applet
To implement applet life cycle:
Applet class
Package: java.applet
To graphics design in the applet :
Graphics class
Package: java.awt
7. Applet Life Cycle Methods
Init : public void init()
-use to write the initialize code
Start : public void start()
-This method starts the applet
Paint : public void paint(Graphics obj)
-This method paint the contents on the applet
- Graphics class is awt class and use as parameter of paint method.
Stop : public void stop()
- This method stop the applet. Stopped applet can restart later by
start() method
Destroy : public void destroy()
- This method destroy the applet permanently. Once applet is
destroy it can’t be start.
8. Methods of Graphics class
void drawString(String str, int x, int y): is used to draw the specified string.
void drawRect(int x, int y, int width, int height): draws a rectangle with the
specified width and height.
void fillRect(int x, int y, int width, int height): is used to fill rectangle with the
default color and specified width and height.
void drawOval(int x, int y, int width, int height): is used to draw oval with the
specified width and height.
void fillOval(int x, int y, int width, int height): is used to fill oval with the default
color and specified width and height.
public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line
between the points(x1, y1) and (x2, y2).
boolean drawImage(Image img, int x, int y, ImageObserver observer): is used
draw the specified image.
9. public abstract void drawArc(int x, int y, int width, int height, int startAngle,
int arcAngle): is used draw a circular or elliptical arc.
public abstract void fillArc(int x, int y, int width, int height, int startAngle, int
arcAngle): is used to fill a circular or elliptical arc.
public abstract void setColor(Color c): is used to set the graphics current color
to the specified color.
public abstract void setFont(Font font): is used to set the graphics current font
to the specified font.
Methods of Graphics class - 2
10. How to run Applet
Applet can be run through HTML file with code :
<Applet Code = “Applet class name" Width = "400" Height =
"400"></Applet>
This code can be embedded in java code as comments :
/* <Applet Code = “Applet class name" Width = "400" Height = "400">
</Applet> */
11. Applet Viewer
It is utility provided by Java to debug and run the
Java Applet.
C:>Appletviewer HTML/Class file name
12. import java.applet.*;
import java.awt.Graphics;
import java.awt.Color;
import java.util.*;
public class TestApplet extends Applet
{
/*<Applet Code = "TestApplet.class" Width = "400" Height = "400"></Applet> */
public void init()
{
setBackground(Color.black);
setForeground(Color.yellow);
}
public void paint(Graphics g)
{ Date dt = new Date();
g.drawString("Hello",20,40);
g.drawString(dt,30,50);
}
}