Putting an Applet on a Web Page

Applets are placed on a web page in the same way that anything unusual is put on a page: HTML markup tags describe the applet, which a web browser loads along with the other parts of the page. If you have used HTML to create a web page, you know that it's a way to combine formatted text, images, sound, and other elements together. HTML uses special commands called tags that are surrounded by < and > marks, including img for the display of images, p for the insertion of a paragraph mark, and <center> to center the text that follows until a </center> tag is reached. The performance of some of these HTML tags can be affected by attributes that determine how they function. For example, src is an attribute of the img tag, and it provides the name of the image file that should be displayed. The following is an example of an img tag:

<IMG src="graduation.jpg">


One way to place applets on a web page is by using an applet tag and several attributes. The following is an example of the HTML required to put an applet on a page:

<applet code="StripYahtzee.class" codebase="javadir" >
Sorry, no dice ... this requires a Java-enabled browser.
</applet>


The code attribute identifies the name of the applet's class file. If more than one class file is being used with an applet, code should refer to the main class file that is a subclass of the JApplet class. If there is no code attribute, all files associated with the applet should be in the same folder as the web page that loads the program. codebase should contain a reference to the folder or subfolder where the applet and any related files can be found. In the preceding example, codebase indicates that the StripYahtzee applet can be found in the javadir subfolder. The height and width attributes designate the exact size of the applet window on the web page. It must be big enough to handle the things you are displaying in your applet. In between the opening <applet> tag and the closing </applet> tag, you can provide an alternate of some kind for web users whose browser software cannot run Java programs (less than two percent of all web users run browsers that fall into this group). In the preceding example, the text "Sorry, no dice…this requires a Java-enabled browser" is displayed in place of the applet on a browser such as Lynx, which does not support Java. You can put instructions here on how to download a Java-enabled browser. You also can include hyperlinks and other HTML elements. Another useful attribute, align, designates how an applet will be displayed in relation to the surrounding material on the page, including text and graphics. The value align="left" lines up the applet to the left of adjacent page elements and align="right" lines it up to the right.

      
Comments