|
ION Java User's Guide: Building ION Applets and Applications |
|
When creating your ION applet, keep the following points in mind.
| Tip It's a good idea to shut down and restart the browser any time you make a changes to your HTML file or your class files. |
In addition to the standard Java packages (and any other packages used in your applet), you must import the ION package with the statement:
import com.itt.ion.*
ION applets are subclassed from (they extend) the Java Applet class. When defining your applet class, use a statement similar to the following:
public class MyIONApplet extends Applet
where MyIONApplet is the name of your applet class.
See Simple Applet Example for an example. For a basic overview of Applets, consult a Java reference.
To include your compiled applet in an HTML page, use the <APPLET> tag with the NAME, CODE, WIDTH, and HEIGHT attributes:
<APPLET NAME="myIONApplet" CODE=myIONApplet.class WIDTH=300 HEIGHT=300 > </APPLET>
For more information, see Using ION's Pre-Built Applets.
ION applets must have access to the ION class files in order to run. While you can use the CODEBASE attribute to specify a relative path from the location of an HTML page containing an ION applet tag to the location of the class files, it is often easier to copy the class files (or provide a symbolic link, if your system supports symbolic links) to another directory located in or near the directory containing your HTML files.
For example, suppose you have located your HTML pages in a directory named public_html. You may wish to place the ION package, the ION ZIP file, and the ION JAR file in a subdirectory of public_html named java. If you then include any ION applet class files you create in the java directory, you could simply specify:
CODEBASE="./java"
in the <APPLET> tag used in your HTML page.
See CODEBASE for further details.
When a web browser encounters an HTML page that contains a Java applet, the class files that make up the applet are downloaded from the web server into the browser. The applet is executed only after all of the necessary class files have been downloaded. Because a separate HTTP connection between the client and the server is established for each class file, the download time for a large applet (an applet with many class files) can be substantial.
To increase the download performance of Java applets, consider using a Java Archive file, or JAR file, detailed in Number 3 of the following section. A JAR file can contain multiple class files, thus avoiding the need for multiple connections. A JAR file can also be compressed, further speeding the download process. Most modern browsers support the JAR format.
To support the different methods used by different browsers to download Java class files, ION provides three separate versions of the ION class library. These are:
com/itt/ion directory structure within the classes directory of the ION distribution. Each file is downloaded to the browser via a separate connection to the server.
Use raw Java class files with browsers that don't support the ARCHIVE attribute to the APPLET tag. For example, Version 3 of Microsoft's Internet Explorer does not support the ARCHIVE attribute.
| Note To use this method, you must copy the com directory and all its subdirectories to your Web server since the raw Java class files are not copied to your Web server during ION installation. |
ion_64.zip contains all of the Java class files included in the ION package. This ZIP file is located in the classes directory of the ION distribution, and can be downloaded via a single connection to the server.
Use the ZIP file with browsers that support the ARCHIVE attribute and support compressed archive files. For example, version 4 of Netscape's Navigator supports the ARCHIVE attribute and compressed JAR files.
ion_64.jar contains the Java class files included in the ION package. This JAR file is located in the classes directory of the ION distribution, and can be downloaded via a single connection to the server.
Use the JAR file with browsers that support uncompressed archive files. For example, version 3 and later of Netscape's Navigator supports uncompressed JAR files.
| Note This section is relevant only for ensuring support of browsers prior to Netscape 4 or Internet Explorer 5. |
Use the following procedure to create a set of HTML pages that will use the most efficient download method for any of the three browser types defined above.
classes subdirectory of the ION distribution. This directory should be specified via the CODEBASE attribute to the APPLET tag. See CODEBASE for more information.
ARCHIVE="ion_64.zip"). The other page should include a reference to the compressed archive file (ARCHIVE="ion_64.jar"). Browsers that do not support the ARCHIVE attribute will ignore it and download the unarchived files.
<SCRIPT language="JavaScript">
<!--
navigator.onerror = null;
version = ( parseInt(navigator.appVersion) > 3 ? "4" : "3");
if(version == "4"){
// Version 4 can handle jar files, load the Jar page
location.replace("JAR_page.html");
} else {
location.replace("ZIP_page.html");
}
// -->
</SCRIPT>
where JAR_PAGE.html is the name of the HTML page that references the ion_64.jar file and ZIP_PAGE.html is the name of the HTML page that references the ion_64.zip file. For example, you may name the page that references the JAR file myfile_j.html and the file that references the ZIP file myfile_z.html.
The following Java code creates a simple applet that displays an IDL graphic. The example constructs an applet named Commands; the code is available in the examples/src directory in a file named commands.java.
| Note The characters "//" denote comments in Java code. |
// -*-C++-*-
//
// commands.java
//
//
/*****************************************************************
Copyright (c) 1997-2002, ITT Visual Information Solutions, Inc.
All rights reserved. Unauthorized reproduction prohibited.
(Of course, because these are examples, feel free to remove these
lines and modify to suit your needs.)
******************************************************************
/
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import com.itt.ion.*; // Import the ION Package
public class commands extends Applet implements
IONDisconnectListener
{
// Instance Vars
IONGrConnection c_ionCon; // the ion connection
IONGrDrawable c_ionDrw; // the ION drawable
Dimension c_dimApp; // Size of drawing area
int c_bConnected=0; // 0 => !conn, 1 => conn, -1 =>
conn failed
// ******************************
// Init Method
// ******************************
public void init(){
// Create connection and drawable objects
c_ionCon = new IONGrConnection();
c_dimApp = getSize();
c_ionDrw = new IONGrDrawable(c_dimApp.width, c_dimApp.height);
// Add the drawable to the AWT tree
setLayout(new GridLayout(1, 1));
add(c_ionDrw);
}
/*
*************************************************
* Inorder to display status messages at startup and also
* to be able to disconnect when the page is not being viewed
* we override the Applets start() and stop() methods
*************************************************
* start()
*
* Purpose:
* Overide the applet's start method.
* Connect to IONJ if not already connected.
*
* Note: in pre-ION1.4 releases, this method called repaint.
* repaint then would call our paint method (now deleted from this
* file). The paint method was responsible for connecting.
* However, in some cases our paint method would not be called and
* the applet would not get its data from the server.
* We are now guaranteed that we will connect to the IONJ server
* because start() will always be called when the applet starts.
*/
public void start(){
if(c_bConnected == 0) // Not connected to ION, do so.
connectToServer();
}
/*
*************************************************
* stop()
*
* Purpose:
* Override the applet's stop method. This method
* Is called when the page is not being viewed. We
* disconnect from the server when this is the case.
*/
public void stop(){
if(c_bConnected == 1){
c_ionCon.removeIONDisconnectListener(this);
c_ionCon.disconnect();
c_bConnected=0;
}
}
/*
************************************************
* connectToServer()
*
* Purpose:
* Connects to the ION server, providing feedback to user
*/
private void connectToServer(){
// Write Status message
writeMessage("Connecting to ION Java Server...");
// Connect to the server
try {
c_ionCon.connect(this.getCodeBase().getHost());
} catch(UnknownHostException eUn) {
System.err.println("Error: Unknown Host.") ;
writeMessage("Error:Unknown Host.");
c_bConnected = -1;
return;
} catch(IOException eIO) {
System.err.println("Error: Establishing Connection. ION
Java Server Down?");
writeMessage("Error: Establishing Connection. ION Java
Server Down?");
c_bConnected = -1;
return;
} catch(IONLicenseException eLic){
System.err.println("Error: ION Java License Unavailable.")
;
writeMessage("Error: ION Java License Unavailable.");
c_bConnected = -1;
return;
}
c_bConnected = 1;
c_ionCon.addIONDisconnectListener(this);
// Add the drawable to the connection
c_ionCon.addDrawable(c_ionDrw);
writeMessage("Drawing Graphics..."); // message to screen
// Issue IDL commands to generate a plot
try {
// Set the color table
c_ionCon.executeIDLCommand("loadct, 15");
// Create some data
c_ionCon.executeIDLCommand("a = dist(30)");
// Draw a contour plot
c_ionCon.executeIDLCommand("show3, a");
//Note that it is generally faster to package multiple
//IDL commands into a single .pro to call. This example
//sends commands separately so that the code is easier to
//follow.
} catch(Exception e) {
String smsg;
if(e instanceof IOException)
smsg = "Communication error:"+e.getMessage();
else if(e instanceof IONSecurityException )
smsg = "ION Java security error";
else if(e instanceof IONIllegalCommandException )
smsg = "Illegal IDL Command detected on server.";
else
smsg = "Unknown error: "+e.getMessage();
System.err.println("Error: "+smsg);
writeMessage("Error: "+smsg);
return;
}
writeMessage("Done");
}
/*
************************************************
* IONDisconnection()
*
* Purpose:
* Called when the connection is broken (can report reason).
*/
public void IONDisconnection(int i){
System.err.println("Server Connection Closed");
writeMessage("Server Connection Closed");
if(c_bConnected == 1)
c_bConnected = 0;
}
/*
***********************************************
* writeMessage()
*
* Purpose:
* Utility method that is used to write a string to the
* screen using Java.
*/
private void writeMessage(String sMsg){
showStatus(sMsg);
System.out.println(sMsg);
}
}
Example code illustrating ION features is included in the installed ION distribution.
| Example Code You will find example HTML files located in the examples directory in your installed ION distribution. The raw Java source files for the example ION classes are included in the src subdirectory of the examples directory. Also included in the examples directory are a number of IDL .pro files that are called by the ION demonstration applets. |
See Running the ION Java Examples for more information.
| Note For the examples to function properly, you must have the ION Server running on your server machine. If you do not yet have the ION Server running on your system, visit ITT Visual Information Solution's ION Web site and view ION examples there. |
You can use scripting languages such as JavaScript and VBScript to control ION applets included on an HTML page by calling ION methods that are available to all applets. Communication between scripts and applets gives you a simple way to create interactive HTML pages that build on ION's pre-built applets.
Two competing scripting languages are currently available for use in HTML pages: JavaScript and VBScript. JavaScript was developed by Netscape for use in its Navigator browser; VBScript was developed by Microsoft for use in its Internet Explorer browser. While the two scripting languages have much in common, they do differ in ways that are beyond the scope of this manual to describe. In the context of writing scripts that communicate with ION applets, the important differences are:
The practical result of this situation is that in order to create HTML pages that allow users of both Netscape's Navigator and Microsoft's Internet Explorer to interact with ION applets via scripts, you must write HTML code that decides "on the fly" which scripting language to use.
The simplest way to provide pages that use JavaScript for Netscape browsers and pages that use VBScript for Microsoft browsers is to use a "gateway" HTML page that loads one of two other HTML pages depending on the type of browser. The following HTML page uses JavaScript statements to detect whether the browser accessing the page is Netscape Navigator. If so, it loads a JavaScript version of the HTML page; otherwise it loads a VBScript version of the HTML page.
<HTML>
<! This page refers IE or Netscape to the proper ION example >
<SCRIPT language=JavaScript>
// <!--
var browser = navigator.appName;
if (browser.indexOf ("Netscape") != -1)
location = "javascript.html"; // jump to JavaScript page
else
location = "vbscript.html"; // jump to VBScript page
// -->
</SCRIPT>
</HTML>
| Note The script above assumes that the browser is either Navigator or Internet Explorer. Currently, the vast majority of browsers in use are one of these two; still, you may wish to make your own "gateway" HTML page more robust. |
The following methods are available for communication between scripting languages and ION applets:
where string is a valid IDL command string. The executeIDLCommand() method allows you to execute any IDL command via a script, with IDL's output going to the specified applet's drawing area.
For example, if you have an IONSurfaceApplet named MYSURF, you could use the following JavaScript statement to change the color table when the user presses a button:
document.MYSURF.executeIDLCommand("LOADCT, 5");
See Example: Using JavaScript for a more complete discussion.
Use this method to disconnect from the ION Server.
The following HTML code demonstrates the use of JavaScript to interactively update an ION graphic. The example includes an IONGraphicApplet that displays a shaded surface, uses a JavaScript select object to create a pulldown list of rotation values, and adds a button to rotate the surface to the selected angle.
| Note The following script will work in Netscape's Navigator browser, but not in the Microsoft's Internet Explorer browser. |
<!-- Define the HTML header. Note that the JavaScript is
included in the HEAD section. -->
<HTML>
<HEAD>
<TITLE>Simple JavaScript Applet Test</TITLE>
<!-- The script language is JavaScript. We declare the variable
rotation with an initial value of 30 degrees. -->
<SCRIPT language=JavaScript>
var rotation = "30";
// The getSelectedValue() function returns the text associated
// with the value chosen from the pulldown list.
function getSelectedValue(sel) {
return sel.options[sel.selectedIndex].text
}
// The rot_surf() function retrieves the rotation value and
// executes the IDL command to re-draw the graphic. It is called
// when the button is clicked.
function rot_surf() {
rotation = getSelectedValue(document.command_form.rot_value);
document.SURFAPP.executeIDLCommand("SHADE_SURF, a,
AZ="+rotation);
}
</SCRIPT>
</HEAD>
<BODY>
<!-- JavaScript input controls must be contained in an
HTML form. -->
<FORM NAME="command_form">
<!-- Create an IONGraphicApplet applet named "SURFAPP" that
generates some data and creates a shaded surface. Note that the
CODEBASE attribute is set to "../classes". This is the proper path
for the example as installed with the ION documentation
files.-->
<APPLET NAME="SURFAPP" CODE=com.itt.ion.IONGraphicApplet.class
CODEBASE="../classes" WIDTH=200 HEIGHT=200>
<PARAM NAME="DEBUG_MODE" VALUE="YES">
<PARAM NAME="SERVER_DISCONNECT" VALUE="NO">
<PARAM NAME="DECOMPOSED_COLOR" VALUE="NO">
<PARAM NAME="IDL_COMMAND_0"
VALUE="a = EXP(-(SHIFT(DIST(30), 15, 15)/7)^2)">
<PARAM NAME="IDL_COMMAND_1" VALUE="LOADCT, 5">
<PARAM NAME="IDL_COMMAND_2" VALUE="SHADE_SURF, a">
</APPLET>
<BR>
<!-- Create the pulldown menu of rotation values -->
<SELECT NAME="rot_value" SIZE=1>
<OPTION VALUE=15>15
<OPTION VALUE=30 SELECTED>30
<OPTION VALUE=45>45
<OPTION VALUE=60>60
<OPTION VALUE=75>75
<OPTION VALUE=90>90
</SELECT>
<!-- Create the "Rotate Surface" button, which calls the
JavaScript function rot_surf().-->
<INPUT TYPE=BUTTON NAME="rot_button" VALUE="Rotate Surface"
onClick="rot_surf()">
</FORM>
</BODY>
</HTML>
See Notes on the Differences Between the JavaScript and VBScript Versions.
The following HTML code demonstrates the use of VBScript to interactively update an ION graphic. The example includes an IONGraphicApplet that displays a shaded surface, uses a VBScript select object to create a pulldown list of rotation values, and adds a button to rotate the surface to the selected angle. The line numbers are provided to aid in discussion; they are not part of the HTML code.
| Note The following script shown will work in Microsoft's Internet Explorer browser, but not in the Netscape Navigator browser. |
<!-- Define the HTML header. Note that the VBScript is included in
the HEAD section. -->
<HTML>
<HEAD>
<TITLE>Simple VBScript Applet Test</TITLE>
<!-- The script language is VBScript. We declare the variable
rotation with an initial value of 30 degrees. -->
<SCRIPT language=VBScript>
Dim rotation
rotation = "30"
// The rot_button_OnClick() subroutine retrieves the index of
// the value selected in the pulldown list, uses the index to
// retrieve the text value, and executes the IDL command to redraw
// the graphic.
sub rot_button_OnClick()
ind = document.command_form.rot_value.selectedIndex
rotation = document.command_form.rot_value.options(ind).value
document.SURFAPP.executeIDLCommand("SHADE_SURF, a,
AZ="+rotation)
end sub
</SCRIPT>
</HEAD>
<BODY>
<!-- Create an IONGraphicApplet applet named "SURFAPP" that
generates some data and creates a shaded surface. Note that the
CODEBASE attribute is set to "../classes". This is the proper path
for the example as installed with the ION documentation
files. -->
<APPLET NAME="SURFAPP" CODE=com.itt.ion.IONGraphicApplet.class
CODEBASE="../classes" WIDTH=200 HEIGHT=200>
<PARAM NAME="DEBUG_MODE" VALUE="YES">
<PARAM NAME="SERVER_DISCONNECT" VALUE="NO">
<PARAM NAME="DECOMPOSED_COLOR" VALUE="NO">
<PARAM NAME="IDL_COMMAND_0"
VALUE="a = EXP(-(SHIFT(DIST(30), 15, 15)/7)^2)">
<PARAM NAME="IDL_COMMAND_1" VALUE="LOADCT, 5">
<PARAM NAME="IDL_COMMAND_2" VALUE="SHADE_SURF, a">
</APPLET>
<!-- VBScript input controls must be contained in an
HTML form. -->
<FORM NAME="command_form">
<BR>
<!-- Create the pulldown menu of rotation values. -->
<SELECT NAME="rot_value" SIZE=1>
<OPTION VALUE=15>15
<OPTION VALUE=30 SELECTED>30
<OPTION VALUE=45>45
<OPTION VALUE=60>60
<OPTION VALUE=75>75
<OPTION VALUE=90>90
</SELECT>
<!-- Create the "Rotate Surface" button. The
rot_button_OnClick() subroutine is called automatically
when this button is clicked. -->
<INPUT TYPE=BUTTON NAME="rot_button" VALUE="Rotate Surface">
</FORM>
</BODY>
</HTML>
In JavaScript, you must explicitly tie a control (a button, for example) to a JavaScript function. VBScript automatically looks for a subroutine name based on the name of the button.
This section includes suggestions that may be useful in some situations. Make sure your installation meets the criteria defined here before implementing any of these suggestions.
If your installation is used only by a known set of users who all use Netscape's Navigator version 4 or later, you can eliminate the need to download the Java class files when an applet loads. Do the following:
ion_release.jar file in theProgram/java/classes subdirectory of their local Netscape directory.
| Note The Java security mechanism requires that applet classes must be loaded from the server on which ION is running. This means that the approach described here will fail with a security error if the applet class files are not located in the com/itt/ion subdirectory of the directory specified by the CODEBASE attribute. |
If your applet includes a stop() method, it will be invoked automatically when the browser leaves the browser window. In your applets, it is good practice to include a stop() method that closes the ION connection and does any other cleanup that may be necessary.
IDL's animation routines all rely on the IDL widget toolkit, and are thus not suitable for use with ION. You can, however, use IDL to create the individual frames of an animation and create an ION applet to build an array of frames and display the animation on the client side (in a browser or Java application).
| Example Code An example application that does this sort of client-side animation is included in the ION distribution. Point your browser at the file animation.html in the demo subdirectory of the examples directory. The Java sources for the animation classes are included in the src subdirectory of the classes directory. Note that the animation demo relies on an IDL .pro file; see Running the ION Java Examples. |
| Tip You can create an MPEG file on-the-fly with IDL and then supply a link to it. If the client browser has a common MPEG plug-in, you can play back the animation without requiring a special java applet. MPEG support in IDL requires a special license. For more information, contact your ITT Visual Information Solutions sales representative. |
IDL Online Help (March 06, 2007)