Wednesday 13 May 2015

The APPLET Tag in Detail

 Now that a simple Java-enabled Web page has been demonstrated, it is time to explore the APPLETtag in more detail. The basic description of this tag is as follows:
<APPLET attributes>

applet_parameters
alternate_content
</APPLET>
What you are concerned with here are the attributesand the applet_parameters.

<APPLET>Tag Attributes

The APPLET tag supports a number of standard attributes. The majority of these attributes are nearly identical to the attributes of the IMGtag (to be discussed later in this chapter). The three requiredattributes are CODE/CODEBASE, WIDTH, and HEIGHT. The following list describes each attribute and its meaning.
ALT-Alternate text that can be displayed by text-only browsers. Some browsers also show this text when the applet is loading.
ALIGN-The ALIGNattribute designates the alignment of the applet within the browser page. Here are the possible choices for alignment:
  • left-Aligns the applet at the left margin. The left and right values allow text to flow around the applet. (For spacing around the applet, see the VSPACEand HSPACE attributes.)
  • right-Aligns the applet at the right margin.
  • top-Aligns the applet with the topmost item on the current line in the HTML file.
  • texttop-Aligns the applet with the top of the tallest text in the current line of the HTML file.
  • middle-Aligns the applet with the middle of the baseline of the text in the current line of the HTML file.
  • absmiddle-Aligns the middle of the applet with the middle of the largest item (text or otherwise) in the current line of the HTML file.
  • baseline-Aligns the bottom of the applet with the baseline of the current line of the HTML file.
  • bottom-Equivalent to baseline.
  • absbottom-Aligns the bottom of the applet with the lowest item (text or otherwise) in the current line of the HTML file.
CODE-(Required) The CODEattribute is used to indicate the .classfile that loads the applet. This file must be in the same directory location as this HTML file. To load an applet located in a different directory, use the CODEBASEattribute.
CODEBASE-The CODEBASEattribute is used to indicate the location of the .classfile that loads the applet. This attribute can contain either a directory name or a URL.
HEIGHT-(Required) The HEIGHTattribute is used to set the applet's box height.
HSPACE-The HSPACEattribute sets the amount of horizontal space to be set off around the applet. Both the HSPACEand VSPACE attributes are used only when the ALIGNattribute is equal to LEFTor RIGHT.
NAME-The NAMEattribute sets the symbolic name of the applet. This attribute is useful when multiple applets present on a page need to locate each other.
VSPACE-The VSPACEattribute sets the amount of vertical space to be set off around the applet. Both the HSPACEand VSPACE attributes are used only when the ALIGNattribute is equal to LEFTor RIGHT.
WIDTH-(Required) The WIDTHattribute is used to set the applet's box width.

Examples Using <APPLET>Attributes

This section presents some examples detailing the use of the APPLETtag's attributes. Because the most commonly used attributes are the ALIGN, HSPACE, and VSPACE attributes, special attention will be paid to them.
Example4.html, shown in Listing 12.6, makes use of the APPLETtag's ALIGN attribute. The HelloWorld applet is loaded several times onto one form to show the use of the ALIGN attribute. Listing 12.7 is Example5.html, which expands on Example4.htmlby including the VSPACE and HSPACE attributes. 

Listing 12.6. Example4.html: Various uses of the APPLETtag's ALIGNattribute.
<HTML>

<HEAD>
<TITLE>This is my applet's first home!</TITLE>
</HEAD>
<BODY>
<H1>Welcome to the brave new world!</H1>
<BR>
<P>This page represents a first try at creating a fully functional HTML
Âpage.</P>
<BR>
<P>Below you will find my HelloWorld applet.</P>
<BR>
<P>Note the clever use of the ALIGN attribute to move the applet around
Âon the screen.</P>
<BR>

<P><APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=30 ALIGN=LEFT>
If you can see this, your browser does not support Java applets. 
</APPLET>
To the left you will notice the HelloWorld applet
</P>
<BR>

<P><APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=30 ALIGN=RIGHT>
If you can see this, your browser does not support Java applets. 
</APPLET>
To the right you will notice the HelloWorld applet
</P>
<BR>

</BODY>
</HTML>


Listing 12.7. Repeats Example4.html, but it now includes the VSPACEand HSPACEattributes.
<HTML>

<HEAD>
<TITLE>This is my applet's first home!</TITLE>
</HEAD>
<BODY>
<H1>Welcome to the brave new world!</H1>
<BR>
<P>This page represents a first try at creating a fully functional HTML
Âpage.</P>
<BR>
<P>Below you will find my HelloWorld applet.</P>
<BR>
<P>Note the clever use of the ALIGN, VSPACE, and HSPACE attributes to set the
 applet's location on the screen.</P>
<BR>

<P><APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=30 ALIGN=LEFT HSPACE=25>
If you can see this, your browser does not support Java applets. 
</APPLET>
To the left you will notice the HelloWorld applet
</P>
<BR>

<P><APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=30 ALIGN=RIGHT VSPACE=50>
If you can see this, your browser does not support Java applets. 
</APPLET>
To the right you will notice the HelloWorld applet
</P>
<BR>

</BODY>
</HTML>

<APPLET>Tag Parameters: The <PARAM>Tag

The APPLET parameters stored in the PARAM tag actually have little directly to do with HTML. Instead, they are parameters passed directly to a Java applet. It is the responsibility of the applet to check the parameter values and respond accordingly.
There are two steps to passing parameters to applets:
  1. Add the PARAM tag (with values) to the HTML source file.
  2. Add necessary code to the applet to retrieve these parameter values.

Passing Parameters to the Applet

The syntax required to pass these parameters is
<PARAM NAME=param_name VALUE=param_value>
In this syntax, param_nameand param_valueare the values passed to the Java applet.

Retrieving Parameters Within the Applet

Java applets retrieve these parameter values using the getParameter()method. Although this function can be called anywhere, the most common case occurs when this function is called in the applet's init() method. The method syntax is the following:
String getParameter(String name);

Note
When passing parameters to applets, keep in mind that the parameter names are case-sensitive! Also note that all parameter values are converted to strings.
It is now time to revisit the HelloWorldclass created earlier in Listing 12.4. A new class entitled FancyHelloWorldwill be created (see Listing 12.8) that will take a parameter named "COLOR" and draw the text on the screen according to the value of that parameter.

Listing 12.8. Class FancyHelloWorldreacts to the COLORparameter.
import java.awt.Font;

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

/* The following class prints the text "Hello World!" to the screen       */
/* It also accepts a font color as input and draws the text in that color */
public class FancyHelloWorld extends java.applet.Applet
{
  Font tempFont = new Font("Helvetica", Font.PLAIN, 20);
  Color tempColor;
  String tempString;

  public void init()
  {
    tempString = getParameter("COLOR"); 

    if (tempString.equals("WHITE")) 
      tempColor = new Color(255, 255, 255);
    else if (tempString.equals("BLACK")) 
      tempColor = new Color(0, 0, 0);
    else if (tempString.equals("GRAY")) 
      tempColor = new Color(128, 128, 128);
    else if (tempString.equals("RED")) 
      tempColor = new Color(255, 0, 0);
    else if (tempString.equals("GREEN")) 
      tempColor = new Color(0, 255, 0);
    else if (tempString.equals("BLUE")) 
      tempColor = new Color(0, 0, 255);
    else if (tempString.equals("YELLOW")) 
      tempColor = new Color(255, 255, 0);
    else if (tempString.equals("MAGENTA")) 
      tempColor = new Color(255, 0, 255);
    else if (tempString.equals("CYAN")) 
      tempColor = new Color(0, 255, 255);
    else if (tempString.equals("PINK")) 
      tempColor = new Color(255, 175, 175);
    else if (tempString.equals("ORANGE")) 
      tempColor = new Color(255, 200, 0);
    else
      tempColor = new Color(0, 0, 0);
  }

  public void paint(Graphics g)
  {
    g.setFont(tempFont);
    g.setColor(tempColor);
    g.drawString("Hello world!", 10, 25);
  }
}

Example6.html, shown in Listing 12.9, modifies the HTML file built in Example3.htmlby passing in several COLORparameters. 

Listing 12.9. Example6.htmlpasses several COLORparameters to FancyHelloWorld.class.

<HTML>

<HEAD>
<TITLE>This is my applet's first home!</TITLE>
</HEAD>
<BODY>
<H1>Welcome to the brave new world!</H1>
<BR>
<P>This page represents a first try at creating a fully functional HTML
Âpage.</P>
<BR>
<P>Below you will find my HelloWorld applet.</P>
<BR>

<APPLET CODE="FancyHelloWorld.class" WIDTH=150 HEIGHT=30> 
<PARAM NAME=COLOR VALUE="BLUE">
If you can see this, your browser does not support Java applets. 
</APPLET>
<BR>

<APPLET CODE="FancyHelloWorld.class" WIDTH=150 HEIGHT=30> 
<PARAM NAME=COLOR VALUE="RED">
If you can see this, your browser does not support Java applets. 
</APPLET>
<BR>

<APPLET CODE="FancyHelloWorld.class" WIDTH=150 HEIGHT=30> 
<PARAM NAME=COLOR VALUE="CYAN">
If you can see this, your browser does not support Java applets. 
</APPLET>
<BR>

<APPLET CODE="FancyHelloWorld.class" WIDTH=150 HEIGHT=30> 
<PARAM NAME=COLOR VALUE="YELLOW">
If you can see this, your browser does not support Java applets. 
</APPLET>

</BODY>
</HTML>

No comments:

Post a Comment