A Java Applet that is also a Java Application

Introduction:

Java is a very interesting language. It allows us to write a program in two different ways, Applet and Stand-alone application. While an applet can be embedded into a web page, enabling it to be executed through a java-enabled web browser, a stand-alone application is very similar to conventional applications. 

We use different structures to write these applications. For the same functionality, we write drastically different codes to make it work as an applet or as a stand-alone application, depending upon the execution context. 

Is it possible to write a single code that can work as an applet or as a stand-alone application, depending upon its execution context? Can we write a java code that, if executed through the java interpreter will work as a stand-alone application and when executed through a web page, using a web browser, will work as an applet?

We shall analyse an elegant solution to this interesting question.

Program functionality:

Let’s write a simple application/applet in Java to display a button on the screen with the caption ‘Click Me!’. Whenever a user clicks on the button, a Frame window is generated with the title ‘Hello World!’. For the sake of brevity, we will try to make the code as small as possible, so that we will be in a position to concentrate more on the topic of discussion rather than the Java code itself. Here, our objective is to understand the code for an applet and that of an application, and then try to combine them into one. 

Structuring the program:

Instead of writing an ad-hoc Java program, we will first try to structure the program in a proper way (refer figure 1):

· Every GUI application contains two distinct components, user interface component and logic component. While designing the program we will try to group them properly in order to modularize our design; and
· Every GUI application requires an environment in which it exists. In case of an applet a web browser provides this environment and in case of a stand-alone application a Frame window provides this environment.



Create a Java Applet having the required functionality:

Step 1: Write Java code in a *.java file (See code 1)

// HelloApplet.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class HelloApplet extends Applet
{
static HelloPanel panel;
public void init()
{
panel = new HelloPanel();
add(panel);
}
}

class HelloPanel extends Panel implements ActionListener
{
Button btnHello;

public HelloPanel()
{
btnHello = new Button("Click Me!");
btnHello.addActionListener(this);
add(btnHello);
}

public void actionPerformed(ActionEvent e)
{
Frame f = new Frame("Hello World!");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
}); 
f.setSize(200, 200);
f.show();
}
}

Step 2: Compile the *.java file into java byte-code using javac application to get the java class files.


Step 3: Write an HTML wrapper code in *.html file. (See code 2)

<!- - HelloApplet.html - - >
<html>
<head>
<title>'Hello World!' Java Applet</title>

</head>
<body>
<applet code="HelloApplet.class" width=200 height=200>
</applet>

</body>
</html>

Step 4: Execute the applet through the web page by opening *.html file in a web browser or AppletViewer application.

AppletViewer HelloApplet.html 

Understanding the structure of Applet code:

The code contains two classes, HelloApplet and HelloPanel. The HelloPanel class is where the actual task is carried out. The HelloApplet class just acts as a wrapper for HelloPanel class.

Through HelloApplet class, which is derived from Applet class, the web browser provides the required environment for HelloPanel class, which itself is derived from Panel class.

Create a Java Application having the required functionality:

Step 1: Write Java code in a *.java file (code 3)

import java.awt.*;
import java.awt.event.*;

public class HelloApplication extends Frame
{
static HelloPanel panel;
public static void main(String args[])
{
panel = new HelloPanel();
Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
f.add("Center", panel);
f.setSize(200, 200);
f.show(); 
}
}

class HelloPanel extends Panel implements ActionListener
{
Button btnHello;

public HelloPanel()
{
btnHello = new Button("Click Me!");
btnHello.addActionListener(this);
add(btnHello);
}

public void actionPerformed(ActionEvent e)
{
Frame f = new Frame("Hello World!");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
}); 
f.setSize(200, 200);
f.show();
}
}

Step 2: Compile the *.java file into java byte-code using javac application, to get the java class files.

Step 3: Execute the application using java interpreter.

java HelloApplication 

Understanding the structure of Application code:

The code contains two classes, HelloApplication and HelloPanel. The HelloPanel class is where the real work happens. The HelloApplication class just acts as a wrapper for HelloPanel class. 

Through the HelloApplication class, which is derived from Frame class, a Frame window provides the required environment for the HelloPanel class, which itself is derived from Panel class.


Combining Applet and Stand-alone Application structures into one:

We can see that in both he situations, applet and application, the objective is accomplished by the HelloPanel class. In the case of applet, a class derived from Applet provides a wrapper for it while for the application a class derived from Frame provides the wrapper. This implies that we have to provide a context sensitive wrapper that will create respective wrappers depending on the execution context. That means it will provide a Frame window-based wrapper when we execute it as a stand-alone application and a web browser-based environment will be provided if we are executing it as an applet.

Such a wrapper has to incorporate the characteristics of both the applet as well as application. Instead of going into the details of life cycles of an applet and that of a stand-alone application, we will concentrate upon their respective entry points. This will make our job a lot easier because once we have access to the respective entry points then onwards the program can control its own life cycle. 

Entry point for an Applet: init() method
Entry point for a stand-alone application: main() method

Java provides a sub-set of functionalities for both applet and application. So we will adopt a strategy where the code will work as an applet by default. That means this code will have init() as its natural entry point. Then we will add a main() to this code that will create a stand-alone environment whenever required.

We will elaborate this strategy through the following code snippet. 

Java code that will work as an Applet as well as an Application:

Step 1: Write Java code in a *.java file (code 4)

// Hello.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Hello extends Applet
{
        static HelloPanel panel;

        public void init()
        {
                panel = new HelloPanel();
                add(panel);
        }

        public static void main(String args[])
        {
                Frame f = new Frame("'Hello World!' Application");
          f.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e)
 {System.exit(0);}
              });       
                f.setSize(200, 200);
                Hello hello = new Hello();
                f.add("Center", hello);
                hello.init();
                f.show();
        }
}

class HelloPanel extends Panel implements ActionListener
{
        Button btnHello;
        public HelloPanel()
        {
                btnHello = new Button("Click Me!");
                btnHello.addActionListener(this);
                add(btnHello);
        }

       
public void actionPerformed(ActionEvent e)
        {
                Frame f = new Frame("Hello World!");
          f.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e)
 {System.exit(0);}
              });       
                f.setSize(200, 200);
                f.show();
        }
}

Step 2: Compile the *.java file into java byte-code using javac application, to get the java class files.

Step 3: Now execute this same java program in Applet or Application mode.

(a) Application mode:
Execute the application using java interpreter.

java Hello 

(b) Applet mode:
(i) Write an HTML wrapper code in *.html file (code 5).

<!- - Hello.html - - >
<html>
<head>
<title>'Hello World!' Java Applet</title>
        </head>
        <body>
<applet code="Hello.class" width=200 height=200>
                </applet>
        </body>
</html>

(ii) Execute the applet through the web page by opening *.html file in a web browser or AppletViewer application.

AppletViewer Hello.html 


Conclusion:

This brings us to the end of our discussion. We examined an elegant solution for a very interesting problem. We have seen that with proper structuring of the Java code it is possible to write a program that will behave as an applet or a stand-alone application, depending upon the execution context.

The strategy we discussed here enables us to convert any applet (with proper modularization of the code) into an application. Someday we will discuss a more generalized approach for the same, as well as whether this generalization is possible in all situations or is restricted to some scenarios. Till then happy experimenting!!!

The author, a lecturer based in Pune, is currently pursuing his Doctorate course. He can be reached at: sachin_a_kadam@rediffmail.com.

Language: Java
Platform: Java




Added on July 31, 2007 Comment

Comments

Post a comment

Your name:

Comment: