Академический Документы
Профессиональный Документы
Культура Документы
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.
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
10 of 30
11 of 30
12 of 30
13 of 30
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
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
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); } }
23 of 30
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
36 of 30
The End
Thanks