This presentation gives an introduction to Java applets. Simple applet program, how to compile and run applet program. It also discuss about the applet life cycle.
1. APPLETS
Prof. Yashoda M B
Assistant Professor in Computer Science (UG)
Kristu Jayanti College, Autonomous
(Reaccredited A++ Grade by NAAC with CGPA 3.78/4)
Bengaluru – 560077, India
2. INTRODUCTION
• Applet is a small program
• Can be placed on web page
• Will be executed by the web browser
• Give web page “dynamic content”
• Applet class is one of the AWT component
• Java applets are usually graphical
• Draw graphics in a defined screen area
Important points :
• All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.
• Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer
tool called applet viewer.
• In general, the execution of an applet does not begin at the main() method.
• Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString().
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
2
3. TYPES OF APPLETS
• Local Applet
• Remote Applet
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
3
6. HOW APPLETS DIFFER FROM APPLICATION
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
6
7. BUILDING APPLET CODE
• Uses the services of two classes, namely Applet and Graphics.
• Applet class in java.applet package
• Graphics class in java.awt package
• Java automatically calls a series of Applet class methods for starting, running,
and stopping the applet code.
• The Applet class therefore maintains the lifecycle of an applet.
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
7
8. CONTD…
• paint() method of the 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 requires a Graphics object as an argument, as follows:
public void paint(Graphics g)
• All output operations of an applet are performed using the methods defined in
the Graphics class.
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
8
9. GENERAL FORMAT OF APPLET CODE
import java.awt.*;
import java.applet.*;
public class AppletEx extends Applet
{
…………
………….
public void paint(Graphics g)
{
…………….. //applet operation code
…………………
}
…………………
………………....
}
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
9
10. EXAMPLE
java HelloWorld!
Error: Main method not found in class HelloWorld,
please define the main method as:
public static void main(String[] args)
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
10
12. CONTD…
• Born or initialization state
• Running state
• Idle state
• Dead or destroyed state
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
12
13. INITIALIZATION STATE
• Applet enters the initialization state when it is first loaded.
• Achieved using init() method
• User can perform the following
• Create objects needed by the applet
• Set up initial values
• Load images
• Setup colors
• Initialization occurs only once in the applet’s life cycle.
• To provide any behaviours mentioned above, we must override the init() method:
public void init()
{
…………
…………
}
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
13
14. RUNNING STATE
• Applet enters the running state when system calls start() method.
• This occurs automatically after the init() method
• Starting can also occur if the applet is there in the stopped state.
• start() method can be called more than once
• we may override the start() method:
public void start()
{
…………
…………
}
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
14
15. IDLE OR STOPPED STATE
• Applet enters the idle state when it stops running.
• When stop() method is called explicitly
• When we leave the page containing currently running
applet
• we may override the stop() method:
public void stop()
{
…………
…………
}
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
15
16. DEAD STATE
• Applet is said to be dead when it is removed from the 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.
• we may override the destroy() method:
public void destroy()
{
…………
…………
}
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
16
17. DISPLAY STATE
• Applet enters the display state whenever it has to perform
some output operation on the screen.
• Achieved using paint() method
• we must override the paint() method:
public void paint(Graphics g)
{
…………
…………
}
11/19/2023
Prof. Yashoda M B, Kristu Jayanti College, Bengaluru
17