Applets

An Applet is a special kind of Java program that is run when embedded in another application (usually a Java-enabled web browser or an applet viewer). The java.applet package provides a standard framework for developing applets. The method main() is not necessary for running applets.

The template for writing an applet is as follows:


public class AnApplet extends Applet
{
  // Default constructor called by the browser when the applet
  // is initially loaded
  public AnApplet ()
  {
    ...
  }

  // Called by the browser after the applet is loaded
  public void init ()
  {
    ...
  }

  // Called by the browser after the init() method or every time
  // the Web page is revisited
  public void start ()
  {
    ...
  }

  // Called by the browser when the page becomes inactive
  public void stop ()
  {
    ...
  }

  // Called by the browser when the web browser exits
  public void destroy ()
  {
    ...
  }

  // Other methods
}

This is a simple applet in AWT.

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

public class HelloApplet extends Applet
{
  public void paint ( Graphics g )
  {
    g.drawString ( "Hello World!", 50, 25 );
  }
}

The applet can be viewed with an applet viewer or embedded in an HTML page.

The complete syntax for the applet tag is given below. The parameters in the square brackets [] are optional.

applet
  [codebase = applet_url]
  code = className.class
  width = applet_viewing_width_in_pixels
  height = applet_viewing_height_in_pixels
  [archive = archiveFile]
  [vspace = vertical_margin]
  [hspace = horizontal_margin]
  [align = applet_alignment]
  [alt = alternative_text]
codebase specifies the URL of the directory in the class files and other resources are located.
archive instructs the browser to load an archive file that contains all the classes and resources needed to run the applet.
vspace and hspace specify the size in pixels of the blank margin to pad around the applet vertically and horizontally.
align specifies how the applet will be aligned in the browser. It can have values like: left, right, top, middle, bottom.
alt attribute specifies the test that will be displayed in case the browser cannot run the applet.

The Applet class is an AWT class and is not designed to work with Swing controls. To use Swing components the Applet should extend javax.swing.JApplet. JApplet inherits all the methods from the Applet class.

Information can be supplied to an applet using the PARAM elements in the applet HTML file. Parameters are declared using the param tag. The param tag must be embedded in the applet tag and has no end tag. The tag specifies a parameter and its corresponding value.


param name = parametername value = parametervalue 

This is an example of how the information regarding the background and foreground color can be passed to an applet. In the applet HTML file the code reads:


applet code = HelloApplet.class width = 200 height = 150 
param name = "background" value = "ddffbb"
param name = "foreground" value = "880000"


The information is accessed by the applet using the getParameter() method.

public void init ()
{
  Color foreground = getColorParam ( "foreground" );
  Color background = getColorParam ( "background" );

  if ( foreground != null ) setForeground ( foreground );
  if ( background != null ) setBackground ( background );
}

private Color getColorParam ( String paramName )
{
  String paramValue = getParameter ( paramName );
  try
  {
    return new Color ( Integer.parseInt ( paramValue, 16 ) );
  }
  catch ( NumberFormatException e )
  {
    return null;
  }
}

Applets can handle images (GIF and JPEG formats) and audio files (AU, AIFF, WAV, and MIDI files). The Image class provides a platform independent interface to handle images off screen. An image that is to be displayed must first either be loaded from an external source or created from scratch. The method signature for getImage() reads:


Image getImage ( URL url, String name )

The url must specify an absolute URL and the name is the name of the image file. The image is then displayed using the method drawImage() from the Graphics class. The following piece of code shows how it is done.

Image image;

public void init ()
{
  image = getImage ("/project/applets/", "Picture.gif");
}

public void paint ( Graphics g )
{
  g.drawImage ( image, 10, 10, this );
}

Audio clips are handled by an interface called AudioClip which all audio clips implement. The AudioClip interface has these three methods - loop(), play(), and stop(). The loop() method plays the audio clip continuously. The play() method plays it only once and the stop() method stops the playing. There is also a method getAudioClip() for retrieving sound files specified by a URL.


AudioClip audioClip;

public void init ()
{
  String audioFile = getParameter ( "audiofile" );
  if ( audiofile != null )
    audioClip = getAudioClip ( "/projects/applets", audioFile );
}

public void start ()
{
  if ( audioClip != null ) audioClip.loop();
}

public void stop ()
{
  if ( audioClip != null ) audioClip.stop();
}

public void destroy ()
{
  audioClip = null;
}

The web browser makes a net connection to load the applet class file specified in the applet HTML file. The class loader of the JVM used by the web browser then resolves any other classes needed by the applet. The web browser may make a new net connection for each class required to run the applet. To save time all the files (images and sound files included) that comprise an applet can be put into a Java Archive (JAR). The JAR file is specified by using the ARCHIVE attribute.

applet archive = "HelloApplet.jar" code = "HelloApplet.class" width = 200 height = 150
The web browser can download the JAR file by making a single net connection. JAR files can be made using the jar tool. The jar command is similar to the Unix tar command. Files to be included are listed on the command line after the JAR file name.

jar cf HelloApplet.jar HelloApplet.class image.gif music.au

Applets loaded over the net have certain restrictions due to security reasons. The following limitations are fairly common for most web browsers:

Algorithms

An algorithm is a set of rules for carrying out some calculation. A problem (say sorting a list of numbers) will have many instances. An instance, in this case, can be the list {23,15, 67}. An algorithm must work correctly on every instance of the problem it claims to solve. There may be several algorithms to solve the same problem. How do we choose the best one?

The empirical approach is to program the competing algorithms and try them on different instances with the help of a computer. The theoretical approach is to determine mathematically the quantity of resources needed by each algorithm as a function of the size of the instances considered. The resources are computing time and storage space. Computing time being the more critical of the two.

The size of an instance is any integer that in some way measures the number of components in an instance.

Time taken by an algorithm can vary considerably between two different instances of the same size. For example, sorting an already sorted list or list that is sorted in reverse order

We usually consider the worst case, i.e. for a given instance size we consider those instances which requires the most time. The average behavior of an algorithm is much harder to analyze.

We analyze an algorithm in terms of the number of elementary operations that are involved. An elementary operation is one whose execution time is bounded by a constant for a particular machine and programming language. Thus within a multiplicative constant it is the number of elementary operations executed that matters in the analysis and not the exact time.

Since the exact time for an elementary operation is unimportant, we say, that an elementary operation can be executed at unit cost. We use the "Big O" notation for the execution of algorithms. The "Big O" notation gives the asymptotic execution time of an algorithm.

Algorithms can be classified using the "Big O" notation.