2. Java Applet
Applet is a special type of program that is embedded in
the webpage to generate the dynamic content. It runs
inside the browser and works at client side.
Advantage of Applet
There are many advantages of applet. They are as
follows:
1. It works at client side so less response time.
2. Secured
3. It can be executed by browsers running under many
plateforms, including Linux, Windows, Mac Os etc.
Drawback of Applet
1. Plugin is required at client browser to execute applet.
BY LECTURER SURAJ PANDEY CCT
COLLEGE
3. There are some important differences between an applet and a
standalone Java application, including the following:
1. An applet is a Java class that extends the
java.applet.Applet class.
2. Applets are designed to be embedded within an HTML
page.
3. When a user views an HTML page that contains an
applet, the code for the applet is downloaded to the
user's machine.
4. A JVM is required to view an applet. The JVM can be
either a plug-in of the Web browser or a separate
runtime environment.
BY LECTURER SURAJ PANDEY CCT
COLLEGE
4. 5. Applets do not use the main() method for initiating the
execution of the code. Applets, when loaded, automatically
call certain methods of applet class to start and execute the
applet code.
6. Unlike stand alone applications, applets cannot be run
independently. They are run from inside a web page using a
special feature known as HTML tag.
7. Applets cannot communicate with other servers on the
network.
BY LECTURER SURAJ PANDEY CCT
COLLEGE
5. Preparing to write applets
We must make sure that java is installed properly and
also ensure that either java aplletviewer or a java-enabled
browser is available. The steps involved in developing and
testing on applet are:
1. Building an applet code(.java file)
2. Creating an executable applet(.class file)
3. Designing a web page using HTML tags
4. Preparing <APPLET> tag
5. Incorporating <APPLET> tag into the web page
6. Creating HTML file
7. Testing the applet code
BY LECTURER SURAJ PANDEY CCT
COLLEGE
6. Creating a applet program
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Hello World", 25, 50);
}
}
BY LECTURER SURAJ PANDEY CCT
COLLEGE
7. Creating a HTML base Applet
<html>
<title>The Hello, World Applet</title>
<hr>
<applet code="HelloWorldApplet.class" width="320"
height="120">
</applet>
<hr>
</html>
BY LECTURER SURAJ PANDEY CCT
COLLEGE
9. As displayed in the above diagram, Applet class extends
Panel. Panel class extends Container which is the subclass
of Component.
BY LECTURER SURAJ PANDEY CCT
COLLEGE
10. Applet life cycle
Every java applet inherit a set of default behaviors from
the Applet class. As a result, when an applet is loaded, it
undergoes a series of changes in its state as shown in fig.
The applet states include:
1. Born on initialization state
2. Running state
3. Idle state
4. Dead or destroyed state
BY LECTURER SURAJ PANDEY CCT
COLLEGE
13. Initialization state:
Applet enters the initialization state when it is first
loaded. This is achieved by calling the init() method of
Applet class. The applet is born. At this stage, we may do
the following if required.
Create objects needed by the applet
Set up initial values
Load images or fonts
Set up colors
The initialization occurs only once in the applet’s life cycle. To
provide any of the behaviors mentioned above, we must
override the init() method:
BY LECTURER SURAJ PANDEY CCT
COLLEGE
14. public void init()
{
………….
…………. (Action)
}
BY LECTURER SURAJ PANDEY CCT
COLLEGE
15. Running state:
Applets enter the running state when the system calls the
start() method of applet class. This occurs automatically
after the applet is initialized. Starting can also occur if the
applet is already is ‘stopped’ (idle) state. For example, we
may leave the web page containing the applet temporarily
to another page and return back to the page. This again
starts the applet running.
public void start()
{
…………….
…………….(Action)
}
BY LECTURER SURAJ PANDEY CCT
COLLEGE
16. Idle or stopped state:
An applet becomes idle when it is stopped from running,
stopping occurs automatically when we leave the page
containing the currently running applet. We can also do so by
calling the stop() method explicitly. If we use a thread to run
the applet, then we must use stop() method to terminate the
thread. We can achieve this by overriding the stop() method:
public void stop()
{
……………
…………. (Action)
…………..
}
BY LECTURER SURAJ PANDEY CCT
COLLEGE
17. Dead state:
An applet is said to be dead when it is removed from memory.
This occurs automatically by invoking the destroy() method
when we quit the browser. Like initialization, destroying stage
occurs only once in the applet’s life cycle. If the applet has
created any resources, like threads, we may override the
destroy() method to clean up these resources.
public void display()
{
…………..
…………..(Action)
……………..
}
BY LECTURER SURAJ PANDEY CCT
COLLEGE
18. Display state:
Applet moves to the display state whenever it has to
perform some operations on the screen. This happens
immediately after the applet enters into the running state.
The paint() method is called to accomplish this task.
Almost every applet will have a paint() method.
public void paint(Graphics g)
{
…………
…………(Display statements)
…………
}
BY LECTURER SURAJ PANDEY CCT
COLLEGE
19. After knowing the methods, let us know when they are called
by the browser.
init() method is called at the time of starting the execution.
This is called only once in the life cycle.
start() method is called by the init() method. This method is
called a number of times in the life cycle; whenever the applet
is deiconifed , to make the applet active.
paint() method is called by the start() method. This is called
number of times in the execution.
stop() method is called whenever the applet window
is iconified to inactivate the applet. This method is called
number of times in the execution.
destroy() method is called when the applet is closed. This
method is called only once in the life cycle.
BY LECTURER SURAJ PANDEY CCT
COLLEGE
20. java.awt.Component class
The Component class provides 1 life cycle method of
applet.
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.
BY LECTURER SURAJ PANDEY CCT
COLLEGE
21. How to run an Applet?
There are two ways to run an applet
1. By html file.
2. By appletViewer tool (for testing purpose).
BY LECTURER SURAJ PANDEY CCT
COLLEGE
22. Simple example of Applet by html file:
To execute the applet by html file, create an applet and
compile it. After that create an html file and place the
applet code in html file. Now click the html file.
BY LECTURER SURAJ PANDEY CCT
COLLEGE
23. //First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome",150,150);
}
}
BY LECTURER SURAJ PANDEY CCT
COLLEGE
25. Displaying Graphics in Applet
java.awt.Graphics class provides many methods for graphics programming.
Commonly used methods of Graphics class:
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 specified 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).
BY LECTURER SURAJ PANDEY CCT
COLLEGE
26. 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.
BY LECTURER SURAJ PANDEY CCT
COLLEGE
27. Example of Graphics in applet:
import java.applet.Applet;
import java.awt.*;
public class GraphicsDemo extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
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);
}
}
BY LECTURER SURAJ PANDEY CCT
COLLEGE
29. EventHandling in Applet
As we perform event handling in AWT or Swing, we can
perform it in applet also. Let's see the simple example of
event handling in applet that prints a message by click on
the button.
BY LECTURER SURAJ PANDEY CCT
COLLEGE