Thursday, February 4, 2016

Topic 36: Applets in Java

  • Applets are small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as part of a web document. After an applet arrives on the client, it has limited access to resources so that it can produce a graphical user interface and run complex computations without introducing the risk of viruses or breaching data integrity. 

    Let’s begin with the simple applet shown here:

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

public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet", 20, 20);
}

}

This applet begins with two import statements.


  • The first imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user (either directly or indirectly) through the AWT, not through the console-based I/O classes. The AWT contains support for a window-based, graphical user interface.

  • The second imports the applet package, which contains the class Applet. Every applet that you create must be a subclass of Applet.

The class SimpleApplet, must be declared as public, because it will be accessed by code that is outside the program.


paint( ) method is defined by the AWT and must be overridden by the applet. paint( ) is called each time that the applet must redisplay its output. paint( ) is also called when the applet begins execution. The paint( ) method has one parameter of type Graphics. This parameter contains the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required.
Inside paint( ) is a call to drawString( ), which is a member of the Graphics class. This method outputs a string beginning at the specified X,Y location. It has the following general form:

void drawString(String message, int x, int y)

Here, message is the string to be output beginning at x,y. In a Java window, the upper-left corner is location 0,0. The call to drawString( ) in the applet causes the message “A Simple Applet” to be displayed beginning at location 20,20.

Unlike Java programs, applets do not begin execution at main( ). In fact, most applets don’t even have a main( ) method. Instead, an applet begins execution when the name of its class is passed to an applet viewer or to a network browser.

  • There are two ways in which we can run an applet:

  •  Executing the applet within a Java-compatible web browser.

  • Using an applet viewer, appletviewer. An applet viewer executes applet in a window. This is generally the fastest and easiest way to test an applet.

  • To execute an applet in a web browser, we need to write a short HTML text file that contains a tag APPLET. Here is the HTML file that executes SimpleApplet:

<applet code="SimpleApplet" width=200 height=60> </applet>

The width and height statements specify the dimensions of the display area used by the applet. After we create this file, we can execute our browser and then load this file, which causes SimpleApplet to be executed.

  • To execute SimpleApplet with an applet viewer, we may also execute the HTML file shown earlier. For example, if the preceding HTML file is called RunApp.html, then the following command line will run SimpleApplet:


C:\>appletviewer RunApp.html

  • A more convenient method is to include a comment at the head of our Java source code file that contains the APPLET tag. By doing so, our code is documented with a prototype of the necessary HTML statements, and we can test our compiled applet merely by starting the applet viewer with our Java source code file. If we use this method, the SimpleApplet source file looks like this:

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

/*<applet code="SimpleApplet" width=200 height=60> </applet>*/

public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet", 20, 20);
}
}

Compile the program and execute the applet viewer, specifying the name of  applet’s source file. The applet viewer will encounter the APPLET tag within the comment and execute the applet.

 

Click for NEXT article. 

 

Please feel free to correct me by commenting your suggestions and feedback.

No comments:

Post a Comment