Q.How does a typical Java applet run? Explain with suitable illustrations making references to the running of a java program.
Ans:A java program goes through three phases – coding, compilation, and execution in order to run. At first, a program is created called “source code” with an extension .java. Then, the compiler creates the byte codes and stores in the same filename with '.class' extension. Next, the Class Loader and Byte code Verifier loads the file into memory and verifies the byte codes. In the final stage, the Java Virtual Machine (JVM) translates the program into machine language and executes the program.
Fig.1 : Steps to run a Java Program
An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. When you use a Java technology-enabled browser to view a page that contains an applet, the applet's code is transferred to your system and executed by the browser's Java Virtual Machine (JVM).
An applet must be a subclass of the java.applet.Applet class. The Applet class provides the standard interface between the applet and the browser environment.Swing provides a special subclass of the Applet class called javax.swing.JApplet. The JApplet class should be used for all applets that use Swing components to construct their graphical user interfaces (GUIs).
Applets are compiled Java programs designed to be small, fast, and easily transferrable over network resources. Applets are ideal for the Web because they leverage browser features which decrease file size, increase download speed, and simplify the programming. Applets, however, have limitations which full-blown Java applications do not have. The limitations of applets can be described as follows:
- Applets have limited file access
- Applets have limited network access
Every Java applet is a graphical user interface on which we can place GUI components using the different techniques. We begin with a simple applet that draws “Welcome to Java Programming” on the applet. This applet executing in two applet containers- the ‘applet viewer’ and the ‘Microsoft Internet Explorer web browser’
Example of applet that draws a string:
import java.awt.Graphics; //program uses class Graphics
import javax.swing.JApplet; //program uses class JApplet
public class WelcomeApplet extends JApplet
//draw text on applet’s background
Public void paint (Graphics g)
// call superclass version of method paint
//draw a String at x-coordinate 25 and y-coordinate 25
g.drawString (“Welcome to Java Programming!”, 25, 25);
} //end method paint
} //end class WelcomeApplet
Creating the Applet Class:-
Line 1 imports class Graphics to enable the applet to draw graphics, such as lines, rectangles, ovals and strings of characters. Class JApplet (imported at line 2) from package javax.swing is used to create applets. As with applications, every Java applet contains at least one public class declaration. An applet container can create only objects of classes that are public and extend JApplet (or the Applet class from early versions of Java). For this reason, class WelcomeApplet (line 3-14) extends JApplet.
An applet container expects every java applet to have methods named init, start, paint, stop and destroy, each of which is declared in class JApplet. Each new applet class we create inherits default implementations of these methods from class JApplet. These methods can be overridden (redefined) to perform tasks that are specific to our applet.
When an applet container loads class WelcomeApplet, the container creates an object of type WelcomeApplet, then calls three of the applet’s methods. In sequence, these three methods are init, start and paint. If we do not declare these methods in our applet, the applet container calls the inherited versions. The superclass methods init and start have empty bodies, so they do not perform any tasks. The superclass method paint does not draw anything on the applet.
We might wonder why it is necessary to inherit methods init, start and paint if their default implementations do not perform tasks. Some applets do not use all three of these methods. However, the applet container does not know that. Thus, it expects every applet to have these methods, so that it can provide a consistent start-up sequence for each applet. This is similar to applications always starting execution with main. Inheriting the ‘default’ versions of these methods guarantees that the applet container can execute each applet uniformly. Also, inheriting default implementations of these methods allows the programmer to concentrate on defining only the methods required for a particular applet.
Overriding Method paint for Drawing:-
To enable our applet to draw, class WelcomeApplet overrides method paint (lines 6-13) by placing statements in the body of paint that draw a message on the screen. Method paint receives a parameter of type Graphics (called g by convention), which is used to draw graphics on the applet. We do not call method paint explicitly in an applet. Rather, the applet container calls paint to tell the applet when to draw, and the applet container is responsible for passing a Graphics object as an argument.
Line 10 calls the superclass version of method paint that was inherited from JApplet. This statement should be the first statement in every applet’s paint method. Omitting it can cause subtle drawing errors in applets that combine drawing and GUI components.
Line 12 uses Graphics method drawString to draw Welcome to Java Programming! on the applet. The method receives as arguments the String to draw and the x-y coordinates at which the bottom-left corner of the String should appear in the drawing area. When line 12 executes, it draws the String on the applet at the coordinates 25 and 25.
Executing an Applet in the appletviewer:-
As with application classes, we must compile an applet class before it can execute. After creating class WelcomeApplet and saving it in the file WelcomeApplet.java open a command window, change to the directory in which we saved the applet class declaration and compile class WelcomeApplet.
Recalling that applets are embedded in web pages for execution in an applet container (appletviewer or a browser); before we can execute the applet, we must create an HTML document that specifies which applet to execute in the applet container. This show a simple HTML document- WelcomeApplet.html – that loads the applet defined as below into an applet container.
Lines 2 & 3 specify an applet element that tells the applet container to load a specific applet and defines the size of the applet’s display area (its width and height in pixels) in the applet container. Normally, the applet and its corresponding HTML document are stored in the same directory on disk. When an applet container encounters an HTML document that contains an applet, the applet container automatically loads the applet’s .class file (or files) from the same directory on the computer in which the HTML document resides.
The applet element has several attributes. The first attributes in line 2, code = ”WelcomeApplet.class”, indicates that the file WelcomeApplet.css contains the compiled applet class. The second and third attributes in line 2 indicate the width (300) and the height (45) of the applet in pixels. The tag (line 3) terminates the applet element that began at line 2.
Executing an Applet in Web Browser:-
This sample program execution demonstrate WelcomeApplet executing in the appletviewer and in Microsoft Internet Explorer web browser. To execute an applet in Internet Explorer, perform the following steps:
- Select Open…from the File menu.
- In the dialog box that appears, click on Browse…button.
- In the dialog box that appears, locate the directory containing the HTML document for the applet we wish to execute.
- Select the HTML document.
- Click the Open button.
- Click the OK button.
Applet Life-Cycle Methods:-
Now that we have created an applet, let’s consider the five applet methods that are called by the applet container from the time the applet is loaded into the browser to the time that it is terminated by the browser. These methods correspond to various aspects of an applet’s life cycle. These methods, which are inherited int our applet classes from class JApplet. Below specifies when each method gets called and explains its purpose. Other than method paint, these methods have empty bodies by default. If we would like to declare any of these methods in our applets and have the applet container call them, we must use the method headers. If we modify the method headers (e.g., by changing the method names or by providing additional parameters), the applet container will not call our methods. Instead, it will call the superclass methods inherited from JApplet.
public void init()
It is called once by the applet container when an applet is loaded for execution. This method initializes an applet. Typical actions performed here are initializing fields, creating GUI components, loading sounds to play, loading images to display and creating threads.
public void start()
It is called by the applet container after method init completes execution. In addition, if the user browsers to another website and later returns to the applet’s HTML page, method start is called again. The method performs any tasks that must be completed when the applet is loaded for the first time and that must be performed every time the applet’s HTML page is revisited. Actions performed here might include starting an animation or starting other threads of execution.
public void paint (Graphics g)
It is called by the applet container after methods init and start. Method paint is also called when the applet needs to be repainted. For example, if the user covers the applet with another open window on the screen and later uncovers the applet, the paint method is called. Typical actions performed here involve drawing with the Graphics object g that is passed to the paint method by the applet container.
public void stop()
This method is called by the applet container when the user leaves the applet’s web page by browsing to another web page. Since it is possible that the user might return to the web page containing the applet, method stop performs tasks that might be required to suspend the applet’s execution, so that the applet does not use computer processing time when it is not displayed on the screen. Typical actions performed here would stop the execution of animations and threads.
public void destroy ()
This method is called by the applet container when the applet is being removed from memory. This occurs when the user exits the browsing session by closing all the browser windows and may also occur at the browser’s discretion when the user has browsed to other web pages. The method performs any tasks that are required to clean up resources allocated to the applet.
- How to Learn Java Program, Deitel and Deitel