Taking Advantage of the Applet API

The applet API lets you take advantage of the close relationship that applets have with Web browsers. The API is provided by the java.applet packagemainly by the Applet [1] class and the AppletContext [2] interface. Thanks to the applet API, applets can:

[1] http://java.sun.com/products/jdk/1.1/api/java.applet.Applet.html

[2] http://java.sun.com/products/jdk/1.1/api/java.applet.AppletContext.html

  • Be notified by the browser of milestones
  • Load data files specified relative to the URL of the applet or the page in which it is running
  • Display short status strings
  • Make the browser display a document
  • Find other applets running in the same page
  • Play sounds
  • Get parameters specified by the user in the tag

    Finding and Loading Data Files

    Whenever an applet needs to load some data from a file that's specified with a relative URL (a URL that doesn't completely specify the file's location), the applet usually uses either the code base or the document base to form the complete URL. The code base, returned by the Applet getCodeBase method, is a URL that specifies the directory from which the applet's classes were loaded. The document base, returned by the Applet getDocumentBase method, specifies the directory of the HTML page that contains the applet.

    Unless the tag specifies a code base, both the code base and the document base refer to the same directory on the same server. For example, in Figure 116, the code base and the document base would both specify the someDirectory directory.

    Data that the applet always needs or needs to rely on as a backup is usually specified relative to the code base. Data that the applet user specifies, often by using parameters, is usually specified relative to the document base.

    Note

    For security reasons, browsers limit the URLs from which untrusted applets can read. For example, most browsers don't allow untrusted applets to use ".." to access directories above the code base or the document base. Also, because untrusted applets can't read files except those on the applet's originating host, the document base isn't generally useful if the document and the untrusted applet are on different servers.

     

    The Applet class defines convenient forms of image-loading and sound-loading methods that let you specify images and sounds relative to a base URL. For example, assume that an applet is set up with one of the directory structures shown in Figure 116.

    Figure 116. This applet's image file is in the imgDir directory, relative to the code base.

    graphics/apbfig09.gif

    To create an Image object using the a.gif image file under imgDir, the applet can use the following code:

    Image image = getImage(getCodeBase(), "imgDir/a.gif"); 

    Displaying Short Status Strings

    Most browsers allow applets to display a short status string. This string typically appears on the status line of the window containing the applet. In full-fledged Web browsers, all applets on the page, as well as the browser itself, generally share the same status line.

    You should never put crucial information in the status line. If many users might need the information, it should instead be displayed within the applet area. If only a few, sophisticated users might need the information, consider displaying it on the standard output. See the section Displaying Diagnostics to the Standard Output and Standard Error Streams (page 446) for details.

    The status line is not usually very prominent, and it can be overwritten by other applets or by the browser. For these reasons, use the status line only for incidental, transitory information. For example, an applet that loads several image files might display the name of the image file it is currently loading.

    Applets display status lines with the showStatus method. Here's an example of its use:

    showStatus("MyApplet: Loading image file " + filename); 

    Note

    Please don't put scrolling text in the status line. Browser users find such status line abuse highly annoying!

     

    Displaying Documents in the Browser

    Have you ever wanted an applet to display formatted HTML text? Here's the easy way to do it: Ask the browser to display the text for you.

    With the AppletContext showDocument methods, an applet can tell the browser which URL to show and in which browser window. (By the way, the JDK Applet Viewer ignores these methods, because it can't display documents.) Here are the two forms of showDocument:

    showDocument(java.net.URL url) 
    showDocument(java.net.URL url, String targetWindow) 

    The one-argument form of showDocument simply tells the browser to display the document at the specified URL, without specifying the window in which to display the document.

    Terminology Note

    In this discussion, frame does not refer to a Frame object but rather to an HTML frame within a browser window.

     

    The two-argument form of showDocument lets you specify the window or the HTML frame in which to display the document. The second argument can have any one of the following values:

    "_blank"

    Displays the document in a new, nameless window.

    " windowName"

    Displays the document in a window named windowName . This window is created if necessary.

    "_self"

    Displays the document in the window and frame that contain the applet.

    "_parent"

    Displays the document in the applet's window but in the parent frame of the applet's frame. If the applet frame has no parent frame, this acts the same as "_self".

    "_top"

    Displays the document in the applet's window but in the top-level frame. If the applet's frame is the top-level frame, this acts the same as "_self".

    graphics/intfig04.gif

    The following applet lets you try every option of both forms of showDocument. The applet brings up a window that lets you type in a URL and choose any of the showDocument options. When you press Return or click the Show document button, the applet calls show-Document. [1]

    [1] ShowDocument.java is included on the CD and is available online. See Code Samples (page 463).

    http://java.sun.com/docs/books/tutorial/applet/appletsonly/browser.html

    graphics/apbfig10.gif

    Following is the applet code that calls showDocument.

     ...//In an Applet subclass: 
     urlWindow = new URLWindow(getAppletContext()); 
     . . . 
    
    class URLWindow extends Frame { 
     . . . 
     public URLWindow(AppletContext appletContext) { 
     . . . 
     this.appletContext = appletContext; 
     . . . 
     } 
    
     public void actionPerformed(ActionEvent event) { 
     String urlString = /* user-entered string */; 
     URL url = null; 
     try { 
     url = new URL(urlString); 
     } catch (MalformedURLException e) { 
     ...//Inform the user and return... 
     } 
    
     if (url != null) { 
     if (/* user doesn't want to specify window */) { 
     appletContext.showDocument(url); 
     } else { 
     appletContext.showDocument(url, 
     /* user-specified window */); 
     } 
     } 
     } 
    } 

    Sending Messages to Other Applets

    Applets can find other applets and send messages to them, with the following security restrictions.

    • Many browsers require that the applets originate from the same server.
    • Many browsers also require that the applets originate from the same directory on the server (that is, the same code base).
    • The Java platform API requires that the applets be running on the same page, in the same browser window.

    Note

    Some browsers let applets invoke methods on other appletseven applets on different pages in the same browseras long as all the applets come from the same code base. This method of interapplet communication isn't supported by the Java API, so it's possible that it will not be supported by all browsers.

     

    An applet can find another applet either by looking it up by name (using the AppletContext getApplet method) or by finding all the applets on the page (using the AppletContext getApplets method). Both methods, if successful, give the caller one or more Applet objects. Once it finds an Applet object, the caller can invoke methods on the object.

    Finding an Applet by Name: The getApplet Method

    The getApplet method looks through all the applets on the current page to see whether one has the specified name. If so, getApplet returns the applet's Applet object.

    By default, an applet has no name. For an applet to have a name, you must specify one in the HTML code that adds the applet to a page. You can do this by specifying either:

    • A NAME attribute within the applet's tag; for example:
      NAME="buddy"> 
      . . . 
      • A NAME parameter with a tag; for example:

         
         
        . . . 

      Browser Note

      Although at least one Java-enabled browser conducts a case-sensitive search, the expected behavior is for the getApplet method to perform a case-insensitive search. For example, getApplet("old pal") and getApplet("OLD PAL") should both find an applet named "Old Pal".

       
      graphics/intfig04.gif

      Following are two applets that illustrate lookup by name. The first applet, the Sender, looks up the second, the Receiver. [1] When it finds the Receiver, the Sender sends a message to the Receiver by invoking one of the Receiver's methods (passing the Sender's name as an argument). The Receiver reacts to this method call by changing its leftmost text string to Received message from sender-name!

      [1] Sender.java and Receiver.java are included on the CD and available online. See Code Samples (page 463).

      http://java.sun.com/docs/books/tutorial/appletsonly/iac.html

      graphics/apbfig11.gif

      Try This

      Visit the page that contains these applets: http://java.sun.com/docs/books/tutorial/applet/appletsonly/iac.html. Click the Send message button of the top applet (the Sender). Some status information will appear in the Sender's window, and the Receiver will confirm, with its own status string, that it received a message. After you've read the Receiver's status string, click its Clear button to reset it. In the Sender's text field labeled Receiver name:, type buddy and press Return. Because buddy is the Sender's own name, it will find an applet named buddy, but it won't send it a message, as it isn't a Receiver instance.

       

      The code the Sender uses to look up and to communicate with the Receiver is listed next. Code that you can use in your own applet is in boldface.

      Applet receiver = null; 
      String receiverName = nameField.getText(); //Name to search for 
      receiver = getAppletContext().getApplet(receiverName); 

      The Sender goes on to make sure that the Receiver was found and that it's an instance of the correct class (Receiver). If all goes well, the Sender sends a message to the Receiver:

      if (receiver != null) { 
       //Use the instanceof operator to make sure the applet 
       //we found is a Receiver object. 
       if (!(receiver instanceof Receiver)) { 
       status.append("Found applet named " 
       + receiverName + ", " 
       + "but it's not a Receiver object." 
       + newline); 
       } else { 
       status.append("Found applet named " 
       + receiverName + newline 
       + " Sending message to it.); 
       + newline); 
       //Cast the receiver to be a Receiver object 
       //(instead of just an Applet object) so that the 
       //compiler will let us call a Receiver method. 
       ((Receiver)receiver).processRequestFrom(myName); 
       } 
      } 

      From an applet's point of view, its name is stored in a parameter called NAME. It can get the value of the parameter by using the Applet getParameter method. For example, Sender gets its own name with the following code:

      myName = getParameter("NAME"); 

      For more information on using getParameter, see the section Writing the Code to Support Parameters (page 436).

      The example applets in this section perform one-way communicationfrom the Sender to the Receiver. If you want your receiver to be able to send messages to the sender, just have the sender give a reference to itself (this) to the receiver. For example:

      ((Receiver)receiver).startCommunicating(this); 

      Finding All the Applets on a Page: The getApplets Method

      graphics/intfig04.gif

      The getApplets method returns a list (an Enumeration, [1] to be precise) of all the applets on the page. For security reasons, getApplets returns only those applets that originated from the same host as the applet that is calling getApplets. Following is an applet that simply lists all the applets it can find on its page: [2]

      [1] http://java.sun.com/products/jdk/1.1/api/java.util.Enumeration.html

      [2] GetApplets.java is included on the CD and is available online. See Code Samples (page 463).

      http://java.sun.com/docs/books/tutorial/applet/appletsonly/iac.html

      graphics/apbfig12.gif

      The following code contains the relevant parts of the method that calls getApplets:

      public void printApplets() { 
       //Enumeration will contain all applets on this page 
       //(including this one) that we can send messages to. 
       Enumeration e = getAppletContext().getApplets(); 
       . . . 
       while (e.hasMoreElements()) { 
       Applet applet = (Applet)e.nextElement(); 
       String info = applet.getAppletInfo(); 
       if (info != null) { 
       textArea.append("- " + info + newline); 
       } else { 
       textArea.append("- " 
       + applet.getClass().getName() 
       + newline); 
       } 
       } 
       . . . 
      } 

      Playing Sounds

      In the applet package (java.applet), the Applet class and the AudioClip interface provide basic support for playing sounds. In JDK 1.1, the API supports only one sound format: 8-bit, µ law, 8,000Hz, one-channel, Sun .au files. You can create these on a Sun workstation by using the audiotool application. You can convert files from other sound formats by using an audio format conversion program.

      Note

      Support for sound has improved in the recent releases of the Java 2 Platform. See the "Sound" chapter in the book, The Java Tutorial Continued. This chapter is also available on the CD or online at http://java.sun.com/docs/books/tutorial/sound/index.html

       

      Sound-Related Methods

      Following are the sound-related Applet methods:

      AudioClip getAudioClip(URL)

      AudioClip getAudioClip(URL, String)

      Return an object that implements the AudioClip interface.

      void play(URL)

      void play(URL, String)

      Play the AudioClip corresponding to the specified URL.

      The two-argument form of each method takes a base URL, which is usually returned by either getCodeBase or getDocumentBase, and the location of the sound file relative to the base URL. You should use the code base for sounds that are integral to the applet. The document base is for sounds specified by the applet user, such as through applet parameters.

      The AudioClip interface defines the following methods:

      void loop()

      Starts playing the clip repeatedly.

      void play()

      Plays the clip once.

      void stop()

      Stops the clip. Works with both looping and one-time sounds.

      An Example: SoundExample

      graphics/intfig04.gif

      Here is an applet called SoundExample [1] that illustrates a few things about sound:

      [1] SoundExample.java is included on the CD and is available online. See Code Samples (page 463).

      http://java.sun.com/docs/books/tutorial/applet/appletsonly/sound.html

      graphics/apbfig13.gif

      The SoundExample applet provides an architecture for loading and playing multiple sounds in an applet. For this reason, it is more complex than necessary. Essentially, the sound loading and playing code boils down to this:

      AudioClip onceClip, loopClip; 
      onceClip = applet.getAudioClip(getCodeBase(), "bark.au"); 
      loopClip = applet.getAudioClip(getCodeBase(), "train.au"); 
      onceClip.play();//Play it once. 
      onceClip.stop();//Cut off the sound. 
      loopClip.loop();//Start the sound loop. 
      loopClip.stop();//Stop the sound loop. 

      Because nothing is more annoying than an applet that continues to make noise after you've left its page, the SoundExample applet stops playing the continuously looping sound when the user leaves the page and resumes playing it when the user comes back. The applet does this by implementing its stop and start methods, as shown:

      public void stop() { 
       onceClip.stop(); //Cut short the one-time sound. 
       if (looping) { 
       loopClip.stop(); //Stop the sound loop. 
       } 
      } 
      
      public void start() { 
       if (looping) { 
       loopClip.loop(); //Restart the sound loop. 
       } 
      } 

      This example is discussed in more detail in the section Using a Thread to Perform a One-Time Task (page 452).

      Defining and Using Applet Parameters

      Parameters are to applets what command line arguments are to UNIX applications. Parameters allow the user to customize the program's operation. By defining parameters, you can increase your applet's flexibility, making your applet work in multiple situations without your having to recode and recompile it.

      Deciding Which Parameters to Support

      As you implement parameters, you should ask four questions:

      1. What should the applet allow the user to configure?
      2. What should the parameters be named?
      3. What kind of value should each parameter take?
      4. What should the default value of each parameter be?

      What Should the Applet Allow the User to Configure?

      Which parameters your applet should support depends on what your applet does and on how flexible you want it to be. Applets that display images might have parameters to specify the image locations. Similarly, applets that play sounds might have parameters to specify the sounds.

      Besides parameters that specify resource locations, such as image and sound files, applets sometimes provide parameters for specifying details of the applet's appearance or operation. For example, an animation applet might let the user specify the number of images shown per second. Or, an applet might let the user change the strings that the applet displays. Anything is possible.

      What Should the Parameters Be Named?

      Once you decide what parameters your applet will support, you need to determine their names. Here are some typical parameter names:

      SOURCE or SRC

      For a data file such as an image file.

      XXX SOURCE (for example, IMAGESOURCE )

      Used in applets that let the user specify more than one type of data file.

      XXX S

      For a parameter that takes a list of XXX s, where XXX might be IMAGE, again.

      NAME

      Used only for an applet's name. Applet names are used for interapplet communication, as described in the section Sending Messages to Other Applets (page 427).

      Clarity of names is more important than keeping the name length short. Do not use names of tag attributes, which are documented in the section TheTag in Appendix E (page 537).

      Note

      Although this tutorial usually uses all uppercase letters to refer to parameter names, parameter names actually are case insensitive. For example, IMAGESOURCE and imageSource both refer to the same parameter. Parameter values, on the other hand, are case sensitive unless you take steps to interpret them otherwise, such as by using the String toLowerCase method before interpreting the parameter's value.

       

      What Kind of Value Should Each Parameter Take?

      Parameter values are all strings. Regardless of whether the user puts quotation marks around a parameter value, that value is passed to your applet as a string. However, your applet can interpret the string in many ways, typically as one of the following types:

      • A URL
      • An integer
      • A floating-point number
      • A Boolean valuetypically true/false or yes/no
      • A stringfor example, the string to use as a window title
      • A list of any of the preceding

      What Should the Default Value of Each Parameter Be?

      Applets should attempt to provide useful default values for each parameter so that the applet will execute even if the user doesn't specify a parameter or specifies it incorrectly. For example, an animation applet should provide a reasonable setting for the number of images it displays per second. In this way, if the user doesn't specify the relevant parameter, the applet will still work well.

      graphics/intfig04.gif

      Take the AppletButton as an example. [1] Its GUI is simple: a button and a label that displays status. When the user clicks the button, the applet brings up a window. The AppletButton class is flexible because it defines parameters that let the user specify any or all of the following:

      [1] AppletButton.java is included on the CD and is available online. See Code Samples (page 463).

      • The type of window to bring up
      • The window's title
      • The window's height
      • The window's width
      • The label of the button that brings up the window
      Here's what a typical tag for AppletButton looks like:
      
       

      When the user doesn't specify a value for a parameter, AppletButton uses a reasonable default value. For example, if the user doesn't specify the window's title, AppletButton uses the window's type as the title.

      Writing the Code to Support Parameters

      Applets use the Applet getParameter method to get user-specified values for applet parameters. The getParameter method is declared as follows:

      String getParameter(String name) 

      Your applet might need to convert the string that getParameter returns into another form, such as an integer. The java.lang package provides classes, such as Integer, that cast strings to primitive types. The following example from the AppletButton class converts a parameter's value into an integer:

      int requestedWidth = 0; 
      . . . 
      String windowWidthString = getParameter("WINDOWWIDTH"); 
      if (windowWidthString != null) { 
       try { 
       requestedWidth = Integer.parseInt(windowWidthString); 
       } catch (NumberFormatException e) { 
       //Use default width. 
       } 
      } 

      Note that if the user doesn't specify a value for the WINDOWWIDTH parameter, this code uses a default value of 0, which the applet interprets as "use the window's natural size." It's important to supply default values wherever possible.

      Besides using the getParameter method to get values of applet-specific parameters, you also can use getParameter to get the values of attributes of the applet's tag. For example, by specifying "HEIGHT" to the getParameter method, an applet could read the height that the user specified in the tag. See the section TheTag in Appendix E (page 537) for a complete list of tag attributes.

      Following is the AppletButton code that gets the applet's parameters:

      String windowClass; 
      String buttonText; 
      String windowTitle; 
      int requestedWidth = 0; 
      int requestedHeight = 0; 
      . . . 
      public void init() { 
       windowClass = getParameter("WINDOWCLASS"); 
       if (windowClass == null) { 
       windowClass = "TestWindow"; 
       } 
       buttonText = getParameter("BUTTONTEXT"); 
       if (buttonText == null) { 
       buttonText = "Click here to bring up a " + windowClass; 
       } 
      
       windowTitle = getParameter("WINDOWTITLE"); 
       if (windowTitle == null) { 
       windowTitle = windowClass; 
       } 
      
       String windowWidthString = getParameter("WINDOWWIDTH"); 
       if (windowWidthString != null) { 
       try { 
       requestedWidth = Integer.parseInt(windowWidthString); 
       } catch (NumberFormatException e) { 
       //Use default width. 
       } 
       } 
      
       String windowHeightString = getParameter("WINDOWHEIGHT"); 
       if (windowHeightString != null) { 
       try { 
       requestedHeight = 
       Integer.parseInt(windowHeightString); 
       } catch (NumberFormatException e) { 
       //Use default height. 
       } 
       } 

      Giving Information about Parameters

      Now that you've provided all those nice parameters to the user, you need to help the user set the parameter values correctly. Of course, your applet's documentation should describe each parameter and give the user examples and hints for setting them. Your job doesn't stop there, though. You also should implement the getParameterInfo method so that it returns information about your applet's parameters. Browsers can use this information to help the user set your applet's parameter values.

      The following code is an example of implementing the getParameterInfo method. This example is from the Animator applet, a wonderfully flexible applet that provides 13 parameters for users to customize their animation: [1]

      [1] Animator.java is included on the CD and is available online. See Code Samples (page 463).

      public String[][] getParameterInfo() { 
       String[][] info = { 
       // Parameter Name Kind of Value Description 
       {"imagesource", "URL", "a directory"}, 
       {"startup", "URL", "displayed at startup"}, 
       {"background", "URL", "displayed as background"}, 
       {"startimage", "int", "start index"}, 
       {"endimage", "int", "end index"}, 
       {"namepattern", "URL", "used to generate " + 
       "indexed names"}, 
       {"pause", "int", "milliseconds"}, 
       {"pauses", "ints", "milliseconds"}, 
       {"repeat", "boolean", "repeat or not"}, 
       {"positions", "coordinates", "path"}, 
       {"soundsource", "URL", "audio directory"}, 
       {"soundtrack", "URL", "background music"}, 
       {"sounds", "URLs", "audio samples"}, 
       }; 
       return info; 
      } 

      As you can see, the getParameterInfo method must return an array of three-String arrays. In each three-String array, the first string is the parameter name. The second string hints as to what general kind of value the applet needs for the parameter. The third string describes the meaning of the parameter.

      Using theTagThis section starts by showing you the tag's simplest form. This section then discusses some of the most common additions to that simple form: the tag, alternative HTML code and text, the CODEBASE attribute, and the ARCHIVE attribute. For a detailed description of the tag, refer to the section TheTag in Appendix E (page 537).You've already have seen the simplest form of the tag:

      numPixels HEIGHT=numPixels>

      This tag tells the browser to load the applet whose Applet subclass is named AppletSub class , displaying it in an area of the specified width and height.

      Note

      The World Wide Web Consortium (W3C) has deprecated the tag in the HTML 4.01 Specification in favor of the tag. The element is an all-purpose tag to include generic objectswhether they're applets or a new media typein Web pages. At present, not all browsers support the tag, but we mention it here for your future reference. Read the specification for more information:

      http://www.w3.org/TR/REC-html40/struct/objects.html

      To run in the Java Plug-in, the tag must be converted to and tags. For more information on converting and tags, visit: http://java.sun.com/products/plugin/ and search for the text "HTML Converter."

      Specifying Parameters

      Some applets let the user customize the applet's configuration with parameters. For example, AppletButton allows the user to set the button's text by specifying the value of a parameter named BUTTONTEXT.

      The user specifies the value of a parameter, using a tag. The tags should appear just after the tag for the applet they affect:

      numPixels HEIGHT=numPixels>parameter1Name VALUE=aValue>parameter2Name VALUE=anotherValue>

      Here's an example of the tag in use:

       
      . . . 

      Specifying Alternative HTML Code and Text

      Note the ellipsis points (...) in the previous HTML example. What did the example leave out? It omitted alternative HTML codeHTML code interpreted only by browsers that don't understand the tag. Alternative HTML code is any text that appears between the and the tags, after any tags. Browsers with a Java Virtual Machine ignore alternative HTML code.To specify alternative text to Java-enabled browsers and other browsers that understand the tag, use the ALT attribute. If it can't display an applet for some reason, the browser can display the applet's ALT text.

      We use alternative HTML code throughout the online version of this tutorial to tell readers about the applets they're missing. Often, the alternative HTML code includes one or more pictures of the applet. Here's the complete HTML code for the Animator example shown previously:

      Your browser is completely ignoring the <APPLET> tag!

      A browser that doesn't understand the tag ignores everything in the previous HTML code except the line that begins with Your browser is. A browser that does understand the tag ignores everything on that line. If it can't run the applet, the applet-savvy browser might display the text listed after the ALT tag.

      Specifying the Applet Directory

      By default, a browser looks for an applet's class and archive files in the same directory as the HTML file that has the tag. (If the applet's class is in a package, the browser uses the package name to construct a directory path underneath the HTML file's directory.) Sometimes, it's useful to put the applet's files somewhere else. You can use the CODEBASE attribute to tell the browser in which directory the applet's files are located:

      anInt HEIGHT=anInt>

      If aURL is a relative URL, it's interpreted relative to the HTML document's location. By making aURL an absolute URL, you can load an applet from just about anywhereeven from another HTTP server.

      This tutorial uses CODEBASE="someDirectory/" frequently, because we group the examples for each section in subdirectories. For example, here's the tag that includes the Simple applet in the section The Life Cycle of an Applet (page 410):

      Figure 117 shows the location of the class file, relative to the HTML file, when CODEBASE is set to "example/". Figure 118 shows where the applet class can be if you specify an absolute URL for the value of CODEBASE.

      Figure 117. The location of an applet's class file when CODEBASE = "example/".

      graphics/apbfig14.gif

      Figure 118. The directory hierarchy when CODEBASE is set to an absolute URL.

      graphics/apbfig15.gif

      Combining an Applet's Files into a Single File

      If your applet has more than one file, you should consider providing an archive file that bundles the applet's files into a single file. Whether archive files make sense for your applet depends on several factors, including your applet's size, performance considerations, and the environment you expect your users to have.

      Archive files reduce your applet's total download time. Much of the time saved comes from reducing the number of HTTP connections that the browser must make. Each HTTP connection can take several seconds to start. This means that for a multifile applet, connection time can dwarf transfer time. You can further reduce transfer time by compressing the files in your archive file.

      If you specify one or more archive files, the browser looks for them in the same directory that it would search for the applet class file. The browser then looks for the applet's class files in the archive files. If a file isn't in the archive, the browser generally tries to load it just as it would if the archive file weren't present.

      The standard Java archive format, called JAR (Java ARchive), was introduced in JDK 1.1 and is based on the ZIP file format. [1] You specify JAR files by using the ARCHIVE attribute of the tag. You can specify multiple archive files by separating them with commas:

      [1] See the "JAR File Format" trail for the latest information about browser support for JAR and details on JAR in the Java 2 Platform. This trail is included in the book The Java Tutorial Continued and is also included on this book's CD and online.

      anInt HEIGHT=anInt> 

      Platform Specific Details Creating a JAR File

      You can create JAR files by using the JDK jar tool. Some examples of creating JAR files follow.

      To create a JAR file named file.jar that includes compressed versions of all the class and GIF files in the current directory:

      jar cvf file.zip *.class *.gif 

      To create a JAR file for an applet whose classes are in a package named com.mycompany.myproject:

      Solaris:

      jar cvf file.zip com/mycompany/myproject/*.class *.gif 

      Win32:

      jar cvf file.zip commycompanymyproject*.class *.gif 

      For detailed descriptions of other attributes, see the section TheTag in Appendix E (page 537).

      Practical Consideration of Writing Applets

Getting Started

Object-Oriented Programming Concepts

Language Basics

Object Basics and Simple Data Objects

Classes and Inheritance

Interfaces and Packages

Handling Errors Using Exceptions

Threads: Doing Two or More Tasks at Once

I/O: Reading and Writing

User Interfaces That Swing

Appendix A. Common Problems and Their Solutions

Appendix B. Internet-Ready Applets

Appendix C. Collections

Appendix D. Deprecated Thread Methods

Appendix E. Reference



The Java Tutorial(c) A Short Course on the Basics
The Java Tutorial: A Short Course on the Basics, 4th Edition
ISBN: 0321334205
EAN: 2147483647
Year: 2002
Pages: 125

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net