|
How To Use the Plotting Widget in an AppletIntroduction to AppletsUnfortunately applet support in Java is currently in a sorry state, and there are many problems in using applets in the real world:
One alternative is to use the Java Plugin, which bypasses the native browser Java support, but it has its own set of problems:
In addition there are a number of problems relating to using JASHist in a Java Applet
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 AppletIn 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