An applet is a program written in Java that can display the results between well-defined boundaries in a web page. Thus, an applet runs in a rectangle of the size specified in the web page in which it is embedded. Java has been designed to provide a high degree of security. The ability to execute code taken from the Internet on the client’s computer can open Pandora’s box for it.
Therefore, the applets were limited their capabilities by prohibiting actions such as writing and/or reading from the client’s hard disk, reading/writing system variables, restrict Internet communication to the computer on which the applet was taken, warnings from browsers that its applet has opened a window.
If these capabilities had been completely eliminated, language would have been seriously limited in its actions. Fortunately, the Sun team offers us a solution: two types of Java programs.
One of the types, the one that will be executed through the Internet will be deprived of the above possibilities, and the other will be able to be executed only on the computer as a regular program, will be without the limitations of the applets but will not be distributed through the Internet.
The first is called an applet and the second is called an application. Limiting applications through the impossibility of running them on the Internet results from their capabilities: they can write to the hard disk, which could cause irreparable damage. Another limitation of the applications is that they do not benefit from the integrated graphics support of the browsers, leaving the programmer to solve this problem.
In a java applet, there is no main function (as in C ++ or Java applications), which determines the call of the other functions.
Then what is the order in which the functions will be executed?
The execution of an applet is marked by several important events generated by the browser. When meeting the <applet> tag, the browser loads the file needed to run the applet (.class files). The applet cannot run until the code has reached the client’s computer. After loading the code, the applet is automatically called for initialization by the init () function. It is the moment when the appliance prepares its parameters and obtains from the system the necessary resources for running.
After the initialization is complete, the browser sends the applet a start command (start function). An applet runs as long as the browser is active. When changing the current page, the applets in the old page receive a temporary stop command (stop function ()). When the browser is closed, the destroy () function is launched, which destroys all the allocated variables, freeing the memory.
All these functions are defined in the Applet class and are called automatically by the system. If you are careful, when Internet Explorer loads a java applet, write on the bottom bar first ‘Applet initialized’, then ‘Applet started,’ which will be written to you as long as the applet is active. If you press the ‘Refresh’ button, it will write ‘Applet stopped’, and then it will go back to initialization (with init ()) and the applet will start again (with a start ()).
Therefore, every Java applet represents a new type of object, derived from the standard Applet object.
Because Java applets need to be portable relative to the operating system, any Java development environment provides classes for creating the basic elements needed for a graphical interface. These items include: labels, text fields, buttons, radio buttons, radio boxes, check boxes, choice menus, etc. All these elements are included in the java.awt package.
A label is created by calling the Label class constructor, as follows:
Label = new Label (str, align); where str is a String variable that defines the string that will be written on this tag, and align is one of the values Label.LEFT, Label.CENTER or Label.RIGHT, representing the alignment of the label to the left, center, or right.
After creating the tag, it must be added to the applet by writing the following line in the init () function: add (tag).
As you can see, the label created above will be displayed on the surface of the applet to the left, center or right. This will not give you much freedom. For example, how can you display a label at certain coordinates and how can you set the width and height of the label according to certain values? When you create an applet, Java automatically creates and assigns a default layout. You can set the layout as having different values, including FlowLayout, GridLayout, BorderLayout, CardLayout, GridBagLayout. These are classes that are part of the java.awt package.