Вы находитесь на странице: 1из 36

Applet Programming

What is Applet
An applet is a special kind of Java program which is used for internet programming. The applets reside in a server and they can be downloaded from the server to the client machine and executed on client machine provided that the browser should be enabled with Java technology.

Types of Applet
Applets are basically of two types. 1. Local Applet These are applets which are created for standalone PC's where the same PC is distinguished as server and client. Hence, to execute a local applet there is no need of internet connection. 2. Remote Applet The applets which are developed for an environment where the server and the client machine are physically apart from each other are called remote applets

Needs of Applet
1. Applet is a window based application. 2. Applets are basically used to create dynamic pages. 3. The difference of an applet program from a normal program is that it does not contain the main function. So it has no entry point. 4. The program starts executing from some functions present in Applet class.

Need for applet


5. an applet program cannot be executed independently as done in case of normal application programs. 6. They must be executed with the help of either applet viewer or with the help of any web browser.

Creating an applet program


We need to import two packages for writing an applet program Import java.lang.awt.*; Import java.lang.applet.*; The java.lang.awt.* package provides a set of classes and using those classes we can create a window component and it also contains a graphic class; the java.lang.applet.* package is used as we are using an applet class which is present in applet package.

7 of 30

Applet Class
An applet class is a member of Java Application Programming Interface(API) Package. An applet class is extended from the Panel Class, which is further extended from the classes Container, Component and Object. Object class is the member class of the java.lang package and is called in the program automatically.

8 of 30

Applet Class
The component, container and Panel classes are the members of java.awt package that provide visual components such as label,button and text fields. An applet is the only member of the java.applet package.

9 of 30

Java Class Hierarchies

10 of 30

Abstract windows Toolkit(AWT)


The awt package is a collection of classes and methods that allow an end user to design and manage graphical user interface(GUI) based application. AWT is used to support applet windows and it also helps in creating independent windows such as frame window or panel window that run in GUI environment.

11 of 30

Abstract windows Toolkit(AWT)


AWT is javas Largest Package that can be included in java program by giving the java.awt.* statement. It is a part of java foundation classes(JFC). AWT has 63 classes and 14 interfaces used for creating user interfaces.

12 of 30

Features of the AWT package


It provides graphics and imaging tools that enable you to develop GUI. It provides layout manager that enables an end user to develop window layouts. It supports event handling. for example, an error occurred in between execution of a program.

13 of 30

Hierarchy of java.awt package

14 of 30

Component Class
Component class is at the top of the AWT inheritance class hierarchy. It is the super class and all others in the hierarchy inherit its characteristics directly or indirectly. Component class is the abstract class that encapsulated all the properties of visual components or object.

15 of 30

Methods of the component class

void setSize(int width, int height): defines the size of a component in terms of wisth and size. void setLocation(int x,int y): specifies the location of a components, in the window, at the position specified by the x and y coordinates. void setBounds(int x,int y,int width ,int heigth): sets the bounds of a component in terms of position x and y and size in terms of width and height.

16 of 30

Methods of the component class


Void setForeground(Color b): sets the foreground color of the component. Void setBackground(Color b): sets the background color of the component.

17 of 30

Container class

Container class is the subclass of component class. Methods that are used to manipulate components on container objects are: void add(object o): Nest a Component or an object inside a container. void remove(object o): removes a component or an object from a container. void removeALL(): removes all the components or objects that are placed inside a container.

When we want to execute an applet program with the help of a web browser, we need to write an HTML file. <html> <head> <title>Example of Applet</title></h ead> <body> <applet code ="filename.class" width =100px height =100px> </applet> </body> </html>

Applet Programming
import java.lang.awt.*; import java.lang.applet.*; public class message extends Applet { String msg; public void init() { msg = Welcome to applet programming; } public void paint(Graphics g) { g.drawString(msg,100,400); } }

Running the applet in a Web browser

23 of 30

APPLET LIFE CYCLE

Applet Life Cycle

Applet
Public keyword is used because we run an applet program in a media different from that in which we write it. Paint() method used in the above example calls drawString() function which is a function of Graphics class. Applet class contains certain functions like init(), start(), stop(), paint(), destroy(), etc. which are usually used in programs.

Method init()
init() method Whenever an applet is loaded into a web page this method is automatically called. This method provides an appropriate place for initialization of the variables required and other set up operations such as setting of background color, defining the applets layout, parsing parameters, etc.

Method start()
start() method After the initialization is completed, start() method is called. The difference between this method and the init() method is that init() method is called only when the page is loaded, whereas start() method is called every time the page is loaded and every time the page is restarted. This method is mainly used while implementing threads in Java.

Method stop()
stop() method This method is called whenever the user navigates from one page to another page. It is always advisable to use stop() method if any type of animation or memory consuming activities is being performed in order to avoid wasting of system resources.

Method destroy()
destroy() method This method is called immediately before the applet exits. The exiting operation is caused by either the applet operation requesting for the exit or may be the web browser is shut down. When it is called it directs the applet to free up all the system resources being used by it.

Methods in applet
/*<applet code="balli1.class" height=400 width=400> </applet>*/ import java.awt.*; import java.applet.*; public class balli1 extends Applet { String str="Hello ";

31 of 30

Methods in applet
public void init() { str=str+"<init method>"; } public void start() { str=str+"<start method> "; }

32 of 30

Methods in applet
public void stop() { str=str+"<stop method>"; } public void destroy() { str=str+"<destroy method>"; }

33 of 30

Methods in applet
public void paint(Graphics g) { str=str+"<paint method>"; g.drawString(str,50,50); } }

34 of 30

Out Put of the Program

1. http://aspalliance.com/1309_Introducing_Java_Applets.1 2. The Complete Reference by Patrick Naughton and Herbert Schildt

36 of 30

The End
Thanks

Вам также может понравиться