Ch5 Applet, AWT, Event and Swing Programming

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

C

Ch5 :- Applet, AWT , Event and Swing Programming


• Applet:-
- An applet is a Java Program that runs in a web browser.
- An Applet is a java class that extends the java.applet.Applet class.
- In general, execution of an applet does not begin at main() method.
- Applets are designed to be embedded within an HTML page.
- 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.
- Output of an applet window is not performed by System.out.println().Rather ,it is
handled with various AWT methods, such as drawstring().
- Hierarchy of Applet :-
- Object -> Component -> Container -> Panel -> Applet -> JApplet
- The java applet class which is a class of applet package extends the Panel class of awt
package.
- The panel class is a subclass of the Container class of the same package.
- The container class is an extension of Component class belonging to the same package.
- The component class is an abstract class and derives several useful classes for the
components such as Checkbox,List,buttons, etc.
- Heading file of applet is java.applet.Applet class.
- Advantages of Applet
i. Applets provide dynamic nature for a webpage.
ii. Applets are used in developing games and animations.
iii. It provides a secure two-way interaction between web pages.
iv. It takes less time to responds because it works on the client-side.

-Disadvantages:-
v. It cannot read certain system properties.
vi. It cannot read and write ordinary files on the execution host.
- Applet Life Cycle:-
- Applet life cycle has 5 methods init(),start(),stop(), paint() and destroy().
1) init() :- it is the first method.
-this method is intended for whatever initialization is needed for your applet.
-it is automatically invoked by the system when java first launches the applet.
- It typically performs the task such as read parameters, load images etc.
- It is invoked only once.
- public void init()
{
------------
-------------------
Action
}
2) Start() : Applet enters the running state when the system calls the start() method of Applet class.
- This occurs automatically after the applet is initialized .
- It is also called whenever the user returns to the page containing the applet after other pages.
- public void start()
{
-----------
---------
Action
}
3) Stop() : the stop() method stops the execution of the applet .An applet turns into idle state, when it is
stopped from running .This method is automatically called when the user moves off the page on which the
applet sits or when moving from one tab to another in the browser.
- - public void stop()
{
-----------
---------
Action
}
- 4) destroy() :- An applet is said to be dead when it is removed from memory.
- This occurs automatically by invoking the destroy() method when we want to quit the
browser.
- An applet is said to be dead when it is removed from memory .
- The destroy() method contains code release the resources such as thread, graphics
objects.
- public void destroy()
{
-----------
---------
Action
}
- 5) paint() :- the paint() method is called each time an AWT- based applets output must
be redrawn.
- Paint() is also called when the applet begins execution.
- The paint() method has one parameter of type graphics .
- This parameter will contain the graphics context ,which describe the graphics
environment in which the applet is running.
- public void paint(Graphics g)
{
-----------
---------
}

You might also like