Java Applet: Client Side Java & Life Cycle

1. CLIENT SIDE JAVA

Applets are Java programs (usually small) that generally run within web browsers [Figure 16.1:]. They are usually incorporated in an HTML page. A web browser downloads the class file of the applet together with the other parts of the web page from the web server. If the browser has a suitable Java Virtual Machine (JVM), it executes the applet in the client computer. Since, applets run within web browsers in the client computer, they are said to be client-side Java technology.

The power of Java is not limited to Java applications only. Applets are also written using Java. Since, these applets can be embedded within web pages, they bring all facilities of Java in the html documents with little restrictions. Accordingly, the importance of Java as a client-side technology in the web is unquestionable.

Usually, applets are downloaded from a remote web server [Figure 16.1:]. Consequently, they should be smaller in size. Larger applets have longer download time giving an unpleasant experience to the visitors.

Note that applets work for the local files also. So, we need not always deploy html files containing applets in a web server. The html documents containing applets may be opened directly in a Java-enabled browser. They work in the same way as they do upon downloading from a remote server. Java also provides a utility appietviewer that may also be used to test applet’s functionality as well to run standalone applets.

In this chapter, we shall discuss the structure of applets, their execution philosophy, and other applet-specific issues.

2. LIFE CYCLE

Though applets are Java programs, they have certain differences from Java application programs. Applets, unlike Java application programs, do not have any main() method. Instead, they have an init() method that can be used for a similar purpose. Applets do not have any constructor. The way instances of applets are created and run is completely different from the way objects are created in Java application programs. So, let us understand the philosophy behind an applet.

Development of an applet consists of two basic steps: writing the Java class and incorporating it in an html document. We first concentrate on writing the applet class.

An applet (a Java class) must be created by extending the java.appiet.Appiet class [Figure 16.2:], which provides interfaces between the web browser and the applet. If you want to use swing components, you should create your applets by extending this j avax. swing. JAppiet class instead of Applet class. Note that the class, javax.swing.JAppiet is a subclass of the Applet class [Figure 16.2:]. java.lang.Object

An applet, from its birth to death, goes through a number of states [Figure 16.3:].

  • Instantiated or born
  • Running
  • Idle or Stopped
  • Dead or destroyed

The duration when an applet remains alive is called life cycle of the applet. The class Appiet provides a framework where your applets can run. It has a set of methods to control and supervise the smooth execution of applets [Figure 16.3:]. These methods (also called life cycle methods) are called in a specific order during an applet’s entire life cycle. An applet usually overrides these methods to do a designated task.

Even though, life cycle methods are called automatically by the browser, we should have sound knowledge about when they are called and what we can do with these methods. The following is a brief description of these life cycle methods.

2.1. init()

This is the first method that gets called only once [Figure 16.4:], when an instance of the applet is created and loaded by a web browser. Applets do not have any constructor. Since this method is called before all other methods, it can be used as a constructor. Usually, the piece of code that we write in the constructor goes here. For example, instantiating other objects, initializing variables, setting background and foreground color may be done here. A pictorial description of this method’s functionality is shown in Figure 16.4:

Do not make this method large, resource-consuming, and computationally intensive. Otherwise, your applet will take a long time to load resulting in a bitter experience to the viewers.

2.2. start()

This is the second method that gets called in the sequence [Figure 16.5:]. Note that init() method merely creates an instance of applet which is not yet active. The start() method, as the name implies, starts the execution of an applet. It is also called every time an applet is restarted (revisited). Restarting an applet means the applet was running, but stopped for some reason using stop() method earlier and needs to be started again. If you want to perform a task after initialization, override this method. It is also overridden if a task has to be performed every time the web page containing the applet is revisited. A pictorial description of this method’s functionality is shown in Figure 16.5:

It is also recommended to return from this method quickly. If it is really necessary to do any computationally intensive task in the start() method, it is better to create a new thread which will perform the task while start() method returns quickly.

2.3. paint()

This method is inherited from the java.awt.Container class. It is called immediately when the applet starts its execution [Figure 16.6:]. This method is also called when the output of an applet is redrawn. There are several situations when an applet is redrawn.

  • When the window containing the applet is minimized and restored
  • When the applet’s container window is overwritten by another window and is uncovered at some later point of time
  • When the window containing this applet is moved/dragged to another location

A pictorial description of this method’s functionality is shown in Figure 16.6:

This method takes a single argument of the type Graphics, which encapsulates the graphic window the applet is contained in. It has numerous useful methods that are used to display AWT components such as graphic elements (e.g. lines, ovals, rectangles, etc.), images, strings, etc. on the window. This is the method where we can write our code of what we expect from the applet such as animation etc.

2.4. stop()

The method stop() makes the applet inactive temporarily. For example, when a visitor switches to another window, leaving the current window containing the applet [Figure 16.7:], this method is called. However, the applet may come back to the running state again using a call of start() method again. This way, an applet can switch between these two states (stopped and running) any number of times in its life cycle.

A pictorial description of this method’s functionality is shown in Figure 16.7:

It is the right place to write code to cleanup resources that are not necessary during the inactive period. For example, we should suspend all threads that were started in the start() method so that they do not consume system resources. Consequently, if an applet shows some animation, we should suspend it when visitors are not viewing it.

2.5. destroy()

This method is called [Figure 16.8:] only once when the browser window containing the applet is closed (exited). This method makes the applet dead (i.e., the applet is not available any more). Typically, you need not override this method. If a browser window containing a running applet is closed, the stop() method is called first, which calls destroy() method after performing all the necessary tasks to shut down the applet. However, the method is still available to release other resources.

A pictorial description of this method’s functionality is shown in Figure 16.8:

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *