Applet
Applet
Applet
1. Applets do not use main() method but applications have main() method.
3. Applets cannot read from or write to the files in the local computer.
6.Applets are restricted from using libraries from other languages such as C or
C++.(Java language supports this feature through native methods)
When to use applets?
• When we need something dynamic to be
included in the display of a Web page.
• When we require some “flash” outputs. For
eg:, applets that produce sounds, animations
or some special effects would be useful when
displaying certain pages.
• When we want to create a program and make
it available on the Internet .
Steps involved
• Before we try to write applets, we must make sure that
Java is installed properly and also ensure that either Java
appletviewer or a Java-enabled browser is available.
Steps
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
Building applet code
• Applet code uses the services of two classes – Applet and Graphics from the Java
class library.
• The Applet class which is contained in the java.applet package provides life and
behavior to the applet through its method such as init(), start(), and paint().
• Unlike the applications, where Java calls the main() method directly to initiate the
execution of the program, when an applet is loaded, Java automatically calls a
series of Applet class methods for starting, running, and stopping the applet code.
• The Applet class therefore maintains the life cycle of an applet.
• The paint() method of Applet class, when it is called, actually displays the result of
the applet code on the screen.
• The output may be text, graphics, or sound.
• The paint() method, which requires a Graphics object as an argument, is defined
as follows:
public void paint( Graphics g)
• This requires that the applet code imports the java.awt package that contains the
Graphics class.
• All output operations of an applet are performed using the methods defined in
the Graphics class.
General form:
import java.awt.*;
import java.applet.*;
---------------------
---------------------
public class appletclassname extends Applet
{
-----------------
-----------------
public void paint (Graphics g)
{
--------------
-------------- //Applet operations code
}
------------------
------------------
}
• The appletclassname is the main class for the applet.
• When an applet is loaded, Java creates an instance of this class, and then a
series of Applet class methods are called on that instance to execute the
code.
The HelloJava applet
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString("HelloJava", 10, 100);
}
}
• When executed , draws the string “Hello Java” at the position 10, 100 (pixels)
• Java requires that the main applet class be declared public.
Applet Life Cycle
• When an applet is loaded, it undergoes a
series of changes in its state.
• The applet states include:
1. Born or initialization state
2. Running state
3. Idle state
4. Dead or destroyed state
Applet Life Cycle
Begin
Load Applet
Destroyed
left
right
top Specifies the alignment of an applet according to
align
bottom surrounding elements
middle
baseline