In the era of the internet, web pages have become a common thing in the world. These are worldwide viewed through different web browsers and contain information like text, or sometimes it consists of hyperlinks or links that connect to another webpage. The viewers are always provided with information whenever they use a webpage. The Java Applet is a unique program within the web pages that helps generate dynamic content. These Applets run within the browser, and the working of the Applet is on the Client side. The use of the Java Applet provides several advantages. A few of the benefits of the Java Applet are:
- The response time of the Java Applet is lesser as the working of the Applet is at the client-side.
- The use of Java Applets is secure.
- Web browsers that run over different platforms such as Windows, Mac OS, Linux, etc., can execute these Applets.
Although several advantages lie with the use of Java Applets, there is still a drawback of the Applet. One of the drawbacks of the Applet is that for the execution of the Applet at the client-side, a plugin is required.
Table of Contents
The Applet may be defined as a program in Java that is embedded in a webpage. It is embedded using the tag of OBJECT or APPLET and then hosted on the webserver. These are mainly used for making the web pages dynamic and also entertaining.
A few important points of the Java Applets are:
- All the Java Applets are sub-classes of the class java. Applet.Applet whether indirectly or directly.
- The Applets cannot be considered to be standalone programs. The execution of the Applets occurs within the web browser or sometimes within the Applet viewer. The JDK provides the “Applet viewer,” which is a standard tool for Applet viewers.
- The primary() function of the program does not start the execution of the Applet.
- The function system.out.println() does not perform the output of the Applet window. Instead, many AWT methods like drawString() are used to handle the production of the Applet window.
The understanding of the Java Applet will begin when the life cycle of the Applet is understood. There are a few steps in the life cycle of a Java Applet, such as; the initialization of the Applet, the starting of the Applet; Applet gets painted, the stopping of the Applet, and the destruction of the Applet. For carrying out these different steps, several functions are used by Java.
- The beginning of an Applet requires a few methods, such as the init(), start(), and the method paint().
- The termination of the Applet uses the methods stop(), and the method destroy().
init(): The first method called at the beginning of the Applet is init(). During this method, the variables should be initialized. During the Applet run time, the method is called for only once.
start(): Just after the init() method, the method start() is called. The main function of the method is to restart the Applet that has been stopped as it can be observed that the method init(0 is called for only once, and that is when the Applet is loaded, while the method start() is called every time the display of the HTML document of the Applet is carried out on the screen. If a user returns after leaving a webpage, the execution will be resumed at the method start(0 by the Applet.
paint(): Whenever the output of an AWT-based Applet is to be redrawn, the method of paint() is called each time. Several reasons might exist for this calling: the window over which the Applet was running got overwritten by a different window and then uncovered. Or sometimes, when an Applet window may get minimized and then it might get restored. The method of paint() is also called when the execution of the Applet begins. The cause may be different, but whenever an Applet is redrawn, the method paint() is to be called.
One parameter is associated with the method paint(), which is of the type graphics. The parameter graphics contain the context of graphics that describes the environment of graphics whenever the Applet is running. This context is used whenever the Applet output is required.
Also, from all the methods mentioned above used in the Java Applet, only this method is parameterized.
The syntax of the method is
public void paint(Graphics g)
Where the object reference for the class Graphic is g.
stop(): Whenever the web browser has to leave the HTML document containing the Applet, like when it is moving to another page, the method stop() is called. The Applet keeps on running when the method stop() is called. The method is called so that the threads are suspended as they are not required to be executed when the Java Applet is invisible. While returning to the page, it can be restarted through the method start().
destroy( ): The method of destroy() is called when it is determined that the Applet will be removed from the memory. For freeing up the memory, every resource should be freed up that is being used by the Applet. The method stop() is always called before the calling of the method destroy().
An Applet Program in Java is shown below-
The explanation of the program is as follows:
- There are two import statements through which the Java program for the Applet begins. The class Applet is imported through the import statement at first. This Applet class is imported from the Applet package. The creation of every Applet, which is AWT-based, should be a subclass of the class Applet. The following import statement imports the class Graphics from the package of AWT.
- In the next line of the code, the HelloWorld class is declared. This class is declared as public as the code from outside the Java program will access the class. The declaration of the function paint() is made inside the class HelloWorld. The AWT defines the function, and the Applet should override the method.
- The call for the function drawString( ), is within the method paint() a Graphics class member. A string results from this method.
The general form of the call is
The string is output at the location x,y. The location of 0,0 is at the upper-left corner of the Java window. When there is a call to the function drawString( ) in the Java Applet, “Hello World” is displayed.
It can be noticed that there is no method of main() in the Java program. So, like the other programs in Java, the Applet execution does not begin at the main(). Also, most of the Applets don’t have the function main(). However, the execution of the Applet begins when the class names are passed to the viewer of the Applet or on to a browser of the network.
The above Applet is run after the source code “HelloWorld.java” is entered. The compilation of the Applet is in a way similar to the compilation of other Java programs. But running the source code through the command of Java will result in an error as it isn’t an application.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Two ways are present for running an Applet:
- The Applet can be executed within a web browser that is Java-compatible.
- The Applet can be executed through an applet viewer, such as Applet-viewer. The Applet is executed in a window in the Applet viewer. It is the fastest and also the most straightforward way generally for testing the Applet.
How can an object reference be created without the creation of an object?
Whenever object references are passed as arguments, the caller will provide the object itself.
What are ways for running an Applet?
Two ways are there for running an applet. 1. Through an HTML file, 2. Through the tool of AppletViewer.
Which class should be inherited for creating the Java Applet?
The class java.applet. Applets should be inherited for creating any applet. The class provides four methods of life-cycle for the applet such as init(), start(), stop(), and destroy(). Understanding the language of Java is quite crucial for its application in several fields, such as software development or web development. Java is the main building block of the entire mobile application sector too. The article discussed all the concepts of the Java applet. Java Applet is once such a program of Java that is used in web pages. For understanding the underlying concepts of development, becoming a master of Java is the most needed. If you want to develop your Java skills in the area of software and web development, then you can pursue the online program of “Executive PG Programme in Software Development - Specialisation in Full Stack Development” offered by upGrad. The course is designed for all working professionals and will train you through several industry-oriented projects and give you placement assistance in top industries. Any queries related to the course are welcomed. Our team will assist you every step of the way.