Monday 11 May 2015

HTML and Java Applets

 The last example took advantage of a file named Example2.htmlto display the applet within a Web browser. The actual Java code used to display the applet was examined in the last section, but you may be wondering where the additional text on the screen came from. Currently, Java applets can only be displayed within a Web browser by being embedded in a standard HTML page. This does not mean that the actual bytecode or source code of the applet is included within the HTML file. Instead, the HTML text contains a reference to the Java applet known as a tag. Each element within an HTML file to be displayed by a Web browser is referenced using different types of these tags. Listing 7.2 shows the contents of the HTML file used to load the LifeCycleApplet applet.

Listing 7.2. The Example2.htmlfile contents.

<HTML>

<HEAD>
<TITLE>This is the LifeCycle applet!</TITLE>
</HEAD>
<BODY>
<H1>Prepare to be amazed!</H1>
<BR>
<APPLET CODE="LifeCycleApplet.class" WIDTH=600 HEIGHT=50> 
If you can see this, your browser does not support Java applets. 
</APPLET>
</BODY>
</HTML>

 A quick examination of Listing 7.2 shows three primary elements:
  • The <TITLE>tag-Used to display the title caption for this page.
  • The <H1>tag-Used to represent the main heading for the page.
  • The <APPLET>tag-Used to represent a Java applet to be loaded.
If you are new to HTML, the most important point to realize is that nothing in this file specifies actual screen layout. The TITLE tag, for instance, does not specify that the title appear at (x,y) coordinates (150, 200) and that it should be set apart from the heading by ¨". HTML only specifies the markup that tells browsers whatinformation to display. The page designer is somewhat at the mercy of Web browser developers, although keep in mind that it is in the best interest of browser developers to provide a standardized "look-and-feel" to most Web pages. It is still common, however, to load pages side by side in Netscape Navigator and Microsoft Internet Explorer and notice slight layout differences.

The remainder of this section presents the <APPLET>tag.
The <APPLET>Tag
The syntax for using the <APPLET>tag is the following:
<APPLET attributes>

applet_parameters
alternate_content
</APPLET>
The APPLET attributes are standard values that all applets accept and are a standard part of HTML. The applet_parametersare applet-specific parameters that are read by the applet at runtime. This is a handy way of passing arguments to an applet to allow the applet to be more generic.
<APPLET>Tag Attributes
ALT-Alternate text that can be displayed by text-only browsers.
ALIGN-The ALIGNattribute designates the alignment of the applet within the browser page.
CODE-(Required) The CODEattribute is used to indicate the .classfile that loads the applet.
CODEBASE-The CODEBASEattribute is used to indicate the location of the .classfile that loads the applet.
HEIGHT-(Required) The HEIGHTattribute is used to set the applet's bounding rectangle height.
HSPACE-The HSPACEattribute sets the amount of horizontal space to set off around the applet.
NAME-The NAMEattribute sets the symbolic name of the applet.
VSPACE-The VSPACEattribute sets the amount of vertical space to set off around the applet.
WIDTH-(Required) The WIDTHattribute is used to set the applet's box width.

Passing Parameters to Java Applets

Parameters are an easy way to configure Java applets without actually changing the source file. In the previous applet example, the text drawn on the screen was drawn using the blue color. This was "hardwired" into the applet's code. However, just as easily, we could have passed a parameter to the applet specifying that it use the blue tag. Listing 7.3 shows the Example2.htmlfile modified to do just this.

Listing 7.3. Passing parameters to the applet using HTML parameters.
<HTML>

<HEAD>
<TITLE>This is the LifeCycle applet!</TITLE>
</HEAD>
<BODY>
<H1>Prepare to be amazed!</H1>
<BR>
<APPLET CODE="LifeCycleApplet.class" WIDTH=600 HEIGHT=50> 
<PARAM NAME=color VALUE="blue">
If you can see this, your browser does not support Java applets. 
</APPLET>
</BODY>
</HTML>

The only question left to be answered is this: how does the Java applet determine the value of the parameters? (Excellent question!) The answer is that the applet has to call the getParameter()method supplied by the java.applet.Appletparent class. Calling getParameter("color")using the previous Java applet example would return a Stringvalue containing the text "blue". It is then left up to the applet to take advantage of this information and actually paint the text blue on the screen.
Here are three methods commonly used by applets:
  • String getParameter(String name)-Returns the value for the specified parameter string
  • URL getCodeBase()-Returns the URL of the applet
  • URL getDocumentBase()-Returns the URL of the document containing the applet 

No comments:

Post a Comment