Chapter 14
Chapter 14
14 APPLET
Advantages of Applet:
Applets are cross platform and can run on Windows, Mac OS and Linux platform
Applets can work all the version of Java Plugin
Applets runs in a sandbox, so the user does not need to trust the code, so it can work
without security approval
Applets are supported by most web browsers
Applets are cached in most web browsers, so will be quick to load when returning to a
web page
User can also have full access to the machine if user allows
Applet Application
Applets are the small programs applications are larger programs.
Applets don't have the main method Applications must have a main().
Java Applets are also compiled using the javac Java applications are compiled using the javac
command, but are run either with a browser or command and run using the java command.
with the appletviewer command.
An Applet also requires an HTML file before it Requires JRE
can be executed. And it need web browser to
execute the file.
Requires highest security for the system as Does not require any security
they are untrusted
cannot access anything on the system except Can access any data or software available on
browser’s services the system
14.2 Java Programming Paradigm
java.lang.Object
java.awt.Component
java.awt.Panel
java.applet.Applet
Applet is initialized.
Applet is started.
Applet is painted.
Applet is stopped.
Applet is destroyed.
init() method
start() method
stop() method
destroy() method
Fig. No: 14.2 Applet Life Cycle
init() method: The life cycle of an applet is begin on that time when the applet is first loaded
into the browser and called the init() method. The init() method is called only one time in the life
cycle on an applet.
start() method: The start method of an applet is called after the initialization method init(). This
method may be called multiples time when the Applet needs to be started or restarted.
Applet 14.3
stop() method: The stop() method can be called multiple times in the life cycle of applet like the
start () method. Or should be called at least one time. There is only miner difference between the
start() method and stop () method. For example the stop() method is called by the web browser
on that time When the user leaves one applet to go another applet and the start() method is called
on that time when the user wants to go back into the first program or Applet.
destroy() method: The destroy() method is called only one time in the life cycle of Applet like
init() method. This method is called only on that time when the browser needs to shut down.
public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that
can be used for drawing oval, rectangle, arc etc.
}
Example 14.1 Applet program by html file
Note: class must be public because its object is created by Java Plugin software that resides on
the browser.
myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
Example 14.1(Continue) Applet program by html file
14.4 Java Programming Paradigm
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
Example 14.2 Applet Program by appletviewer tool
To execute the applet by appletviewer tool, write in command prompt:
c:\>javac First.java
c:\>appletviewer First.java
public abstract void drawString(String str, int x, int y): is used to draw the specified string.
public void drawRect(int x, int y, int width, int height): draws a rectangle with the specifed
width and height.
public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the
default color and specified width and height.
public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with
the specified width and height.
public abstract 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).
public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is
used draw the specified image.
Applet 14.5
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.
import java.applet.Applet;
import java.awt.*;
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}
}
Example 14.3 Graphics Program in applet:
myapplet.html
<html>
<body>
<applet code=" GraphicsDemo.class " width="300" height="300">
</applet>
</body>
</html>
Example 14.3(Continue) Graphics Program support by html
/*
<applet code="LifeCycle.class" width=200 height=200></applet>
*/
import java.applet.Applet;
import java.awt.*;
public class LifeCycle extends Applet
{
14.6 Java Programming Paradigm
I:\java\graphics>appletviewer LifeCycle.java
Initializing...
Starting...
Stopping...
Starting...
Stopping...
Starting...
Stopping...
Destroying...
Applet 14.7
Output:
Review Questions
1. Which classes can an applet extend?
2. What is the difference between applet and application program?
3. What is the order of method invocation in an applet?