Applet How To

How To Use the Plotting Widget in an Applet

Introduction to Applets

Unfortunately applet support in Java is currently in a sorry state, and there are many problems in using applets in the real world:

  • Java support in IE and Netscape use a very old version of Java (JDK 1.1.5?)
  • IE and Netscape support a slightly different version of Java
  • Netscape on Linux has a habit of totally hanging when trying to load Java.

One alternative is to use the Java Plugin, which bypasses the native browser Java support, but it has its own set of problems:

  • It is only supported on some platforms
  • Not many users have it installed
  • It requires different HTML from the normal <APPLET> tag.
  • It creates a separate VM for each applet

In addition there are a number of problems relating to using JASHist in a Java Applet

  • Java applets are limited to only opening socket connections with the host they were downloaded from. This means that plots which require dynamic data can only fetch that data from the host they were downloaded from. Applet signing, which in principle allows applets to free themselves from this restriction, is supported differently in IE and Netscape, and is in either case very hard to use.
  • JASHist now uses Swing, which is not installed by default in either Netscape or IE, and must therefore be downloaded, either manually by the user, or automatically each time the applet is run. The swing library is large, and therefore takes a long time to download and, in the case of Netscape, takes a long time to "verify" after it is downloaded. (In IE the verification is much faster).
  • The JASHist widget itself is now quite large, and can therefore be quite slow to download.

All of this is pretty depressing, and is why I strongly encourage the use of servlets instead of applets if at all possible. Maybe things will get better in the future, the next release of Netscape (if it ever arrives) is supposed to support Java 2, and be easily upgradeable to the latest Java. In addition Java Web Start looks like an interesting technology which could take the place of applets in many cases.

If I have not convinced you otherwise, read on for information on using JASHist in an applet.

Using the JAS Plotting Widget in an Applet

In principle using JASHist in an applet is straightforward, just create an applet in the normal way, and attach one or more data sources to it. If you want your applet to display data from a remote source you will need to implement your DataSource so it can fetch the data remotely, taking into account the security restrictions placed on applets.

The following shows a very simple applet:

import javax.swing.*;
import jas.hist.*;
import jas.hist.test.MemoryDataSource;
import java.awt.Color;

public class JASApplet extends JApplet
{
   private JASHist plot;
   
   public JASApplet()
   {
      plot = new JASHist();
      plot.setBackground(Color.white);
      plot.setTitle("Java Memory Usage");
      plot.getYAxis().setLabel("MB");
      plot.getXAxis().setLabel("Time");
      getContentPane().add(plot);
   }
   public void start()
   {
      plot.addData(new MemoryDataSource()).show(true);
      super.start();
   }
   public void stop()
   {
      super.stop();
      plot.removeAllData();
   }
}

To embed this in an HTML page you will need to create some HTML like this:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft FrontPage 4.0">
<TITLE></TITLE>
</HEAD>
<BODY>
<h1><a href="../default.shtml">JAS Applet</a> Example</h1>
<p>
<APPLET code="JASApplet" width="300" height="300" archive="swingall.jar,jas-applet.jar">
Java Applet Support Required
</APPLET>
</p>
</BODY>
</HTML>

Note, if you have instructed your users to manually Swing-Enable their browser then you do not need to include swingall.jar. The jas-applet.jar used here is a cut down version of the standard jas.jar, which includes only the code needed for the applet. You can download the file here, or build it yourself from the JAS source files like this:

cd Base
java -jar ../jmk.jar jas-applet.jar

To try running this sample applet in your own browser, click here.


Page last updated: Wednesday, January 14, 2004 by SLAC\tonyj