home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Note that TRaceApplet supports parameters from the HTML <PARAM> tag. Lines 21 through 28 (as follows) look for a parameter named NUMBER.

      String theParameter = getParameter(“NUMBER”);
      if (theParameter == null)
        fRacerCount = 0;
      else
        fRacerCount = Short.parseShort(theParameter);
    }
    if (fRacerCount <= 0)
      fRacerCount = 2;

If the parameter is found, it is used as the number of racers (fRacerCount). If it can’t be found, if it is unreadable, or if it is less than or equal to zero, the program defaults to two racers.

How is the HTML author to know that this applet supports this parameter? Many browsers can call the getParameterInfo() method. TRaceApplet implements this method in lines 114 through 124 (as follows).

   public String[][] getParameterInfo()
   {
    short theMaximumNumberOfThreads =
 ⇒(short)(Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1);
    String theParameterInfo[][] =
    {
      {“NUMBER”,    “1-”+theMaximumNumberOfThreads,   
 ⇒”number of racers”},
    };
    return theParameterInfo;
   }

If the user requests this information, the program returns the parameter name, type, and range. Figure 37.10 shows how AppletViewer handles this request.

Listing 37.7 shows an HTML file that takes advantage of this feature.


FIGURE 37.10  Choose Info from the Applet menu in AppletViewer to see the applet and parameter information.

Listing 37.7 autogen_Race1.html—This HTML Was Originally Written by Visual Cafè, from Symantec


 <HTML>
 <HEAD>
 <TITLE>Autogenerated HTML</TITLE>
 </HEAD>
 <BODY>
 <APPLET CODE=”TRaceApplet.class” WIDTH=430 HEIGHT=270>
 <PARAM NAME=”NUMBER” VALUE=”5">
 </APPLET>
 </BODY>
 </HTML>

TRaceApplet requires two additional files: TRacer.java, which contains the class definition for the racer itself, and TSuspendable.java, an interface that supports pausing and unpausing the race with a mouse click. These files are shown in Listings 37.8 and 37.9, respectively.

Listing 37.8 TRacer.javaTRacer Is a Runnable Canvas and Is Responsible for Its Own User Interface


 import java.awt.Graphics;
 import java.awt.Color;

 public class TRacer extends java.awt.Canvas implements
 ⇒Runnable
 {
   private short fPosition = 0;
   private String fName;
   static private final short kNumberOfSteps = 1000;
   TSuspendable fAncestor;

   public TRacer(String theName, TSuspendable theAncestor)
   {
     fName = new String(theName);
     fAncestor = theAncestor;
   }
  
   public TRacer(String theName)
   {
     fName = new String(theName);
     fAncestor = null;
   }

   public synchronized void paint(Graphics g)
   {
     g.setColor(Color.black);
     g.drawLine(0,getSize().height/2, getSize().width,
 ⇒getSize().height/2);
     g.setColor(Color.red);
     g.fillOval(fPosition*getSize().width/kNumberOfSteps, 0,
 ⇒15, getSize().height);
   }

   public void run()
   {
     while (fPosition < kNumberOfSteps)
     {
       fPosition++;
       repaint();
       try
       {
         Thread.currentThread().sleep(10);
         if (fAncestor != null)
         {
           synchronized (fAncestor)
           {
             if (fAncestor.isSuspended())
               fAncestor.wait();
           }
         }
       } catch (InterruptedException e)
       {
         System.err.println(“Thread “ + fName + “ interrupted.”);
         System.exit(1);
       }
     }
     System.out.println(“Thread “ + fName + “ has finished
 ⇒the race.”);
   }
  
   public static void main(String argv[])
 {
   java.awt.Frame theFrame = new java.awt.Frame(“One Racer”);
   TRacer aRacer = new TRacer(“Test”);
   theFrame.setSize(400,200);
   theFrame.add(aRacer, java.awt.BorderLayout.CENTER);
   theFrame.show();
   aRacer.paint(theFrame.getGraphics());
   theFrame.pack();
   aRacer.run();
   System.exit(0);
 }
 }

Listing 37.9 TSuspendable.java—Use This Interface to Ensure that the Ancestor Is Suspendable


 public interface TSuspendable
 {
   public boolean isSuspended();
 }

Recall (lines 79 and 80 of TRaceApplet.java follow) that the highest-numbered thread should always win.

         // should guarantee that the high-number thread wins
         fThreads[i].setPriority(Thread.MIN_PRIORITY+i);

As Figures 37.11 and 37.12 show, you can’t count on the priority mechanism to function as you’d expect. In AppletViewer, threads 2, 3, and 4 have a decided advantage over threads 0 and 1, but little difference exists within each group. Figure 37.12 shows that Netscape Navigator (a component of Netscape Communicator) ignores priority completely.


FIGURE 37.11  This version of the Great Thread Race is running on the JDK 1.2 AppletViewer.


FIGURE 37.12  When run in Navigator 4.04, thread priority seems to have no effect.

Working with an Object-Oriented Database

Sun supports an interface to relational databases called JDBC. For new applications, however, you may want to take advantage of the higher performance of an object-oriented database. Object Design, Inc. (http://www.odi.com/) offers a free version of its object-oriented database at http://www.odi.com/content/products/pse/PSEHome.html. It calls this version the Personal Storage Environment, or PSE.

After you’ve downloaded and installed the PSE, you can build and run applications such as DBDemo, shown in Listing 37.10. This program opens an existing database (demo.odb); if the database doesn’t exist, the program makes a new one.


NOTE:  After you’ve compiled this applet, you’ll need to make class TNurse persistent. Follow the instructions that come with PSE and use its osjcfp utility to make TNurse persistent.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.