Introduction to Applets in Java
The applet is a concept that can be beautifully used in Java for the amalgamation of web browsers and web pages to produce a specific output. Applets in Java can range from a simple applet to a very complex one. A simple hello World applet lets someone see how a basic Java applet works, and it can be represented on the screen. In this article, we will check the basic Java applets and another Java applet that can be used extensively in Java to solve several problems in real life. Java applets can be implemented in real life into several other programming languages such as Python, Dot Net, and other codes.
The Lifecycle of Applets in Java
In this phase, we are going to observe the lifecycle of the applet.
There are five basic phases for the lifecycle of an applet.
- Initialize
- Started
- Painted
- Stopped
- Destroyed
Let us see what these 5 phases do:
- The applet is initialized. With the help of init(), the applet is initialized with all the variables that are required for the applet to run.
- The applet is started. With the help of Start(), the applet is started, and there is the execution of the applet after the initialization phase.
- The applet is painted. With the paint(), the basic functionality of the applet is drawn and executed.
- The applet is stopped.
- The applet is destroyed. It means that all the contents are removed from the main memory, which is the function of the destroy().
Working of Applets in Java
Whenever an applet runs, there are several functions that are called sequentially to find the main objective of the applet running several outputs.
The functions that are sequentially called are:
- Init()= The init() is a function that is called at the beginning of the program to assign different variables to the program and implement them in real-time.
- Start()– The Start () is usually called after the init(), and it is called to see the HTML screen that is present along with the Java coding. Whenever a user leaves an open HTML Screen and comes back, he goes from the start(), skipping the Init().
- Paint()– The paint() is called when the applet begins the execution of the program. The paint() has a parameter known as Graphics, and Graphics g helps in the execution of the program in painting different parameters.
- Stop()– The Stop() is called when the user leaves the HTML document and moves to the applet. Start() is used to restart them whenever the Stop() terminates the program and then finally pushes it into an effect.
- Destroy()– The Destroy() is called when the applet has to be removed completely from memory.
Examples of Applets in Java
Below are mentioned some of the examples on Applets in Java:
Example #1
In the coding example, we are going to see a basic version of the Hello World applet. In the figure given below, we see how a basic Applet Hello World is represented in the Bluej Java programming platform.
Whenever we try to run the applet, the applet provides the screen below, which shows us many options to select from within a range of options. In the screen below, we can generate a web page, or run an applet in the Applet Viewer or run the applet in a Web browser. These are some of the options which have given by us in running an applet in the Bluej Programming platform. The applet’s height and width can also be provided in the box, and depending on the height and width provided, the Applet runs, making a distinct applet serving solutions for various problems.
Now we see the example of the sample coding and import packages to present the Hello World Sample Code. In this program, we implement packages such as Applet and Graphics. Graphics g is used for painting and providing output in a web browser or in the desired medium as wanted.
Code:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet
{
// Overriding paint() method
@Override
public void paint(Graphics g)
{
g.drawString("Hello [email protected] Great to be here!!!", 50, 50);
}
}
Output:
The Sample output is also given in the example, and in the Java applet, we can have several other comments being installed to produce several other outputs.
Output:
Now, we can do many functions on the applet such as Restart, Reload, Stop, Save Start and Clone. These are functionalities that the applet provides with various stages.
Example #2
In the next coding example, we are going to see some more aspects of Java applets in which we see the basic functionality of Java applets. In the next Java applet, we change the height and width of the Java applet and we some basic changes in the functionality. The writing comes more in the middle. An example of the coding sample is given below, which is executed to get the print of the “Hello World” statement.
Code:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet
{
// Overriding paint() method
@Override
public void paint(Graphics g)
{
g.drawString("Hello World!!! Great to be here!!!", 150,150);
}
}
Output:
Advantages and Disadvantages of Applets in Java
There are several advantages and disadvantages of running a Java applet.
Advantages
Some of the advantages listed down are as follows:
- The applet works at the client-side hence there is a very low response time.
- The applet is very secure and robust.
- It can be executed under many operating systems such as Unix, Linux, and macOS operating system.
- The applet is combined with HTML, CSS, and Javascript to provide more functionality to the web page.
- The applet is very easy to build and contains very little redundancy.
Disadvantages
Some of the disadvantages of Applets can also be shown in the document illustrating the support of non- use of these applets. The disadvantages are:
- The plugin is required at the client side to execute the applet.
- Without the proper plugin, there is no execution of the applet at the client side.
- Making complex applets is quite difficult and hence is a disadvantage.
Conclusion
In this article, we see the basic functions of an applet, the lifecycle of an applet, as well as some basic programs as to how an applet runs in the Java programming language. There can be very complex applets that can be made and implemented using BlueJ and other software for HTML and CSS. We stress more on the theory part of applets rather than the programming concept for applets.
Recommended Articles
This is a guide to Applets in Java. Here we discuss the introduction, Working, the Lifecycle of Applets in Java, and the Advantages and Disadvantages. You can also go through our other suggested articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses