CISC 370 Lecture Notes for Class No. 18 April 13, 2000

Course Home Prev Lect Next Lect Example Progs Exercise Solns
CoreJava Progs
Documentation
Gen'l Docs Java 2 APIs Java Glossary Term Proj Info

Assignment

Reading:
Horstmann & Cornell, Chap 10
Exercises:

Today's Lecture Topics

  1. Applets

    "Applets let you do some tasks that you can't do with HTML. A popular early use of applets was to provide user-interface components (i.e., buttons, boxes, and so forth) on the Web page. In the beginning, Java applets were just about the only game in town for this functionality. Now, you can do such tasks with ActiveX controls as well, or with Dynamic HTML -- probably the easiest and most lightweight method. Applets also provide limited cross-platform and cross-browser compatibility. Unfortunately, their limitations make it difficult to write an applet once, and have it run everywhere."

    The above quote was taken from a Microsoft article "DHTML? Applets? Controls? Which to Use?" For more on the pros and cons of each of these technologies, see the entire article, which despite its Microsoft-centric view provides some good information.

    A traffic light example with which we can demo when init(), start(), stop(), and destroy() are called.

  2. Steps for setting up an applet that uses the Java 2.0 Swing components

    Since most browsers implement version 1.x of Java (not version 2 that we are using), it is fairly painful to debug and use JApplets at this time. Furthermore they will be currently be unviewable with most people's browsers (but this should change with time), so Java 2.0 applets are not currently recommended for real web applications (at least without taking necessary steps to insure that they are usable by all).

    To make Java 2.0 applets work for us, we need two pieces of additional software. One is the Java 2.0 plugin for whatever browsers is being used. This has already been done for a version of Netscape that is available on the composers (that is, copland, strauss, and mahler). It is invoked with the command

    netscapeWjpi

    once you have added the directory $CISC370HOME/bin to your Unix path. The name is meant as an acronym for "netscape with the Java plug in."

    There is a download from SUN to upgrade the browsers on your PC if you do not already have it. As a test of your PC browsers, try viewing a Java 2.0 applet, for example, the one that we will demo today or the NotAHelloWorld applet from chap 10 of the textbook (the latter is online via the CoreJavaBook link from the top of various course html pages) to see if you can view the applet. If you can, you already have the plugin. If not, a window should appear with instructions on how to download the required plugin.

    An applet is invoked from an html document. Normally, there is a fairly straightforward way to do this, but with the Java 2.0 plugin the invoking html code is much more complicated. Fortunately, SUN has developed an html converter that will convert the simple html code, ordinarily needed, into the more complicated code needed to work with the plugin. This software has been installed on the composers and may be invoked by the command

    htmlconverter

    once you have added the directory $CLASS370HOME/bin to your Unix directory. To add this directory to your path, add the line

    setenv NEWPATH ${NEWPATH}:$CISC370HOME/bin

    to your .cshrc or .localenv file on strauss AFTER the line

    setenv CISC370HOME /home/base/usrc/35/13772/Class/CISC370-00S

    If you need assistance doing this, see me or the TA Sivaram Burra.

    If you wish to have the converter software on your PC, it must be downloaded from the SUN java web site. Steer your PC browser to http://java.sun.com/products/plugin/1.2/features.html and follow the directions that you will find there.

    Detailed instructions on using the converter can be found at http://java.sun.com/products/plugin/1.2/converter_readme.txt .

    Assuming you have taken the necessary steps to use the above two pieces of software, here is a summary of the steps to take to implement and debug a Java 2.0 applet.

    1. First write the code for the applet class that must extend the javax.swing.JApplet class.

    2. Write the HTML code for the .hmtl file. The HTML code for the traffic light applet can be found in the file $CISC370HOME/example-progs/applets/trafficLight/TL_Applet.html . View this file with an editor or a command line command like more. If you access it with a browser, you will see the HTML page as displayed by the browser and not the source that produced it.

    3. Debug the applet using the appletviewer.

    4. Use the htmlconverter program to convert the html file to an html file that can be used with the Java 2.0 plugin for use with netscape, internet explorer, or hotjava.

    5. Make the .html and all the java .classs readable to the world. Use the Unix chmod command to do this. They also have to be in a sub-directory of your public_html directory so that a browser can access them.

    6. Debug the applet using a browser with the Java 2.0 plugin.

  3. For a good applet demo of many of the Swing components, see SUN's SwingSet demo . This is a large Java applet and takes a long time to load so be patient. This is a good demo for getting ideas of components to use with term projects.

Back to the CISC 370 homepage.

This page has been accessed times since 13 April 2000.

Corrections, suggestions and comments to Bob Caviness

Copyright 2000 B. F. Caviness